Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/error.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 * ErrorHandlerTest 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 if (class_exists('TestErrorHandler')) { | |
21 return; | |
22 } | |
23 if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { | |
24 define('CAKEPHP_UNIT_TEST_EXECUTION', 1); | |
25 } | |
26 | |
27 /** | |
28 * BlueberryComponent class | |
29 * | |
30 * @package cake | |
31 * @subpackage cake.tests.cases.libs | |
32 */ | |
33 class BlueberryComponent extends Object { | |
34 | |
35 /** | |
36 * testName property | |
37 * | |
38 * @access public | |
39 * @return void | |
40 */ | |
41 var $testName = null; | |
42 | |
43 /** | |
44 * initialize method | |
45 * | |
46 * @access public | |
47 * @return void | |
48 */ | |
49 function initialize(&$controller) { | |
50 $this->testName = 'BlueberryComponent'; | |
51 } | |
52 } | |
53 | |
54 /** | |
55 * BlueberryDispatcher class | |
56 * | |
57 * @package cake | |
58 * @subpackage cake.tests.cases.libs | |
59 */ | |
60 class BlueberryDispatcher extends Dispatcher { | |
61 | |
62 /** | |
63 * cakeError method | |
64 * | |
65 * @access public | |
66 * @return void | |
67 */ | |
68 function cakeError($method, $messages = array()) { | |
69 $error = new TestErrorHandler($method, $messages); | |
70 return $error; | |
71 } | |
72 } | |
73 | |
74 /** | |
75 * Short description for class. | |
76 * | |
77 * @package cake | |
78 * @subpackage cake.tests.cases.libs | |
79 */ | |
80 class AuthBlueberryUser extends CakeTestModel { | |
81 | |
82 /** | |
83 * name property | |
84 * | |
85 * @var string 'AuthBlueberryUser' | |
86 * @access public | |
87 */ | |
88 var $name = 'AuthBlueberryUser'; | |
89 | |
90 /** | |
91 * useTable property | |
92 * | |
93 * @var string | |
94 * @access public | |
95 */ | |
96 var $useTable = false; | |
97 } | |
98 if (!class_exists('AppController')) { | |
99 /** | |
100 * AppController class | |
101 * | |
102 * @package cake | |
103 * @subpackage cake.tests.cases.libs | |
104 */ | |
105 class AppController extends Controller { | |
106 /** | |
107 * components property | |
108 * | |
109 * @access public | |
110 * @return void | |
111 */ | |
112 var $components = array('Blueberry'); | |
113 /** | |
114 * beforeRender method | |
115 * | |
116 * @access public | |
117 * @return void | |
118 */ | |
119 function beforeRender() { | |
120 echo $this->Blueberry->testName; | |
121 } | |
122 /** | |
123 * header method | |
124 * | |
125 * @access public | |
126 * @return void | |
127 */ | |
128 function header($header) { | |
129 echo $header; | |
130 } | |
131 /** | |
132 * _stop method | |
133 * | |
134 * @access public | |
135 * @return void | |
136 */ | |
137 function _stop($status = 0) { | |
138 echo 'Stopped with status: ' . $status; | |
139 } | |
140 } | |
141 } elseif (!defined('APP_CONTROLLER_EXISTS')){ | |
142 define('APP_CONTROLLER_EXISTS', true); | |
143 } | |
144 App::import('Core', array('Error', 'Controller')); | |
145 | |
146 /** | |
147 * TestErrorController class | |
148 * | |
149 * @package cake | |
150 * @subpackage cake.tests.cases.libs | |
151 */ | |
152 class TestErrorController extends AppController { | |
153 | |
154 /** | |
155 * uses property | |
156 * | |
157 * @var array | |
158 * @access public | |
159 */ | |
160 var $uses = array(); | |
161 | |
162 /** | |
163 * index method | |
164 * | |
165 * @access public | |
166 * @return void | |
167 */ | |
168 function index() { | |
169 $this->autoRender = false; | |
170 return 'what up'; | |
171 } | |
172 } | |
173 | |
174 /** | |
175 * BlueberryController class | |
176 * | |
177 * @package cake | |
178 * @subpackage cake.tests.cases.libs | |
179 */ | |
180 class BlueberryController extends AppController { | |
181 | |
182 /** | |
183 * name property | |
184 * | |
185 * @access public | |
186 * @return void | |
187 */ | |
188 var $name = 'BlueberryController'; | |
189 | |
190 /** | |
191 * uses property | |
192 * | |
193 * @access public | |
194 * @return void | |
195 */ | |
196 var $uses = array('AuthBlueberryUser'); | |
197 | |
198 /** | |
199 * components property | |
200 * | |
201 * @access public | |
202 * @return void | |
203 */ | |
204 var $components = array('Auth'); | |
205 } | |
206 | |
207 /** | |
208 * MyCustomErrorHandler class | |
209 * | |
210 * @package cake | |
211 * @subpackage cake.tests.cases.libs | |
212 */ | |
213 class MyCustomErrorHandler extends ErrorHandler { | |
214 | |
215 /** | |
216 * custom error message type. | |
217 * | |
218 * @return void | |
219 */ | |
220 function missingWidgetThing() { | |
221 echo 'widget thing is missing'; | |
222 } | |
223 | |
224 /** | |
225 * stop method | |
226 * | |
227 * @access public | |
228 * @return void | |
229 */ | |
230 function _stop() { | |
231 return; | |
232 } | |
233 } | |
234 | |
235 /** | |
236 * TestErrorHandler class | |
237 * | |
238 * @package cake | |
239 * @subpackage cake.tests.cases.libs | |
240 */ | |
241 class TestErrorHandler extends ErrorHandler { | |
242 | |
243 /** | |
244 * stop method | |
245 * | |
246 * @access public | |
247 * @return void | |
248 */ | |
249 function _stop() { | |
250 return; | |
251 } | |
252 } | |
253 | |
254 /** | |
255 * ErrorHandlerTest class | |
256 * | |
257 * @package cake | |
258 * @subpackage cake.tests.cases.libs | |
259 */ | |
260 class ErrorHandlerTest extends CakeTestCase { | |
261 | |
262 /** | |
263 * skip method | |
264 * | |
265 * @access public | |
266 * @return void | |
267 */ | |
268 function skip() { | |
269 $this->skipIf(PHP_SAPI === 'cli', '%s Cannot be run from console'); | |
270 } | |
271 | |
272 /** | |
273 * test that methods declared in an ErrorHandler subclass are not converted | |
274 * into error404 when debug == 0 | |
275 * | |
276 * @return void | |
277 */ | |
278 function testSubclassMethodsNotBeingConvertedToError() { | |
279 $back = Configure::read('debug'); | |
280 Configure::write('debug', 2); | |
281 ob_start(); | |
282 $ErrorHandler =& new MyCustomErrorHandler('missingWidgetThing', array('message' => 'doh!')); | |
283 $result = ob_get_clean(); | |
284 $this->assertEqual($result, 'widget thing is missing'); | |
285 | |
286 Configure::write('debug', 0); | |
287 ob_start(); | |
288 $ErrorHandler =& new MyCustomErrorHandler('missingWidgetThing', array('message' => 'doh!')); | |
289 $result = ob_get_clean(); | |
290 $this->assertEqual($result, 'widget thing is missing', 'Method declared in subclass converted to error404. %s'); | |
291 | |
292 Configure::write('debug', 0); | |
293 ob_start(); | |
294 $ErrorHandler =& new MyCustomErrorHandler('missingController', array( | |
295 'className' => 'Missing', 'message' => 'Page not found' | |
296 )); | |
297 $result = ob_get_clean(); | |
298 $this->assertPattern('/Not Found/', $result, 'Method declared in error handler not converted to error404. %s'); | |
299 | |
300 Configure::write('debug', $back); | |
301 } | |
302 | |
303 /** | |
304 * testError method | |
305 * | |
306 * @access public | |
307 * @return void | |
308 */ | |
309 function testError() { | |
310 ob_start(); | |
311 $TestErrorHandler = new TestErrorHandler('error404', array('message' => 'Page not found')); | |
312 ob_clean(); | |
313 ob_start(); | |
314 $TestErrorHandler->error(array( | |
315 'code' => 404, | |
316 'message' => 'Page not Found', | |
317 'name' => "Couldn't find what you were looking for" | |
318 )); | |
319 $result = ob_get_clean(); | |
320 $this->assertPattern("/<h2>Couldn't find what you were looking for<\/h2>/", $result); | |
321 $this->assertPattern('/Page not Found/', $result); | |
322 } | |
323 | |
324 /** | |
325 * testError404 method | |
326 * | |
327 * @access public | |
328 * @return void | |
329 */ | |
330 function testError404() { | |
331 App::build(array( | |
332 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS) | |
333 ), true); | |
334 | |
335 ob_start(); | |
336 $TestErrorHandler = new TestErrorHandler('error404', array('message' => 'Page not found', 'url' => '/test_error')); | |
337 $result = ob_get_clean(); | |
338 $this->assertPattern('/<h2>Not Found<\/h2>/', $result); | |
339 $this->assertPattern("/<strong>'\/test_error'<\/strong>/", $result); | |
340 | |
341 ob_start(); | |
342 $TestErrorHandler =& new TestErrorHandler('error404', array('message' => 'Page not found')); | |
343 ob_get_clean(); | |
344 ob_start(); | |
345 $TestErrorHandler->error404(array( | |
346 'url' => 'pages/<span id=333>pink</span></id><script>document.body.style.background = t=document.getElementById(333).innerHTML;window.alert(t);</script>', | |
347 'message' => 'Page not found' | |
348 )); | |
349 $result = ob_get_clean(); | |
350 $this->assertNoPattern('#<script>#', $result); | |
351 $this->assertNoPattern('#</script>#', $result); | |
352 | |
353 App::build(); | |
354 } | |
355 | |
356 /** | |
357 * testError500 method | |
358 * | |
359 * @access public | |
360 * @return void | |
361 */ | |
362 function testError500() { | |
363 ob_start(); | |
364 $TestErrorHandler = new TestErrorHandler('error500', array( | |
365 'message' => 'An Internal Error Has Occurred' | |
366 )); | |
367 $result = ob_get_clean(); | |
368 $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result); | |
369 | |
370 ob_start(); | |
371 $TestErrorHandler = new TestErrorHandler('error500', array( | |
372 'message' => 'An Internal Error Has Occurred', | |
373 'code' => '500' | |
374 )); | |
375 $result = ob_get_clean(); | |
376 $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result); | |
377 } | |
378 | |
379 /** | |
380 * testMissingController method | |
381 * | |
382 * @access public | |
383 * @return void | |
384 */ | |
385 function testMissingController() { | |
386 $this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController'); | |
387 | |
388 ob_start(); | |
389 $TestErrorHandler = new TestErrorHandler('missingController', array('className' => 'PostsController')); | |
390 $result = ob_get_clean(); | |
391 $this->assertPattern('/<h2>Missing Controller<\/h2>/', $result); | |
392 $this->assertPattern('/<em>PostsController<\/em>/', $result); | |
393 $this->assertPattern('/BlueberryComponent/', $result); | |
394 } | |
395 | |
396 /** | |
397 * testMissingAction method | |
398 * | |
399 * @access public | |
400 * @return void | |
401 */ | |
402 function testMissingAction() { | |
403 ob_start(); | |
404 $TestErrorHandler = new TestErrorHandler('missingAction', array('className' => 'PostsController', 'action' => 'index')); | |
405 $result = ob_get_clean(); | |
406 $this->assertPattern('/<h2>Missing Method in PostsController<\/h2>/', $result); | |
407 $this->assertPattern('/<em>PostsController::<\/em><em>index\(\)<\/em>/', $result); | |
408 | |
409 ob_start(); | |
410 $dispatcher = new BlueberryDispatcher('/blueberry/inexistent'); | |
411 $result = ob_get_clean(); | |
412 $this->assertPattern('/<h2>Missing Method in BlueberryController<\/h2>/', $result); | |
413 $this->assertPattern('/<em>BlueberryController::<\/em><em>inexistent\(\)<\/em>/', $result); | |
414 $this->assertNoPattern('/Location: (.*)\/users\/login/', $result); | |
415 $this->assertNoPattern('/Stopped with status: 0/', $result); | |
416 } | |
417 | |
418 /** | |
419 * testPrivateAction method | |
420 * | |
421 * @access public | |
422 * @return void | |
423 */ | |
424 function testPrivateAction() { | |
425 ob_start(); | |
426 $TestErrorHandler = new TestErrorHandler('privateAction', array('className' => 'PostsController', 'action' => '_secretSauce')); | |
427 $result = ob_get_clean(); | |
428 $this->assertPattern('/<h2>Private Method in PostsController<\/h2>/', $result); | |
429 $this->assertPattern('/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/', $result); | |
430 } | |
431 | |
432 /** | |
433 * testMissingTable method | |
434 * | |
435 * @access public | |
436 * @return void | |
437 */ | |
438 function testMissingTable() { | |
439 ob_start(); | |
440 $TestErrorHandler = new TestErrorHandler('missingTable', array('className' => 'Article', 'table' => 'articles')); | |
441 $result = ob_get_clean(); | |
442 $this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result); | |
443 $this->assertPattern('/<h2>Missing Database Table<\/h2>/', $result); | |
444 $this->assertPattern('/table <em>articles<\/em> for model <em>Article<\/em>/', $result); | |
445 } | |
446 | |
447 /** | |
448 * testMissingDatabase method | |
449 * | |
450 * @access public | |
451 * @return void | |
452 */ | |
453 function testMissingDatabase() { | |
454 ob_start(); | |
455 $TestErrorHandler = new TestErrorHandler('missingDatabase', array()); | |
456 $result = ob_get_clean(); | |
457 $this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result); | |
458 $this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result); | |
459 $this->assertPattern('/Confirm you have created the file/', $result); | |
460 } | |
461 | |
462 /** | |
463 * testMissingView method | |
464 * | |
465 * @access public | |
466 * @return void | |
467 */ | |
468 function testMissingView() { | |
469 restore_error_handler(); | |
470 ob_start(); | |
471 $TestErrorHandler = new TestErrorHandler('missingView', array('className' => 'Pages', 'action' => 'display', 'file' => 'pages/about.ctp', 'base' => '')); | |
472 $expected = ob_get_clean(); | |
473 set_error_handler('simpleTestErrorHandler'); | |
474 $this->assertPattern("/PagesController::/", $expected); | |
475 $this->assertPattern("/pages\/about.ctp/", $expected); | |
476 } | |
477 | |
478 /** | |
479 * testMissingLayout method | |
480 * | |
481 * @access public | |
482 * @return void | |
483 */ | |
484 function testMissingLayout() { | |
485 restore_error_handler(); | |
486 ob_start(); | |
487 $TestErrorHandler = new TestErrorHandler('missingLayout', array( 'layout' => 'my_layout', 'file' => 'layouts/my_layout.ctp', 'base' => '')); | |
488 $expected = ob_get_clean(); | |
489 set_error_handler('simpleTestErrorHandler'); | |
490 $this->assertPattern("/Missing Layout/", $expected); | |
491 $this->assertPattern("/layouts\/my_layout.ctp/", $expected); | |
492 } | |
493 | |
494 /** | |
495 * testMissingConnection method | |
496 * | |
497 * @access public | |
498 * @return void | |
499 */ | |
500 function testMissingConnection() { | |
501 ob_start(); | |
502 $TestErrorHandler = new TestErrorHandler('missingConnection', array('className' => 'Article')); | |
503 $result = ob_get_clean(); | |
504 $this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result); | |
505 $this->assertPattern('/Article requires a database connection/', $result); | |
506 } | |
507 | |
508 /** | |
509 * testMissingHelperFile method | |
510 * | |
511 * @access public | |
512 * @return void | |
513 */ | |
514 function testMissingHelperFile() { | |
515 ob_start(); | |
516 $TestErrorHandler = new TestErrorHandler('missingHelperFile', array('helper' => 'MyCustom', 'file' => 'my_custom.php')); | |
517 $result = ob_get_clean(); | |
518 $this->assertPattern('/<h2>Missing Helper File<\/h2>/', $result); | |
519 $this->assertPattern('/Create the class below in file:/', $result); | |
520 $this->assertPattern('/(\/|\\\)my_custom.php/', $result); | |
521 } | |
522 | |
523 /** | |
524 * testMissingHelperClass method | |
525 * | |
526 * @access public | |
527 * @return void | |
528 */ | |
529 function testMissingHelperClass() { | |
530 ob_start(); | |
531 $TestErrorHandler = new TestErrorHandler('missingHelperClass', array('helper' => 'MyCustom', 'file' => 'my_custom.php')); | |
532 $result = ob_get_clean(); | |
533 $this->assertPattern('/<h2>Missing Helper Class<\/h2>/', $result); | |
534 $this->assertPattern('/The helper class <em>MyCustomHelper<\/em> can not be found or does not exist./', $result); | |
535 $this->assertPattern('/(\/|\\\)my_custom.php/', $result); | |
536 } | |
537 | |
538 /** | |
539 * test missingBehaviorFile method | |
540 * | |
541 * @access public | |
542 * @return void | |
543 */ | |
544 function testMissingBehaviorFile() { | |
545 ob_start(); | |
546 $TestErrorHandler = new TestErrorHandler('missingBehaviorFile', array('behavior' => 'MyCustom', 'file' => 'my_custom.php')); | |
547 $result = ob_get_clean(); | |
548 $this->assertPattern('/<h2>Missing Behavior File<\/h2>/', $result); | |
549 $this->assertPattern('/Create the class below in file:/', $result); | |
550 $this->assertPattern('/(\/|\\\)my_custom.php/', $result); | |
551 } | |
552 | |
553 /** | |
554 * test MissingBehaviorClass method | |
555 * | |
556 * @access public | |
557 * @return void | |
558 */ | |
559 function testMissingBehaviorClass() { | |
560 ob_start(); | |
561 $TestErrorHandler = new TestErrorHandler('missingBehaviorClass', array('behavior' => 'MyCustom', 'file' => 'my_custom.php')); | |
562 $result = ob_get_clean(); | |
563 $this->assertPattern('/<h2>Missing Behavior Class<\/h2>/', $result); | |
564 $this->assertPattern('/The behavior class <em>MyCustomBehavior<\/em> can not be found or does not exist./', $result); | |
565 $this->assertPattern('/(\/|\\\)my_custom.php/', $result); | |
566 } | |
567 | |
568 /** | |
569 * testMissingComponentFile method | |
570 * | |
571 * @access public | |
572 * @return void | |
573 */ | |
574 function testMissingComponentFile() { | |
575 ob_start(); | |
576 $TestErrorHandler = new TestErrorHandler('missingComponentFile', array('className' => 'PostsController', 'component' => 'Sidebox', 'file' => 'sidebox.php')); | |
577 $result = ob_get_clean(); | |
578 $this->assertPattern('/<h2>Missing Component File<\/h2>/', $result); | |
579 $this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result); | |
580 $this->assertPattern('/(\/|\\\)sidebox.php/', $result); | |
581 } | |
582 | |
583 /** | |
584 * testMissingComponentClass method | |
585 * | |
586 * @access public | |
587 * @return void | |
588 */ | |
589 function testMissingComponentClass() { | |
590 ob_start(); | |
591 $TestErrorHandler = new TestErrorHandler('missingComponentClass', array('className' => 'PostsController', 'component' => 'Sidebox', 'file' => 'sidebox.php')); | |
592 $result = ob_get_clean(); | |
593 $this->assertPattern('/<h2>Missing Component Class<\/h2>/', $result); | |
594 $this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result); | |
595 $this->assertPattern('/(\/|\\\)sidebox.php/', $result); | |
596 } | |
597 | |
598 /** | |
599 * testMissingModel method | |
600 * | |
601 * @access public | |
602 * @return void | |
603 */ | |
604 function testMissingModel() { | |
605 ob_start(); | |
606 $TestErrorHandler = new TestErrorHandler('missingModel', array('className' => 'Article', 'file' => 'article.php')); | |
607 $result = ob_get_clean(); | |
608 $this->assertPattern('/<h2>Missing Model<\/h2>/', $result); | |
609 $this->assertPattern('/<em>Article<\/em> could not be found./', $result); | |
610 $this->assertPattern('/(\/|\\\)article.php/', $result); | |
611 } | |
612 | |
613 /** | |
614 * testing that having a code => 500 in the cakeError call makes an | |
615 * internal server error. | |
616 * | |
617 * @return void | |
618 */ | |
619 function testThatCode500Works() { | |
620 Configure::write('debug', 0); | |
621 ob_start(); | |
622 $TestErrorHandler = new TestErrorHandler('missingTable', array( | |
623 'className' => 'Article', | |
624 'table' => 'articles', | |
625 'code' => 500 | |
626 )); | |
627 $result = ob_get_clean(); | |
628 $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result); | |
629 } | |
630 } |