Pumpkin helps you to organize the storage of your mocks for PHPUnit. It gives mocks objects and database resources when you execute a test.
You just have to add the Pumpkin package in your composer.json and update your project.
$ composer require raphhh/pumpkin
Usually, when you use PHPUnit, you create a new test class extending \PHPUnit_Framework_TestCase
. Instead, with Pumpkin, you have to extend \Pumpkin\TestCase
.
\Pumpkin\TestCase
extends \PHPUnit_Framework_TestCase
, so you have the same interface as if you use PHPUnit.
But you can also know the current test with \Pumpkin\TestCase::getTest(
). You receive a \Pumpkin\Test\Test
object which reflects the current method of the test.
The current test is the method executed by PHPUnit and containing your assertions. Typically, this is a test* method.
For example
class FooTest extends Pumpkin\TestCase
{
function testA(){
$this->getTest()->getReflectedTestMethod()->getName(); //FooTest::testA
}
function testB(){
$this->getTest()->getReflectedTestMethod()->getName(); //FooTest::testB
}
}
With this method you can reflected the method of the executed test, retrieve annotations, ... anything!
Note that if you do not want to extend \Pumpkin\TestCase
, you can also directly extend \PHPUnit_Framework_TestCase
and use the trait \Pumpkin\Test\TestCaseTrait
.
You can retrieve mocks of a the current test. Mocks objects must be user objects. They are declared in a specific file for the current test.
The file is located in the following path:
/fixtures/mocks/{ClassTestName}/{methodName}.php
This path can start from the test directory or from a parent one.
You can declare as many mocks as you want in this file.
For example:
//Mocks
// /fixtures/mocks/FooTest/testA.php
class Mock1{}
class mock2{}
// Test case
class FooTest extends Pumpkin\TestCase
{
function testA(){
$this->getMocks(); //returns [Mock1, Mock2]
}
}
When you want to mock databases with PHPUnit, you need to use \PHPUnit_Extensions_Database_TestCase
. Instead, with Pumpkin, you have to extend \Pumpkin\Database\TestCase
.
\Pumpkin\Database\TestCase extends \PHPUnit_Extensions_Database_TestCase
, so you have the same interface as if you use PHPUnit.
Note that if you do not want to extend \Pumpkin\Database\TestCase
, you can also directly extend \PHPUnit_Extensions_Database_TestCase
and use the trait \Pumpkin\Test\Database\TestCaseTrait
.
But you can also specify the tables you want to load for the current test. This is done in the comments of the current test, with the @db tag.
// Test case
use Pumpkin\Database\Annotation as db;
class FooTest extends Pumpkin\Database\TestCase
{
/**
* @db("my_database.my_table")
* @db("my_other_database.my_other_table")
*/
function testA(){
//my_database.my_table and my_other_database.my_other_table data will be load when this test will be executed
}
}
The data are stored in a file with the following path:
/fixtures/databases/{databaseName}/data/{tableName}.csv
Currently, Pumpkin support only csv files, but it can be evolved in future.
Your data can be specific for the current test, or be common with several tests. Everything is determined by the data file path. If you want specific data, you have to locate the data file in the directory of your test. If you want common data, you have to locate the data file in a common directory of your tests.
If you want to load your mocked data, you have to allow PHPUnit to access to your database. With Pumpkin you have just to specify your config in the phpunit.xml.
For example
<phpunit>
<php>
<var name="db_dsn" value="mysql:dbname=my_db;host=127.0.0.1"/>
<var name="db_username" value="root"/>
<var name="db_password" value=""/>
</php>
... other phpunit config
</phpunit>