comparison cake/tests/cases/libs/cake_log.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * CakeLogTest file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases.libs
17 * @since CakePHP(tm) v 1.2.0.5432
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 App::import('Core', 'Log');
21 App::import('Core', 'log/FileLog');
22
23 /**
24 * CakeLogTest class
25 *
26 * @package cake
27 * @subpackage cake.tests.cases.libs
28 */
29 class CakeLogTest extends CakeTestCase {
30
31 /**
32 * Start test callback, clears all streams enabled.
33 *
34 * @return void
35 */
36 function startTest() {
37 $streams = CakeLog::configured();
38 foreach ($streams as $stream) {
39 CakeLog::drop($stream);
40 }
41 }
42
43 /**
44 * test importing loggers from app/libs and plugins.
45 *
46 * @return void
47 */
48 function testImportingLoggers() {
49 App::build(array(
50 'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
51 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
52 ), true);
53
54 $result = CakeLog::config('libtest', array(
55 'engine' => 'TestAppLog'
56 ));
57 $this->assertTrue($result);
58 $this->assertEqual(CakeLog::configured(), array('libtest'));
59
60 $result = CakeLog::config('plugintest', array(
61 'engine' => 'TestPlugin.TestPluginLog'
62 ));
63 $this->assertTrue($result);
64 $this->assertEqual(CakeLog::configured(), array('libtest', 'plugintest'));
65
66 App::build();
67 }
68
69 /**
70 * test all the errors from failed logger imports
71 *
72 * @return void
73 */
74 function testImportingLoggerFailure() {
75 $this->expectError('Missing logger classname');
76 CakeLog::config('fail', array());
77
78 $this->expectError('Could not load logger class born to fail');
79 CakeLog::config('fail', array('engine' => 'born to fail'));
80
81 $this->expectError('logger class stdClass does not implement a write method.');
82 CakeLog::config('fail', array('engine' => 'stdClass'));
83 }
84
85 /**
86 * Test that CakeLog autoconfigures itself to use a FileLogger with the LOGS dir.
87 * When no streams are there.
88 *
89 * @return void
90 */
91 function testAutoConfig() {
92 @unlink(LOGS . 'error.log');
93 CakeLog::write(LOG_WARNING, 'Test warning');
94 $this->assertTrue(file_exists(LOGS . 'error.log'));
95
96 $result = CakeLog::configured();
97 $this->assertEqual($result, array('default'));
98 unlink(LOGS . 'error.log');
99 }
100
101 /**
102 * test configuring log streams
103 *
104 * @return void
105 */
106 function testConfig() {
107 CakeLog::config('file', array(
108 'engine' => 'FileLog',
109 'path' => LOGS
110 ));
111 $result = CakeLog::configured();
112 $this->assertEqual($result, array('file'));
113
114 @unlink(LOGS . 'error.log');
115 CakeLog::write(LOG_WARNING, 'Test warning');
116 $this->assertTrue(file_exists(LOGS . 'error.log'));
117
118 $result = file_get_contents(LOGS . 'error.log');
119 $this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning/', $result);
120 unlink(LOGS . 'error.log');
121 }
122
123 /**
124 * explict tests for drop()
125 *
126 * @return void
127 **/
128 function testDrop() {
129 CakeLog::config('file', array(
130 'engine' => 'FileLog',
131 'path' => LOGS
132 ));
133 $result = CakeLog::configured();
134 $this->assertEqual($result, array('file'));
135
136 CakeLog::drop('file');
137 $result = CakeLog::configured();
138 $this->assertEqual($result, array());
139 }
140
141 /**
142 * testLogFileWriting method
143 *
144 * @access public
145 * @return void
146 */
147 function testLogFileWriting() {
148 @unlink(LOGS . 'error.log');
149 $result = CakeLog::write(LOG_WARNING, 'Test warning');
150 $this->assertTrue($result);
151 $this->assertTrue(file_exists(LOGS . 'error.log'));
152 unlink(LOGS . 'error.log');
153
154 CakeLog::write(LOG_WARNING, 'Test warning 1');
155 CakeLog::write(LOG_WARNING, 'Test warning 2');
156 $result = file_get_contents(LOGS . 'error.log');
157 $this->assertPattern('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1/', $result);
158 $this->assertPattern('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
159 unlink(LOGS . 'error.log');
160 }
161
162 /**
163 * Test logging with the error handler.
164 *
165 * @return void
166 */
167 function testLoggingWithErrorHandling() {
168 @unlink(LOGS . 'debug.log');
169 Configure::write('log', E_ALL & ~E_DEPRECATED);
170 Configure::write('debug', 0);
171
172 set_error_handler(array('CakeLog', 'handleError'));
173 $out .= '';
174
175 $result = file(LOGS . 'debug.log');
176 $this->assertEqual(count($result), 1);
177 $this->assertPattern(
178 '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Notice: Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
179 $result[0]
180 );
181 @unlink(LOGS . 'debug.log');
182 }
183 }