Skip to content

Commit 3cf8ea4

Browse files
committed
Adding log from Yii to artifacts when test fails
1 parent 7e2eaeb commit 3cf8ea4

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

src/Codeception/Lib/Connector/Yii2.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function createUrl($params)
261261
return is_array($params) ?$this->getApplication()->getUrlManager()->createUrl($params) : $params;
262262
}
263263

264-
public function startApp()
264+
public function startApp(\yii\log\Logger $logger = null)
265265
{
266266
codecept_debug('Starting application');
267267
$config = require($this->configFile);
@@ -278,7 +278,12 @@ public function startApp()
278278
$config = $this->mockMailer($config);
279279
/** @var \yii\web\Application $app */
280280
Yii::$app = Yii::createObject($config);
281-
Yii::setLogger(new Logger());
281+
282+
if ($logger !== null) {
283+
Yii::setLogger($logger);
284+
} else {
285+
Yii::setLogger(new Logger());
286+
}
282287
}
283288

284289
/**

src/Codeception/Lib/Connector/Yii2/Logger.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55

66
class Logger extends \yii\log\Logger
77
{
8+
/**
9+
* @var \SplQueue
10+
*/
11+
private $logQueue;
12+
13+
/**
14+
* @var int
15+
*/
16+
private $maxLogItems;
17+
18+
public function __construct($maxLogItems = 5, $config = [])
19+
{
20+
parent::__construct($config);
21+
$this->logQueue = new \SplQueue();
22+
$this->maxLogItems = $maxLogItems;
23+
}
24+
825
public function init()
926
{
1027
// overridden to prevent register_shutdown_function
@@ -28,6 +45,27 @@ public function log($message, $level, $category = 'application')
2845
$message = $message->__toString();
2946
}
3047

31-
Debug::debug("[$category] " . \yii\helpers\VarDumper::export($message));
48+
$logMessage = "[$category] " . \yii\helpers\VarDumper::export($message);
49+
50+
Debug::debug($logMessage);
51+
52+
$this->logQueue->enqueue($logMessage);
53+
if ($this->logQueue->count() > $this->maxLogItems) {
54+
$this->logQueue->dequeue();
55+
}
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getAndClearLog()
62+
{
63+
$completeStr = '';
64+
foreach ($this->logQueue as $item) {
65+
$completeStr .= $item . PHP_EOL;
66+
}
67+
$this->logQueue = new \SplQueue();
68+
69+
return $completeStr;
3270
}
3371
}

src/Codeception/Module/Yii2.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule
206206
*/
207207
private $server;
208208

209+
/**
210+
* @var Yii2Connector\Logger
211+
*/
212+
private $yiiLogger;
213+
209214
public function _initialize()
210215
{
211216
if ($this->config['transaction'] === null) {
@@ -227,7 +232,8 @@ protected function onReconfigure()
227232
parent::onReconfigure();
228233
$this->client->resetApplication();
229234
$this->configureClient($this->config);
230-
$this->client->startApp();
235+
$this->yiiLogger->getAndClearLog();
236+
$this->client->startApp($this->yiiLogger);
231237
}
232238

233239
/**
@@ -312,7 +318,8 @@ protected function recreateClient()
312318
public function _before(TestInterface $test)
313319
{
314320
$this->recreateClient();
315-
$this->client->startApp();
321+
$this->yiiLogger = new Yii2Connector\Logger();
322+
$this->client->startApp($this->yiiLogger);
316323

317324
$this->connectionWatcher = new Yii2Connector\ConnectionWatcher();
318325
$this->connectionWatcher->start();
@@ -379,6 +386,16 @@ public function _after(TestInterface $test)
379386
parent::_after($test);
380387
}
381388

389+
public function _failed(TestInterface $test, $fail)
390+
{
391+
$log = $this->yiiLogger->getAndClearLog();
392+
if (! empty($log)) {
393+
$test->getMetadata()->addReport('yii-log', $log);
394+
}
395+
396+
parent::_failed($test, $fail);
397+
}
398+
382399
protected function startTransactions()
383400
{
384401
if ($this->config['transaction']) {

0 commit comments

Comments
 (0)