Logging Inside PHPUnit Test Cases

Hi all,

I’m working on a site with yii, which is very nice and everything is fine except the logging doesn’t work when I run my test cases. I’m basically logging values inside my model classes which works when I run the application in the browser fine. My tests are supported by a bootstrapper and the models are run inside unit tests as usual.

The unit tests run fine, but they don’t add anything to the logs. Is there anything I need to do to like activate the logger in the bootstrapper?

Here’s my bootstrapper: -


<?php

  $yiit = '/usr/share/php/yii-framework/framework/yii.php';

  $config= dirname(__FILE__) . '/../config/test.php';


  require_once($yiit);	

  Yii::createWebApplication($config);

Here’s my test config snippet: -


'log'=>array(

  'class'=>'CLogRouter',

  'routes'=>array(

    array(

      'class'=>'CFileLogRoute',

      'levels'=>'error, warning, info, trace',

      'filter'=>'CLogFilter',

      'categories' => 'system.*'

    ),

  ),

),

Here’s how I’m logging my messages: -


Yii::log('example message', 'info', 'bobcom');

thanks for any help

David

I would like to figure out how to do this too, have you had any luck? I’ll let you know if I get anywhere.

Hi

Looks like your logger is monitoring the category "system.*" and the log you are testing with specifies "bobcom" as the category to log to. Since they do not match you will not the event. See the document here http://www.yiiframework.com/doc/guide/topics.logging

nZ

Hi,

Had the same problem…

Saw that qiang recommended to register a shutdown function here (http://www.yiiframework.com/forum/index.php?/topic/2798-yiilog-from-forked-thread-does-not-log/)

So I added to the bootstrap.php

function shutdown() {

Yii::app()-&gt;end();

}

register_shutdown_function(‘shutdown’);

cheers,

atom

Another helpful hint:

To see the tracing done by other parts of Yii, i.e., the Yii::trace calls, (for me the CActiveRecord SQL calls were particularly helpful) in unit tests, one needs to enable DEBUG mode in the unit tests, just as it is enabled in the index.php file.

I copied this fragment from index.php:


defined('YII_DEBUG') or define('YII_DEBUG',true);

defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 1);



to protected/tests/bootstrap.php

Then I can see the SQL generated by my overridden CActiveRecord methods.