HandsomeWong
HandsomeWong

Reputation: 1

phpunit dataprovider out of scope

The class I'm testing:

class Calculate {
    public $x;
    public $y;

    public function __construct($x, $y) {
        $this->x = $x;
        $this->y = $y;
    }

    public function add(): int {
        return $this->x + $this->y;
    }
}

My test code:

use PHPUnit\Framework\TestCase;

class CalculateTest extends TestCase {

    public function additionProvider() {
        return [
            [1, 2],
            [5, 8],
            [-1, 10],
            [66, 3],
            [9, 4]
        ];
    }

    /**
     * @dataProvider additionProvider
     */
    public function testGetIsOk($x, $y): Calculate {
        $c = new Calculate($x, $y);
        var_dump($c);
        $this->assertEquals($x, $c->x);
        $this->assertEquals($y, $c->y);

        return $c;
    }

    /**
     * @depends testGetIsOk
     */
    public function testAddIsNormal(Calculate $c):void {
        $this->assertEquals($c->x + $c->y, $c->add());
    }



}

There are 5 elements in the dataprovider, but the test result shows that the 6th test is error.

PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

.....E 6 / 6 (100%)

Time: 97 ms, Memory: 8.00MB

There was 1 error:

1) CalculateTest::testAddIsNormal TypeError: Argument 1 passed to CalculateTest::testAddIsNormal() must be an inst ance of Calculate, null given

Thanks.

Upvotes: 0

Views: 125

Answers (1)

HandsomeWong
HandsomeWong

Reputation: 1

Ok, I check the issue on github, and I finally know that I cannot pass the result of a test that get data from data providers to another test. There is it: https://github.com/sebastianbergmann/phpunit/issues/611

Upvotes: 0

Related Questions