2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-19 14:10:13 -07:00
|
|
|
* CakeLogTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 12:31:21 -04:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-18 22:15:13 -03:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2011-05-29 17:31:39 -04:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 12:31:21 -04:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-05-29 17:31:39 -04:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-18 22:15:13 -03:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Log
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
2010-10-03 12:27:27 -04:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-09 00:43:11 -04:30
|
|
|
|
|
|
|
App::uses('CakeLog', 'Log');
|
|
|
|
App::uses('FileLog', 'Log/Engine');
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-19 14:10:13 -07:00
|
|
|
* CakeLogTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Log
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
class CakeLogTest extends CakeTestCase {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-11-04 18:07:39 -05:00
|
|
|
/**
|
2009-11-05 19:16:46 -05:00
|
|
|
* Start test callback, clears all streams enabled.
|
2009-11-04 18:07:39 -05:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function setUp() {
|
2010-09-25 21:36:49 -04:00
|
|
|
parent::setUp();
|
2009-11-14 20:42:57 -05:00
|
|
|
$streams = CakeLog::configured();
|
2009-11-04 18:07:39 -05:00
|
|
|
foreach ($streams as $stream) {
|
2009-11-14 20:42:57 -05:00
|
|
|
CakeLog::drop($stream);
|
2009-11-04 18:07:39 -05:00
|
|
|
}
|
2009-11-05 19:16:46 -05:00
|
|
|
}
|
2009-11-04 18:07:39 -05:00
|
|
|
|
2009-11-05 20:13:15 -05:00
|
|
|
/**
|
|
|
|
* test importing loggers from app/libs and plugins.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testImportingLoggers() {
|
2009-11-05 20:13:15 -05:00
|
|
|
App::build(array(
|
2011-04-17 12:35:21 +02:00
|
|
|
'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
|
|
|
|
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2009-11-05 20:13:15 -05:00
|
|
|
), true);
|
2011-05-09 00:31:38 -04:30
|
|
|
CakePlugin::load('TestPlugin');
|
2009-11-05 20:13:15 -05:00
|
|
|
|
|
|
|
$result = CakeLog::config('libtest', array(
|
|
|
|
'engine' => 'TestAppLog'
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals(CakeLog::configured(), array('libtest'));
|
2009-11-05 20:13:15 -05:00
|
|
|
|
|
|
|
$result = CakeLog::config('plugintest', array(
|
|
|
|
'engine' => 'TestPlugin.TestPluginLog'
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals(CakeLog::configured(), array('libtest', 'plugintest'));
|
2009-11-05 20:13:15 -05:00
|
|
|
|
|
|
|
App::build();
|
2011-05-09 00:31:38 -04:30
|
|
|
CakePlugin::unload();
|
2009-11-05 20:13:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test all the errors from failed logger imports
|
|
|
|
*
|
2010-12-11 19:01:07 -05:00
|
|
|
* @expectedException CakeLogException
|
2009-11-05 20:13:15 -05:00
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testImportingLoggerFailure() {
|
2009-11-05 20:13:15 -05:00
|
|
|
CakeLog::config('fail', array());
|
2010-04-23 22:31:21 -04:00
|
|
|
}
|
2009-11-05 20:13:15 -05:00
|
|
|
|
2010-04-23 22:31:21 -04:00
|
|
|
/**
|
|
|
|
* test that loggers have to implement the correct interface.
|
|
|
|
*
|
2010-12-11 19:01:07 -05:00
|
|
|
* @expectedException CakeLogException
|
2010-04-23 22:31:21 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testNotImplementingInterface() {
|
2009-11-05 20:13:15 -05:00
|
|
|
CakeLog::config('fail', array('engine' => 'stdClass'));
|
|
|
|
}
|
|
|
|
|
2009-11-05 19:16:46 -05:00
|
|
|
/**
|
|
|
|
* Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
|
|
|
|
* When no streams are there.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testAutoConfig() {
|
2011-05-29 02:38:22 +05:30
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
2009-11-04 18:07:39 -05:00
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
|
2009-11-14 20:42:57 -05:00
|
|
|
$result = CakeLog::configured();
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($result, array('default'));
|
2009-11-05 19:16:46 -05:00
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test configuring log streams
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testConfig() {
|
2009-11-05 19:16:46 -05:00
|
|
|
CakeLog::config('file', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'path' => LOGS
|
|
|
|
));
|
2009-11-14 20:42:57 -05:00
|
|
|
$result = CakeLog::configured();
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($result, array('file'));
|
2009-11-05 19:16:46 -05:00
|
|
|
|
2010-11-14 21:07:23 -05:00
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
@unlink(LOGS . 'error.log');
|
|
|
|
}
|
2009-11-05 19:16:46 -05:00
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
|
|
|
|
$result = file_get_contents(LOGS . 'error.log');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
|
2009-11-05 19:16:46 -05:00
|
|
|
unlink(LOGS . 'error.log');
|
2009-11-04 18:07:39 -05:00
|
|
|
}
|
|
|
|
|
2009-11-14 20:44:42 -05:00
|
|
|
/**
|
|
|
|
* explict tests for drop()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testDrop() {
|
2009-11-14 20:44:42 -05:00
|
|
|
CakeLog::config('file', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'path' => LOGS
|
|
|
|
));
|
|
|
|
$result = CakeLog::configured();
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($result, array('file'));
|
2009-11-14 20:44:42 -05:00
|
|
|
|
|
|
|
CakeLog::drop('file');
|
|
|
|
$result = CakeLog::configured();
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertEquals($result, array());
|
2009-11-14 20:44:42 -05:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testLogFileWriting method
|
2008-11-08 02:58:37 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testLogFileWriting() {
|
2011-05-29 02:38:22 +05:30
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
2009-11-05 23:20:40 -05:00
|
|
|
$result = CakeLog::write(LOG_WARNING, 'Test warning');
|
|
|
|
$this->assertTrue($result);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
|
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning 1');
|
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning 2');
|
|
|
|
$result = file_get_contents(LOGS . 'error.log');
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
|
|
|
|
$this->assertRegExp('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
2009-09-07 23:59:31 -04:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|