Variables in the @Dataprovider use PHPUnit @Depends


Unfortunately, it is currently not possible for PHPUnit testing both to highlight another test as a prerequisite for a test, to run the tests in case of an error does not occur, and at the same time a data provider for the test to use a previous test accessing the result. This is because, that data provider be initilisiert before iterating through the tests of PHPunit.

The problem

class StackTest extends PHPUnit_Framework_TestCase {
    protected static $foo;

    public function provider() { 
        print_r( self::$foo); //does not work
    }

    /**
     * @dataProvider provider
     */
    public function testOne() {
        self::$foo = array();
    }

    /**
     * @depends testOne
     * @dataProvider provider
     */
    public function testTwo( $data ) { 
    }

The solution

The problem in the current version of PHPUnit to work around 5.2+, by accessing you actually test on the static varaible, What is ugly, but the only way is:

class StackTest extends PHPUnit_Framework_TestCase {
    protected static $foo;

    public function provider() { 
      
    }

    /**
     * @dataProvider provider
     */
    public function testOne() {
        self::$foo = array();
    }

    /**
     * @depends testOne
     * @dataProvider provider
     */
    public function testTwo( $data ) { 
        $data->foo = self::$foo;

    }