pictcode / lib / Cake / Test / Case / Console / ConsoleErrorHandlerTest.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (4.512 KB)
1 | 635eef61 | spyder1211 | <?php
|
---|---|---|---|
2 | /**
|
||
3 | * ConsoleErrorHandler Test case
|
||
4 | *
|
||
5 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||
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://cakephp.org CakePHP(tm) Project
|
||
14 | * @package Cake.Test.Case.Console
|
||
15 | * @since CakePHP(tm) v 2.0
|
||
16 | * @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||
17 | */
|
||
18 | |||
19 | App::uses('ConsoleErrorHandler', 'Console'); |
||
20 | |||
21 | /**
|
||
22 | * ConsoleErrorHandler Test case.
|
||
23 | *
|
||
24 | * @package Cake.Test.Case.Console
|
||
25 | */
|
||
26 | class ConsoleErrorHandlerTest extends CakeTestCase { |
||
27 | |||
28 | /**
|
||
29 | * setup, create mocks
|
||
30 | *
|
||
31 | * @return Mock object
|
||
32 | */
|
||
33 | public function setUp() { |
||
34 | parent::setUp();
|
||
35 | $this->Error = $this->getMock('ConsoleErrorHandler', array('_stop')); |
||
36 | ConsoleErrorHandler::$stderr = $this->getMock('ConsoleOutput', array(), array(), '', false); |
||
37 | } |
||
38 | |||
39 | /**
|
||
40 | * tearDown
|
||
41 | *
|
||
42 | * @return void
|
||
43 | */
|
||
44 | public function tearDown() { |
||
45 | unset($this->Error); |
||
46 | parent::tearDown();
|
||
47 | } |
||
48 | |||
49 | /**
|
||
50 | * test that the console error handler can deal with CakeExceptions.
|
||
51 | *
|
||
52 | * @return void
|
||
53 | */
|
||
54 | public function testHandleError() { |
||
55 | $content = "<error>Notice Error:</error> This is a notice error in [/some/file, line 275]\n"; |
||
56 | ConsoleErrorHandler::$stderr->expects($this->once())->method('write') |
||
57 | ->with($content);
|
||
58 | |||
59 | $this->Error->handleError(E_NOTICE, 'This is a notice error', '/some/file', 275); |
||
60 | } |
||
61 | |||
62 | /**
|
||
63 | * test that the console error handler can deal with fatal errors.
|
||
64 | *
|
||
65 | * @return void
|
||
66 | */
|
||
67 | public function testHandleFatalError() { |
||
68 | $content = "<error>Fatal Error Error:</error> This is a fatal error in [/some/file, line 275]\n"; |
||
69 | ConsoleErrorHandler::$stderr->expects($this->once())->method('write') |
||
70 | ->with($content);
|
||
71 | |||
72 | $this->Error->expects($this->once()) |
||
73 | ->method('_stop')
|
||
74 | ->with(1);
|
||
75 | |||
76 | $this->Error->handleError(E_USER_ERROR, 'This is a fatal error', '/some/file', 275); |
||
77 | } |
||
78 | |||
79 | /**
|
||
80 | * test that the console error handler can deal with CakeExceptions.
|
||
81 | *
|
||
82 | * @return void
|
||
83 | */
|
||
84 | public function testCakeErrors() { |
||
85 | $exception = new MissingActionException('Missing action'); |
||
86 | ConsoleErrorHandler::$stderr->expects($this->once())->method('write') |
||
87 | ->with($this->stringContains('Missing action')); |
||
88 | |||
89 | $this->Error->expects($this->once()) |
||
90 | ->method('_stop')
|
||
91 | ->with(404);
|
||
92 | |||
93 | $this->Error->handleException($exception); |
||
94 | } |
||
95 | |||
96 | /**
|
||
97 | * test a non CakeException exception.
|
||
98 | *
|
||
99 | * @return void
|
||
100 | */
|
||
101 | public function testNonCakeExceptions() { |
||
102 | $exception = new InvalidArgumentException('Too many parameters.'); |
||
103 | |||
104 | ConsoleErrorHandler::$stderr->expects($this->once())->method('write') |
||
105 | ->with($this->stringContains('Too many parameters.')); |
||
106 | |||
107 | $this->Error->expects($this->once()) |
||
108 | ->method('_stop')
|
||
109 | ->with(1);
|
||
110 | |||
111 | $this->Error->handleException($exception); |
||
112 | } |
||
113 | |||
114 | /**
|
||
115 | * test a Error404 exception.
|
||
116 | *
|
||
117 | * @return void
|
||
118 | */
|
||
119 | public function testError404Exception() { |
||
120 | $exception = new NotFoundException('dont use me in cli.'); |
||
121 | |||
122 | ConsoleErrorHandler::$stderr->expects($this->once())->method('write') |
||
123 | ->with($this->stringContains('dont use me in cli.')); |
||
124 | |||
125 | $this->Error->expects($this->once()) |
||
126 | ->method('_stop')
|
||
127 | ->with(404);
|
||
128 | |||
129 | $this->Error->handleException($exception); |
||
130 | } |
||
131 | |||
132 | /**
|
||
133 | * test a Error500 exception.
|
||
134 | *
|
||
135 | * @return void
|
||
136 | */
|
||
137 | public function testError500Exception() { |
||
138 | $exception = new InternalErrorException('dont use me in cli.'); |
||
139 | |||
140 | ConsoleErrorHandler::$stderr->expects($this->once())->method('write') |
||
141 | ->with($this->stringContains('dont use me in cli.')); |
||
142 | |||
143 | $this->Error->expects($this->once()) |
||
144 | ->method('_stop')
|
||
145 | ->with(500);
|
||
146 | |||
147 | $this->Error->handleException($exception); |
||
148 | } |
||
149 | |||
150 | /**
|
||
151 | * test a exception with non-integer code
|
||
152 | *
|
||
153 | * @return void
|
||
154 | */
|
||
155 | public function testNonIntegerExceptionCode() { |
||
156 | if (PHP_VERSION_ID < 50300) { |
||
157 | $this->markTestSkipped('ReflectionProperty::setAccessible() is available since 5.3'); |
||
158 | } |
||
159 | |||
160 | $exception = new Exception('Non-integer exception code'); |
||
161 | |||
162 | $class = new ReflectionClass('Exception'); |
||
163 | $property = $class->getProperty('code'); |
||
164 | $property->setAccessible(true); |
||
165 | $property->setValue($exception, '42S22'); |
||
166 | |||
167 | ConsoleErrorHandler::$stderr->expects($this->once())->method('write') |
||
168 | ->with($this->stringContains('Non-integer exception code')); |
||
169 | |||
170 | $this->Error->expects($this->once()) |
||
171 | ->method('_stop')
|
||
172 | ->with(1);
|
||
173 | |||
174 | $this->Error->handleException($exception); |
||
175 | } |
||
176 | |||
177 | } |