Skip to content

yiiLogger not initialized when ChromeDriver fails, causing error #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Eseperio opened this issue Apr 14, 2025 · 4 comments
Closed

yiiLogger not initialized when ChromeDriver fails, causing error #131

Eseperio opened this issue Apr 14, 2025 · 4 comments

Comments

@Eseperio
Copy link

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
@Biggy905
Copy link

Try this config:

'log' => [  
    'class' => \Codeception\Lib\Connector\Yii2\Logger::class,  
    'targets' => [],  
], 

@SamMousa
Copy link
Collaborator

Could you test this branch? #132

Try this config:

I don't think the issue is related to configuration.

@Biggy905
Copy link

Could you test this branch? #132

Try this config:

I don't think the issue is related to configuration.

Yes, indeed. The configuration doesn't affect it.
The only thing is, I have some questions regarding the logic.
Here, I commented out the line, and it started working.

I'd like to get your thoughts on this line.

https://github.com/Codeception/module-yii2/blob/v2.0.3/src/Codeception/Module/Yii2.php#L377

@SamMousa
Copy link
Collaborator

Read the comments above the file: https://github.com/Codeception/module-yii2/blob/v2.0.3/src/Codeception/Module/Yii2.php#L32

The line is needed; removing it changes the behavior in several ways and is not a solution.

SamMousa added a commit that referenced this issue May 31, 2025
fix: fixes #131 yiilogger may not be initialized in _failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants