Is it possible to access $this
in a PHPUnit BrowserTestBase
data provider method to get access to properties defined in the testBase class? It seems that the sequence of processing during startup of the tests means that the properties in my test base class are not available at the dataProvider stage, they are only available after the test function has actually started, and received its first test data parameters. But by this time it is too late and NULL has already been passed as the value.
Example, my TestBase class has
/**
* @var string
*/
protected $valueAsConstant = 'some predefined value';
/**
* @var string
*/
protected $valueInSetup;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->valueInSetup = 'value given in setUp method';
}
The actual test is
/**
* @dataProvider dataQuestion()
*/
public function testQuestion($value) {
}
public function dataQuestion() {
$data = [
[$this->valueAsConstant],
[$this->valueInSetup],
];
return $data;
}
This produces the following (url shortened for ease of reading)
Testing modules/d8testing
Test 'D8testingTest::testQuestion with data set #0 ('some predefined value')' started
Test 'D8testingTest::testQuestion with data set #0 ('some predefined value')' ended
Test 'D8testingTest::testQuestion with data set #1 (null)' started
Test 'D8testingTest::testQuestion with data set #1 (null)' ended
$this
as it doesOffCanvasTestBase::themeDataProvider()
. It would be better to show the code you are using, so the answers can better answer about what is wrong in your code.themeDataProvider()
exists in the OffCanvasTestBase class. The dataProvider function I am using is written in the actual test class. I will post my example here shortly, as I'm sure there is a simple way to solve it.protected $defaultTheme = 'stark';
and this property is availbale in the data provider method. Other properties that are not populated until thesetUp()
method is called, remain asnull
public function dataNonEnabledType() { $data = [ 1 => [$this->typeName], 2 => [$this->mediaTypeName], 3 => [$this->defaultTheme], ]; return $data; }
setUp()
is called before the test are executed, which means before the data provider is called. The properties should be already set.