Description
Description:
When running tests using Codeception with the Yii2 module, if ChromeDriver fails, such as in the following case:
session not created: This version of ChromeDriver only supports Chrome version 130
Current browser version is 135.0.7049.85 with binary path /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
It leads to an error in the test execution. The issue is related to the instantiation of yiiLogger
in the _before
event, but it is being accessed in the _failed
event before being properly initialized in case of failure. Specifically, in src/Codeception/Module/Yii2.php
, the following code is used in the _failed
event:
public function _failed(TestInterface $test, $fail): void
{
$log = $this->yiiLogger->getAndClearLog();
if ($log !== '') {
$test->getMetadata()->addReport('yii-log', $log);
}
parent::_failed($test, $fail);
}
But in the _before
method, yiiLogger
is instantiated as follows:
public function _before(TestInterface $test): void
{
$this->recreateClient();
$this->yiiLogger = new Yii2Connector\Logger();
$this->getClient()->startApp($this->yiiLogger);
$this->connectionWatcher = new ConnectionWatcher();
$this->connectionWatcher->start();
// load fixtures before db transaction
if ($test instanceof \Codeception\Test\Cest) {
$this->loadFixtures($test->getTestInstance());
} elseif ($test instanceof \Codeception\Test\TestCaseWrapper) {
$this->loadFixtures($test->getTestCase());
} else {
$this->loadFixtures($test);
}
$this->startTransactions();
}
If ChromeDriver fails before the _before
event is executed, the yiiLogger
is never initialized, and accessing it in _failed
results in the following error:
Typed property Codeception\Module\Yii2::$yiiLogger must not be accessed before initialization
Steps to reproduce:
- Run a test using Codeception with the Yii2 module.
- Ensure that ChromeDriver fails (e.g., by using an incompatible version of ChromeDriver).
- The error
Typed property Codeception\Module\Yii2::$yiiLogger must not be accessed before initialization
occurs.
Expected behavior:
The yiiLogger
should be properly initialized before being accessed, or there should be a check to ensure that it is only accessed if initialized. A more graceful handling of the failure, such as skipping the logging in case of initialization failure, would avoid this issue.
Component | Version |
---|---|
Yii2 | 2.0.52 |
Codeception | 5.1.2 |
Codeception Yii2 module | 1.1.12 |
PHP | 8.1 |