Skip to content

Commit

Permalink
Add log from Yii to artifacts when test fails (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
aywan committed Jun 17, 2022
1 parent 14269d0 commit cfb21a7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/Codeception/Lib/Connector/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,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);
Expand All @@ -297,7 +297,12 @@ public function startApp()
$config = $this->mockMailer($config);
/** @var \yii\base\Application $app */
Yii::$app = Yii::createObject($config);
Yii::setLogger(new Logger());

if ($logger !== null) {
Yii::setLogger($logger);
} else {
Yii::setLogger(new Logger());
}
}

/**
Expand Down
40 changes: 39 additions & 1 deletion src/Codeception/Lib/Connector/Yii2/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
21 changes: 19 additions & 2 deletions src/Codeception/Module/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule
*/
private $server;

/**
* @var Yii2Connector\Logger
*/
private $yiiLogger;

public function _initialize()
{
if ($this->config['transaction'] === null) {
Expand All @@ -233,7 +238,8 @@ protected function onReconfigure()
parent::onReconfigure();
$this->client->resetApplication();
$this->configureClient($this->config);
$this->client->startApp();
$this->yiiLogger->getAndClearLog();
$this->client->startApp($this->yiiLogger);
}

/**
Expand Down Expand Up @@ -318,7 +324,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();
Expand Down Expand Up @@ -385,6 +392,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']) {
Expand Down

0 comments on commit cfb21a7

Please sign in to comment.