-
Notifications
You must be signed in to change notification settings - Fork 39
allow to override default Yii2 Logger class via DI container #1
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
Conversation
previous PR: Codeception/Codeception#5771 |
I think that your tests should be failing. /**
* @return Logger message logger
*/
public static function getLogger()
{
if (self::$_logger !== null) {
return self::$_logger;
}
return self::$_logger = static::createObject('yii\log\Logger');
}
/**
* Sets the logger object.
* @param Logger $logger the logger object.
*/
public static function setLogger($logger)
{
self::$_logger = $logger;
} While the code does not use type hinting because Yii2 supports older PHP versions, the PHPDoc clearly indicates that the param of If we merge this PR and allow for custom implementations that do not (properly) override the Given that you have basically changed the contract in your code: class Yii {
public static function getLogger(): LoggerInterface
{
...
} I'm inclined to recommend actually fixing your code using composition instead of inheritance: public static function setLogger(Logger $logger)
{
if ($logger instanceof LoggerInterface) {
self::$_logger = $logger;
} else {
// Deal with a logger that does not implement your interface
}
} I'm not a fan of static instantiation and would gladly replace it. That implementation doesn't replace your logger at all, so no issue there. All it does is add an extra log target for Codeception, which is what we want. Also, it mocks |
I understand what you said, but in my case it is a different use case. I just extended the default Logger with some helper method. For example But now, I decoupled these functions into a new component, so I do not need this feature anymore. |
Okay, thanks for spending your time making Codeception better! |
Fix last commit error: ```shell Exception 'Error' with message 'Call to a member function getAndClearLog() on null' in /srv/www/app/vendor/codeception/module-yii2/src/Codeception/Module/Yii2.php:400 Stack trace: #0 /srv/www/app/vendor/codeception/codeception/src/Codeception/Subscriber/Module.php(90): Codeception\Module\Yii2->_failed(Object(Codeception\Test\TestCaseWrapper), Object(PHPUnit\Framework\ExpectationFailedException)) Codeception#1 /srv/www/app/vendor/symfony/event-dispatcher/EventDispatcher.php(230): Codeception\Subscriber\Module->failed(Object(Codeception\Event\FailEvent), 'test.fail', Object(Symfony\Component\EventDispatcher\EventDispatcher)) ... ```
Fix last commit error: ```shell Exception 'Error' with message 'Call to a member function getAndClearLog() on null' in /srv/www/app/vendor/codeception/module-yii2/src/Codeception/Module/Yii2.php:400 Stack trace: #0 /srv/www/app/vendor/codeception/codeception/src/Codeception/Subscriber/Module.php(90): Codeception\Module\Yii2->_failed(Object(Codeception\Test\TestCaseWrapper), Object(PHPUnit\Framework\ExpectationFailedException)) #1 /srv/www/app/vendor/symfony/event-dispatcher/EventDispatcher.php(230): Codeception\Subscriber\Module->failed(Object(Codeception\Event\FailEvent), 'test.fail', Object(Symfony\Component\EventDispatcher\EventDispatcher)) ... ```
…g) of type string is deprecated
PHP 8.1: parse_str(): Passing null to parameter #1 ($string) of type string is deprecated
I have an enhanced Logger class which implements my
LoggerInterface
. All of my tests are failing because the default Logger class of Codeception is not implements this interface. I made a custom Logger class which extends the Codeception's Logger class and implements my LoggerInterface, but the Logger class is hardcoded.With this PR it is possible to override in the test application via the configuration of the DI container, with the following way: