Код: Выделить всё
use PHPUnit\Framework\TestCase;
class DataProviderSetupTest extends TestCase {
/**
* @dataProvider basicDataProvider
*/
public function testDataProvider(string $expected): void
{
$actualResult = 'I am the value!'; // generated by some service under test
$this->assertSame($expected, $actualResult);
}
public function basicDataProvider(): \Generator
{
yield 'no state, all fine' => ['I am the value!'];
}
}
use PHPUnit\Framework\TestCase;
class DataProviderSetupTest extends TestCase
{
protected string $state;
protected function setUp(): void
{
$this->state = 'Thank you for the fish!';
}
/**
* @dataProvider statefulDataProvider
*/
public function testDataProviderWithStateDependency(string $expected): void
{
$actualResult = 'Thank you for the fish!'; // generated by some service under test
$this->assertSame($expected, $actualResult);
}
public function statefulDataProvider(): \Generator
{
yield 'member not initialized' => [$this->state];
}
}
< /code>
Этот тест не удастся: < /p>
The data provider specified for DataProviderSetupTest::testDataProviderWithStateDependency is invalid.
Error: Typed property DataProviderSetupTest::$state must not be accessed before initialization
< /code>
Как инициализировать участники тестовых классов, чтобы я мог использовать их в поставщиках данных? < /code> Тем не менее, поскольку этот статичен, я не могу установить переменные члена.>
Подробнее здесь: https://stackoverflow.com/questions/794 ... ned-during
Мобильная версия