Skip to content

Adding log from Yii to artifacts when test fails #8

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

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Codeception/Lib/Connector/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
}

/**
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 @@ -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) {
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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']) {
Expand Down