diff --git a/src/Codeception/Lib/Connector/Yii2.php b/src/Codeception/Lib/Connector/Yii2.php index 22b5e9d..20e95fa 100644 --- a/src/Codeception/Lib/Connector/Yii2.php +++ b/src/Codeception/Lib/Connector/Yii2.php @@ -261,7 +261,7 @@ public function createUrl($params) return is_array($params) ?$this->getApplication()->getUrlManager()->createUrl($params) : $params; } - public function startApp() + public function startApp(\yii\log\Logger $logger = null) { codecept_debug('Starting application'); $config = require($this->configFile); @@ -278,7 +278,12 @@ public function startApp() $config = $this->mockMailer($config); /** @var \yii\web\Application $app */ Yii::$app = Yii::createObject($config); - Yii::setLogger(new Logger()); + + if ($logger !== null) { + Yii::setLogger($logger); + } else { + Yii::setLogger(new Logger()); + } } /** diff --git a/src/Codeception/Lib/Connector/Yii2/Logger.php b/src/Codeception/Lib/Connector/Yii2/Logger.php index b87b14d..3135962 100644 --- a/src/Codeception/Lib/Connector/Yii2/Logger.php +++ b/src/Codeception/Lib/Connector/Yii2/Logger.php @@ -5,6 +5,23 @@ class Logger extends \yii\log\Logger { + /** + * @var \SplQueue + */ + private $logQueue; + + /** + * @var int + */ + private $maxLogItems; + + public function __construct($maxLogItems = 5, $config = []) + { + parent::__construct($config); + $this->logQueue = new \SplQueue(); + $this->maxLogItems = $maxLogItems; + } + public function init() { // overridden to prevent register_shutdown_function @@ -28,6 +45,27 @@ public function log($message, $level, $category = 'application') $message = $message->__toString(); } - Debug::debug("[$category] " . \yii\helpers\VarDumper::export($message)); + $logMessage = "[$category] " . \yii\helpers\VarDumper::export($message); + + Debug::debug($logMessage); + + $this->logQueue->enqueue($logMessage); + if ($this->logQueue->count() > $this->maxLogItems) { + $this->logQueue->dequeue(); + } + } + + /** + * @return string + */ + public function getAndClearLog() + { + $completeStr = ''; + foreach ($this->logQueue as $item) { + $completeStr .= $item . PHP_EOL; + } + $this->logQueue = new \SplQueue(); + + return $completeStr; } } diff --git a/src/Codeception/Module/Yii2.php b/src/Codeception/Module/Yii2.php index 5ffe49a..1ae309d 100644 --- a/src/Codeception/Module/Yii2.php +++ b/src/Codeception/Module/Yii2.php @@ -206,6 +206,11 @@ class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule */ private $server; + /** + * @var Yii2Connector\Logger + */ + private $yiiLogger; + public function _initialize() { if ($this->config['transaction'] === null) { @@ -227,7 +232,8 @@ protected function onReconfigure() parent::onReconfigure(); $this->client->resetApplication(); $this->configureClient($this->config); - $this->client->startApp(); + $this->yiiLogger->getAndClearLog(); + $this->client->startApp($this->yiiLogger); } /** @@ -312,7 +318,8 @@ protected function recreateClient() public function _before(TestInterface $test) { $this->recreateClient(); - $this->client->startApp(); + $this->yiiLogger = new Yii2Connector\Logger(); + $this->client->startApp($this->yiiLogger); $this->connectionWatcher = new Yii2Connector\ConnectionWatcher(); $this->connectionWatcher->start(); @@ -379,6 +386,16 @@ public function _after(TestInterface $test) parent::_after($test); } + public function _failed(TestInterface $test, $fail) + { + $log = $this->yiiLogger->getAndClearLog(); + if (! empty($log)) { + $test->getMetadata()->addReport('yii-log', $log); + } + + parent::_failed($test, $fail); + } + protected function startTransactions() { if ($this->config['transaction']) {