Permalink
Browse files

Adding tests for importing loggers from app/libs and plugin/libs.

Adding tests for failed configs.
  • Loading branch information...
1 parent 54c772c commit 84c8cc3f366cbd76cb3b6835b8996702526b0f9d @markstory markstory committed Nov 6, 2009
View
@@ -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;
}
/**
@@ -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;
@@ -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));
}
@@ -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
@@ -44,6 +44,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.
*
@@ -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.