2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* CakeLogTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2009-03-19 21:10:13 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
App::import('Core', 'Log');
|
2009-11-06 01:13:15 +00:00
|
|
|
App::import('Core', 'log/FileLog');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-19 21:10:13 +00:00
|
|
|
* CakeLogTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
class CakeLogTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-11-04 23:07:39 +00:00
|
|
|
/**
|
2009-11-06 00:16:46 +00:00
|
|
|
* Start test callback, clears all streams enabled.
|
2009-11-04 23:07:39 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-11-06 00:16:46 +00:00
|
|
|
function startTest() {
|
2009-11-15 01:42:57 +00:00
|
|
|
$streams = CakeLog::configured();
|
2009-11-04 23:07:39 +00:00
|
|
|
foreach ($streams as $stream) {
|
2009-11-15 01:42:57 +00:00
|
|
|
CakeLog::drop($stream);
|
2009-11-04 23:07:39 +00:00
|
|
|
}
|
2009-11-06 00:16:46 +00:00
|
|
|
}
|
2009-11-04 23:07:39 +00:00
|
|
|
|
2009-11-06 01:13:15 +00:00
|
|
|
/**
|
|
|
|
* test importing loggers from app/libs and plugins.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-11-06 01:13:15 +00:00
|
|
|
function testImportingLoggers() {
|
|
|
|
App::build(array(
|
|
|
|
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
|
|
|
|
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
|
|
|
|
), true);
|
|
|
|
|
|
|
|
$result = CakeLog::config('libtest', array(
|
|
|
|
'engine' => 'TestAppLog'
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
2009-11-15 01:42:57 +00:00
|
|
|
$this->assertEqual(CakeLog::configured(), array('libtest'));
|
2009-11-06 01:13:15 +00:00
|
|
|
|
|
|
|
$result = CakeLog::config('plugintest', array(
|
|
|
|
'engine' => 'TestPlugin.TestPluginLog'
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
2009-11-15 01:42:57 +00:00
|
|
|
$this->assertEqual(CakeLog::configured(), array('libtest', 'plugintest'));
|
2009-11-06 01:13:15 +00:00
|
|
|
|
|
|
|
App::build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test all the errors from failed logger imports
|
|
|
|
*
|
2010-05-08 20:17:20 +00:00
|
|
|
* @expectedException Exception
|
2009-11-06 01:13:15 +00:00
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-11-06 01:13:15 +00:00
|
|
|
function testImportingLoggerFailure() {
|
|
|
|
CakeLog::config('fail', array());
|
2010-04-24 02:31:21 +00:00
|
|
|
}
|
2009-11-06 01:13:15 +00:00
|
|
|
|
2010-04-24 02:31:21 +00:00
|
|
|
/**
|
|
|
|
* test that loggers have to implement the correct interface.
|
|
|
|
*
|
2010-05-08 20:17:20 +00:00
|
|
|
* @expectedException Exception
|
2010-04-24 02:31:21 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testNotImplementingInterface() {
|
2009-11-06 01:13:15 +00:00
|
|
|
CakeLog::config('fail', array('engine' => 'stdClass'));
|
|
|
|
}
|
|
|
|
|
2009-11-06 00:16:46 +00:00
|
|
|
/**
|
|
|
|
* Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
|
|
|
|
* When no streams are there.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-11-06 00:16:46 +00:00
|
|
|
function testAutoConfig() {
|
2009-11-04 23:07:39 +00:00
|
|
|
@unlink(LOGS . 'error.log');
|
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
|
2009-11-15 01:42:57 +00:00
|
|
|
$result = CakeLog::configured();
|
2009-11-04 23:07:39 +00:00
|
|
|
$this->assertEqual($result, array('default'));
|
2009-11-06 00:16:46 +00:00
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test configuring log streams
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-11-06 00:16:46 +00:00
|
|
|
function testConfig() {
|
|
|
|
CakeLog::config('file', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'path' => LOGS
|
|
|
|
));
|
2009-11-15 01:42:57 +00:00
|
|
|
$result = CakeLog::configured();
|
2009-11-06 00:16:46 +00:00
|
|
|
$this->assertEqual($result, array('file'));
|
|
|
|
|
|
|
|
@unlink(LOGS . 'error.log');
|
|
|
|
CakeLog::write(LOG_WARNING, 'Test warning');
|
|
|
|
$this->assertTrue(file_exists(LOGS . 'error.log'));
|
|
|
|
|
|
|
|
$result = file_get_contents(LOGS . 'error.log');
|
|
|
|
$this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
|
|
|
|
unlink(LOGS . 'error.log');
|
2009-11-04 23:07:39 +00:00
|
|
|
}
|
|
|
|
|
2009-11-15 01:44:42 +00:00
|
|
|
/**
|
|
|
|
* explict tests for drop()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
function testDrop() {
|
|
|
|
CakeLog::config('file', array(
|
|
|
|
'engine' => 'FileLog',
|
|
|
|
'path' => LOGS
|
|
|
|
));
|
|
|
|
$result = CakeLog::configured();
|
|
|
|
$this->assertEqual($result, array('file'));
|
|
|
|
|
|
|
|
CakeLog::drop('file');
|
|
|
|
$result = CakeLog::configured();
|
|
|
|
$this->assertEqual($result, array());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function testLogFileWriting() {
|
|
|
|
@unlink(LOGS . 'error.log');
|
2009-11-06 04:20:40 +00: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');
|
|
|
|
$this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
|
|
|
|
$this->assertPattern('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
2009-09-08 03:59:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test logging with the error handler.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-09-08 03:59:31 +00:00
|
|
|
function testLoggingWithErrorHandling() {
|
|
|
|
@unlink(LOGS . 'debug.log');
|
|
|
|
Configure::write('log', E_ALL & ~E_DEPRECATED);
|
|
|
|
Configure::write('debug', 0);
|
|
|
|
|
2009-09-10 15:32:21 +00:00
|
|
|
set_error_handler(array('CakeLog', 'handleError'));
|
2009-09-08 03:59:31 +00:00
|
|
|
$out .= '';
|
|
|
|
|
|
|
|
$result = file(LOGS . 'debug.log');
|
|
|
|
$this->assertEqual(count($result), 1);
|
|
|
|
$this->assertPattern(
|
2010-01-15 03:33:14 +00:00
|
|
|
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Notice: Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
|
2009-09-08 03:59:31 +00:00
|
|
|
$result[0]
|
|
|
|
);
|
|
|
|
@unlink(LOGS . 'debug.log');
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|