Skip to content

Commit

Permalink
Adding tests for importing loggers from app/libs and plugin/libs.
Browse files Browse the repository at this point in the history
Adding tests for failed configs.
  • Loading branch information
markstory committed Nov 6, 2009
1 parent 54c772c commit 84c8cc3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
17 changes: 12 additions & 5 deletions cake/libs/cake_log.php
Expand Up @@ -84,20 +84,21 @@ function &getInstance() {
*
* @param string $key The keyname for this logger, used to revmoe the logger later.
* @param array $config Array of configuration information for the logger
* @return void
* @return boolean success of configuration.
**/
function config($key, $config) {
if (empty($config['engine'])) {
trigger_error(__('Missing logger classname', true), E_USER_WARNING);
return false;
}
$className = CakeLog::_getLogger($config['engine']);
$self = CakeLog::getInstance();
$className = $self->_getLogger($config['engine']);
if (!$className) {
return false;
}
unset($config['engine']);
$self = CakeLog::getInstance();
$self->_streams[$key] = new $className($config);
return true;
}

/**
Expand All @@ -111,7 +112,13 @@ function _getLogger($loggerName) {
if (strpos($loggerName, '.') !== false) {
list($plugin, $loggerName) = explode('.', $loggerName);
}

if ($plugin) {
App::import('Lib', $plugin . '.log/' . $loggerName);
} else {
if (!App::import('Lib', 'log/' . $loggerName)) {
App::import('Core', 'log/' . $loggerName);
}
}
if (!class_exists($loggerName)) {
trigger_error(sprintf(__('Could not load logger class %s', true), $loggerName), E_USER_WARNING);
return false;
Expand Down Expand Up @@ -171,7 +178,7 @@ function addStream($key, $config) {
**/
function _autoConfig() {
if (!class_exists('FileLog')) {
require LIBS . 'log' . DS . 'file_log.php';
App::import('Core', 'log/FileLog');
}
$this->_streams['default'] = new FileLog(array('path' => LOGS));
}
Expand Down
44 changes: 43 additions & 1 deletion cake/tests/cases/libs/cake_log.test.php
Expand Up @@ -21,7 +21,7 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', 'Log');
require_once LIBS . 'log' . DS . 'file_log.php';
App::import('Core', 'log/FileLog');

/**
* CakeLogTest class
Expand All @@ -43,6 +43,48 @@ function startTest() {
}
}

/**
* test importing loggers from app/libs and plugins.
*
* @return void
**/
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);
$this->assertEqual(CakeLog::streams(), array('libtest'));

$result = CakeLog::config('plugintest', array(
'engine' => 'TestPlugin.TestPluginLog'
));
$this->assertTrue($result);
$this->assertEqual(CakeLog::streams(), array('libtest', 'plugintest'));

App::build();
}

/**
* test all the errors from failed logger imports
*
* @return void
**/
function testImportingLoggerFailure() {
$this->expectError('Missing logger classname');
CakeLog::config('fail', array());

$this->expectError('Could not load logger class born to fail');
CakeLog::config('fail', array('engine' => 'born to fail'));

$this->expectError('logger class stdClass does not implement a write method.');
CakeLog::config('fail', array('engine' => 'stdClass'));
}

/**
* Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
* When no streams are there.
Expand Down
25 changes: 25 additions & 0 deletions cake/tests/test_app/libs/log/test_app_log.php
@@ -0,0 +1,25 @@
<?php
/**
* Test Suite Test App Logging stream class.
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs
* @since CakePHP(tm) v 1.3
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
class TestAppLog {

function write($type, $message) {

}
}
@@ -0,0 +1,25 @@
<?php
/**
* Test Suite Test Plugin Logging stream class.
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs
* @since CakePHP(tm) v 1.3
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
class TestPluginLog {

function write($type, $message) {

}
}

0 comments on commit 84c8cc3

Please sign in to comment.