Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * ErrorHandlerTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Error
15
 * @since         CakePHP(tm) v 1.2.0.5432
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ErrorHandler', 'Error');
20
App::uses('Controller', 'Controller');
21
App::uses('Router', 'Routing');
22
 
23
/**
24
 * ErrorHandlerTest class
25
 *
26
 * @package       Cake.Test.Case.Error
27
 */
28
class ErrorHandlerTest extends CakeTestCase {
29
 
30
	protected $_restoreError = false;
31
 
32
/**
33
 * setup create a request object to get out of router later.
34
 *
35
 * @return void
36
 */
37
	public function setUp() {
38
		parent::setUp();
39
		App::build(array(
40
			'View' => array(
41
				CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
42
			)
43
		), App::RESET);
44
		Router::reload();
45
 
46
		$request = new CakeRequest(null, false);
47
		$request->base = '';
48
		Router::setRequestInfo($request);
49
		Configure::write('debug', 2);
50
 
51
		CakeLog::disable('stdout');
52
		CakeLog::disable('stderr');
53
	}
54
 
55
/**
56
 * tearDown
57
 *
58
 * @return void
59
 */
60
	public function tearDown() {
61
		parent::tearDown();
62
		if ($this->_restoreError) {
63
			restore_error_handler();
64
		}
65
		CakeLog::enable('stdout');
66
		CakeLog::enable('stderr');
67
	}
68
 
69
/**
70
 * test error handling when debug is on, an error should be printed from Debugger.
71
 *
72
 * @return void
73
 */
74
	public function testHandleErrorDebugOn() {
75
		set_error_handler('ErrorHandler::handleError');
76
		$this->_restoreError = true;
77
 
78
		ob_start();
79
		$wrong .= '';
80
		$result = ob_get_clean();
81
 
82
		$this->assertRegExp('/<pre class="cake-error">/', $result);
83
		$this->assertRegExp('/<b>Notice<\/b>/', $result);
84
		$this->assertRegExp('/variable:\s+wrong/', $result);
85
	}
86
 
87
/**
88
 * provides errors for mapping tests.
89
 *
90
 * @return void
91
 */
92
	public static function errorProvider() {
93
		return array(
94
			array(E_USER_NOTICE, 'Notice'),
95
			array(E_USER_WARNING, 'Warning'),
96
		);
97
	}
98
 
99
/**
100
 * test error mappings
101
 *
102
 * @dataProvider errorProvider
103
 * @return void
104
 */
105
	public function testErrorMapping($error, $expected) {
106
		set_error_handler('ErrorHandler::handleError');
107
		$this->_restoreError = true;
108
 
109
		ob_start();
110
		trigger_error('Test error', $error);
111
 
112
		$result = ob_get_clean();
113
		$this->assertRegExp('/<b>' . $expected . '<\/b>/', $result);
114
	}
115
 
116
/**
117
 * test error prepended by @
118
 *
119
 * @return void
120
 */
121
	public function testErrorSuppressed() {
122
		set_error_handler('ErrorHandler::handleError');
123
		$this->_restoreError = true;
124
 
125
		ob_start();
126
		//@codingStandardsIgnoreStart
127
		@include 'invalid.file';
128
		//@codingStandardsIgnoreEnd
129
		$result = ob_get_clean();
130
		$this->assertTrue(empty($result));
131
	}
132
 
133
/**
134
 * Test that errors go into CakeLog when debug = 0.
135
 *
136
 * @return void
137
 */
138
	public function testHandleErrorDebugOff() {
139
		Configure::write('debug', 0);
140
		Configure::write('Error.trace', false);
141
		if (file_exists(LOGS . 'debug.log')) {
142
			unlink(LOGS . 'debug.log');
143
		}
144
 
145
		set_error_handler('ErrorHandler::handleError');
146
		$this->_restoreError = true;
147
 
148
		$out .= '';
149
 
150
		$result = file(LOGS . 'debug.log');
151
		$this->assertEquals(1, count($result));
152
		$this->assertRegExp(
153
			'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
154
			$result[0]
155
		);
156
		if (file_exists(LOGS . 'debug.log')) {
157
			unlink(LOGS . 'debug.log');
158
		}
159
	}
160
 
161
/**
162
 * Test that errors going into CakeLog include traces.
163
 *
164
 * @return void
165
 */
166
	public function testHandleErrorLoggingTrace() {
167
		Configure::write('debug', 0);
168
		Configure::write('Error.trace', true);
169
		if (file_exists(LOGS . 'debug.log')) {
170
			unlink(LOGS . 'debug.log');
171
		}
172
 
173
		set_error_handler('ErrorHandler::handleError');
174
		$this->_restoreError = true;
175
 
176
		$out .= '';
177
 
178
		$result = file(LOGS . 'debug.log');
179
		$this->assertRegExp(
180
			'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
181
			$result[0]
182
		);
183
		$this->assertRegExp('/^Trace:/', $result[1]);
184
		$this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
185
		if (file_exists(LOGS . 'debug.log')) {
186
			unlink(LOGS . 'debug.log');
187
		}
188
	}
189
 
190
/**
191
 * test handleException generating a page.
192
 *
193
 * @return void
194
 */
195
	public function testHandleException() {
196
		$error = new NotFoundException('Kaboom!');
197
		ob_start();
198
		ErrorHandler::handleException($error);
199
		$result = ob_get_clean();
200
		$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
201
	}
202
 
203
/**
204
 * test handleException generating log.
205
 *
206
 * @return void
207
 */
208
	public function testHandleExceptionLog() {
209
		if (file_exists(LOGS . 'error.log')) {
210
			unlink(LOGS . 'error.log');
211
		}
212
		Configure::write('Exception.log', true);
213
		$error = new NotFoundException('Kaboom!');
214
 
215
		ob_start();
216
		ErrorHandler::handleException($error);
217
		$result = ob_get_clean();
218
		$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
219
 
220
		$log = file(LOGS . 'error.log');
221
		$this->assertContains('[NotFoundException] Kaboom!', $log[0], 'message missing.');
222
		$this->assertContains('ErrorHandlerTest->testHandleExceptionLog', $log[2], 'Stack trace missing.');
223
	}
224
 
225
/**
226
 * test handleException generating log.
227
 *
228
 * @return void
229
 */
230
	public function testHandleExceptionLogSkipping() {
231
		if (file_exists(LOGS . 'error.log')) {
232
			unlink(LOGS . 'error.log');
233
		}
234
		Configure::write('Exception.log', true);
235
		Configure::write('Exception.skipLog', array('NotFoundException'));
236
		$notFound = new NotFoundException('Kaboom!');
237
		$forbidden = new ForbiddenException('Fooled you!');
238
 
239
		ob_start();
240
		ErrorHandler::handleException($notFound);
241
		$result = ob_get_clean();
242
		$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
243
 
244
		ob_start();
245
		ErrorHandler::handleException($forbidden);
246
		$result = ob_get_clean();
247
		$this->assertRegExp('/Fooled you!/', $result, 'message missing.');
248
 
249
		$log = file(LOGS . 'error.log');
250
		$this->assertNotContains('[NotFoundException] Kaboom!', $log[0], 'message should not be logged.');
251
		$this->assertContains('[ForbiddenException] Fooled you!', $log[0], 'message missing.');
252
	}
253
 
254
/**
255
 * tests it is possible to load a plugin exception renderer
256
 *
257
 * @return void
258
 */
259
	public function testLoadPluginHandler() {
260
		App::build(array(
261
			'Plugin' => array(
262
				CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
263
			)
264
		), App::RESET);
265
		CakePlugin::load('TestPlugin');
266
		Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
267
		$error = new NotFoundException('Kaboom!');
268
		ob_start();
269
		ErrorHandler::handleException($error);
270
		$result = ob_get_clean();
271
		$this->assertEquals('Rendered by test plugin', $result);
272
		CakePlugin::unload();
273
	}
274
 
275
/**
276
 * test handleFatalError generating a page.
277
 *
278
 * These tests start two buffers as handleFatalError blows the outer one up.
279
 *
280
 * @return void
281
 */
282
	public function testHandleFatalErrorPage() {
283
		$line = __LINE__;
284
 
285
		ob_start();
286
		ob_start();
287
		Configure::write('debug', 1);
288
		ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
289
		$result = ob_get_clean();
290
		$this->assertContains('Something wrong', $result, 'message missing.');
291
		$this->assertContains(__FILE__, $result, 'filename missing.');
292
		$this->assertContains((string)$line, $result, 'line missing.');
293
 
294
		ob_start();
295
		ob_start();
296
		Configure::write('debug', 0);
297
		ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
298
		$result = ob_get_clean();
299
		$this->assertNotContains('Something wrong', $result, 'message must not appear.');
300
		$this->assertNotContains(__FILE__, $result, 'filename must not appear.');
301
		$this->assertContains('An Internal Error Has Occurred', $result);
302
	}
303
 
304
/**
305
 * test handleException generating log.
306
 *
307
 * @return void
308
 */
309
	public function testHandleFatalErrorLog() {
310
		if (file_exists(LOGS . 'error.log')) {
311
			unlink(LOGS . 'error.log');
312
		}
313
 
314
		ob_start();
315
		ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
316
		ob_clean();
317
 
318
		$log = file(LOGS . 'error.log');
319
		$this->assertContains(__FILE__, $log[0], 'missing filename');
320
		$this->assertContains('[FatalErrorException] Something wrong', $log[1], 'message missing.');
321
	}
322
 
323
}