Categorie
Information and Communications Technology

Yii Console Log with Unit Testing

1. install phpunit correctly
pear install phpunit/PHPUnit
pear install phpunit/PHPUnit_Selenium
2. create the class CConsoleLogRoute in the components directory in the file CConsoleLogRoute.php with this code:
<?php
class CConsoleLogRoute extends CLogRoute {
protected function processLogs($logs)
{
foreach($logs as $log) {
echo $this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
}
}
}
3. configure YII to log when unit testing:
3a. add the log key in the components array in protected/config/test.php
‘log’=>array(
‘class’=>’CLogRouter’,
‘routes’=>array(
array(
‘class’=>’CConsoleLogRoute’,
‘levels’=>’error, warning, info, trace’,
),
),
),
3b. add some lines in protected/tests/bootstrap.php, the modified file should be this: (added the lines with YII_DEBUG, YII_TRACE_LEVEL, autoFlush and autoDump)
<?php
// change the following paths if necessary
$yiit=dirname(__FILE__).’/../../../framework/yiit.php’;
$config=dirname(__FILE__).’/../config/test.php’;
defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true);
defined(‘YII_TRACE_LEVEL’) or define(‘YII_TRACE_LEVEL’, 0);
require_once($yiit);
require_once(dirname(__FILE__).’/WebTestCase.php’);
Yii::createWebApplication($config);
Yii::getLogger()->autoFlush = 1;
Yii::getLogger()->autoDump = true;