Dynamic setUp to create an appropriate context in your tests

0.2.0 2014-08-25 21:40 UTC


SensioLabsInsight Build Status Scrutinizer Quality Score Code Coverage


**This project is dead. Please use this one instead: You won't regret it ;) **

Thanks to annotations, this library allows you to call dynamic/custom "setUp" methods before each one of your test. It eases the understanding of your test because you explicitly set which context/variables you will use. It also can speed up your tests as you only initialize what they need.



Simply add this to your composer.json file:

"require": {
    "nikoms/phpunit-dynamic-fixture": "dev-master"

Then run php composer.phar install

PhpUnit configuration

To activate the plugin. Add the listener to your phpunit.xml(.dist) file:

<?xml version="1.0" encoding="UTF-8"?>
        <listener class="Nikoms\DynamicFixture\DynamicFixtureListener" file="vendor/nikoms/phpunit-dynamic-fixture/src/DynamicFixtureListener.php" />


Use the annotation "@setUpContext" to call the specified method(s) just before the test. Of course, you can add as many annotations as you want.

class MyTest extends PHPUnit_Framework_TestCase {

    private $name;

     * Must be public
    public function setUpName()
        $this->name = 'Nicolas';

    public function initContext()

     * @setUpContext setUpName
     * @setUpContext initContext
     * @setUpContext ...
    public function testSetUpName()
        //$this->name is "Nicolas"


If you don't like the name of the annotation, you can change it by passing a new one in the constructor:

 <listener class="Nikoms\DynamicFixture\DynamicFixtureListener" file="vendor/nikoms/phpunit-dynamic-fixture/src/DynamicFixtureListener.php">