CakePHP Logging to Live DB During Unit Testing

◇◆丶佛笑我妖孽 提交于 2019-12-12 00:37:26

问题


I'm using DB logging in Cake 2.1, which works great.

The problem I'm having is when running Unit Tests, all logs are still getting sent to the live db rather than the test database.

All other db interactions go to test, except logging.

I do have a log fixture created and imported into the test case.

Here's my Database logger (/Lib/Log/Engine/DatabaseLogger.php)

App::uses('CakeLogInterface', 'Log');

class DatabaseLogger implements CakeLogInterface
{
    public function __construct($options = array() )
    {
        App::import('Model', 'Log');
        $this->Log = new Log;
    }

    public function write($type, $message)
    {
        $this->Log->create();

        $log['type'] = ucfirst($type);
        $log['date'] = date('Y-m-d H:i:s');
        $log['message'] = $message;

        return $this->Log->save($log);
    }
}

I'm sure I'm missing some basic setting here but I can't figure this out for the life of me.


回答1:


Well, in my case the problem was caused because of a bad initialization of a constructor. You can check the update solution here: How to choose the test DB cakePHP testing

And here: How to override model's constructor correctly in CakePHP



来源:https://stackoverflow.com/questions/9952001/cakephp-logging-to-live-db-during-unit-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!