Skip to content

yiiLogger not initialized when ChromeDriver fails, causing error #131

Closed
@Eseperio

Description

@Eseperio

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:

  1. Run a test using Codeception with the Yii2 module.
  2. Ensure that ChromeDriver fails (e.g., by using an incompatible version of ChromeDriver).
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions