Skip to content

Commit cfb21a7

Browse files
authored
Add log from Yii to artifacts when test fails (#8)
1 parent 14269d0 commit cfb21a7

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
@@ -276,7 +276,7 @@ public function createUrl($params)
276276
return is_array($params) ?$this->getApplication()->getUrlManager()->createUrl($params) : $params;
277277
}
278278

279-
public function startApp()
279+
public function startApp(\yii\log\Logger $logger = null)
280280
{
281281
codecept_debug('Starting application');
282282
$config = require($this->configFile);
@@ -297,7 +297,12 @@ public function startApp()
297297
$config = $this->mockMailer($config);
298298
/** @var \yii\base\Application $app */
299299
Yii::$app = Yii::createObject($config);
300-
Yii::setLogger(new Logger());
300+
301+
if ($logger !== null) {
302+
Yii::setLogger($logger);
303+
} else {
304+
Yii::setLogger(new Logger());
305+
}
301306
}
302307

303308
/**

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
@@ -212,6 +212,11 @@ class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule
212212
*/
213213
private $server;
214214

215+
/**
216+
* @var Yii2Connector\Logger
217+
*/
218+
private $yiiLogger;
219+
215220
public function _initialize()
216221
{
217222
if ($this->config['transaction'] === null) {
@@ -233,7 +238,8 @@ protected function onReconfigure()
233238
parent::onReconfigure();
234239
$this->client->resetApplication();
235240
$this->configureClient($this->config);
236-
$this->client->startApp();
241+
$this->yiiLogger->getAndClearLog();
242+
$this->client->startApp($this->yiiLogger);
237243
}
238244

239245
/**
@@ -318,7 +324,8 @@ protected function recreateClient()
318324
public function _before(TestInterface $test)
319325
{
320326
$this->recreateClient();
321-
$this->client->startApp();
327+
$this->yiiLogger = new Yii2Connector\Logger();
328+
$this->client->startApp($this->yiiLogger);
322329

323330
$this->connectionWatcher = new Yii2Connector\ConnectionWatcher();
324331
$this->connectionWatcher->start();
@@ -385,6 +392,16 @@ public function _after(TestInterface $test)
385392
parent::_after($test);
386393
}
387394

395+
public function _failed(TestInterface $test, $fail)
396+
{
397+
$log = $this->yiiLogger->getAndClearLog();
398+
if (! empty($log)) {
399+
$test->getMetadata()->addReport('yii-log', $log);
400+
}
401+
402+
parent::_failed($test, $fail);
403+
}
404+
388405
protected function startTransactions()
389406
{
390407
if ($this->config['transaction']) {

0 commit comments

Comments
 (0)