Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
15 | /** |
||
16 | * @covers Jasny\ErrorHandler |
||
17 | */ |
||
18 | class ErrorHandlerTest extends \PHPUnit_Framework_TestCase |
||
19 | { |
||
20 | /** |
||
21 | * @var ErrorHandler|MockObject |
||
22 | */ |
||
23 | protected $errorHandler; |
||
24 | |||
25 | public function setUp() |
||
26 | { |
||
27 | $this->errorHandler = $this->getMockBuilder(ErrorHandler::class) |
||
28 | ->setMethods(['errorReporting', 'errorGetLast', 'setErrorHandler', 'registerShutdownFunction', |
||
29 | 'clearOutputBuffer']) |
||
30 | ->getMock(); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Test invoke with invalid 'next' param |
||
35 | * |
||
36 | * @expectedException \InvalidArgumentException |
||
37 | */ |
||
38 | public function testInvokeInvalidNext() |
||
39 | { |
||
40 | $request = $this->createMock(ServerRequestInterface::class); |
||
41 | $response = $this->createMock(ResponseInterface::class); |
||
42 | |||
43 | $errorHandler = $this->errorHandler; |
||
44 | |||
45 | $errorHandler($request, $response, 'not callable'); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Test case when there is no error |
||
50 | */ |
||
51 | public function testInvokeNoError() |
||
52 | { |
||
53 | $request = $this->createMock(ServerRequestInterface::class); |
||
54 | $response = $this->createMock(ResponseInterface::class); |
||
55 | $finalResponse = $this->createMock(ResponseInterface::class); |
||
56 | |||
57 | $next = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock(); |
||
58 | $next->expects($this->once())->method('__invoke') |
||
59 | ->with($request, $response) |
||
60 | ->willReturn($finalResponse); |
||
61 | |||
62 | $errorHandler = $this->errorHandler; |
||
63 | |||
64 | $result = $errorHandler($request, $response, $next); |
||
65 | |||
66 | $this->assertSame($finalResponse, $result); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Test that Exception in 'next' callback is caught |
||
71 | */ |
||
72 | public function testInvokeCatchException() |
||
73 | { |
||
74 | $request = $this->createMock(ServerRequestInterface::class); |
||
75 | $response = $this->createMock(ResponseInterface::class); |
||
76 | $errorResponse = $this->createMock(ResponseInterface::class); |
||
77 | $stream = $this->createMock(StreamInterface::class); |
||
78 | |||
79 | $exception = $this->createMock(\Exception::class); |
||
80 | |||
81 | $stream->expects($this->once())->method('write')->with('Unexpected error'); |
||
82 | $response->expects($this->once())->method('withStatus')->with(500)->willReturn($errorResponse); |
||
83 | |||
84 | $errorResponse->expects($this->once())->method('getBody')->willReturn($stream); |
||
85 | |||
86 | $next = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock(); |
||
87 | $next->expects($this->once())->method('__invoke') |
||
88 | ->with($request, $response) |
||
89 | ->willThrowException($exception); |
||
90 | |||
91 | $errorHandler = $this->errorHandler; |
||
92 | |||
93 | $result = $errorHandler($request, $response, $next); |
||
94 | |||
95 | $this->assertSame($errorResponse, $result); |
||
96 | $this->assertSame($exception, $errorHandler->getError()); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Test that an error in 'next' callback is caught |
||
101 | */ |
||
102 | public function testInvokeCatchError() |
||
103 | { |
||
104 | if (!class_exists('Error')) { |
||
105 | $this->markTestSkipped(PHP_VERSION . " doesn't throw errors yet"); |
||
106 | } |
||
107 | |||
108 | $request = $this->createMock(ServerRequestInterface::class); |
||
109 | $response = $this->createMock(ResponseInterface::class); |
||
110 | $errorResponse = $this->createMock(ResponseInterface::class); |
||
111 | $stream = $this->createMock(StreamInterface::class); |
||
112 | |||
113 | $stream->expects($this->once())->method('write')->with('Unexpected error'); |
||
114 | $response->expects($this->once())->method('withStatus')->with(500)->willReturn($errorResponse); |
||
115 | |||
116 | $errorResponse->expects($this->once())->method('getBody')->willReturn($stream); |
||
117 | |||
118 | $next = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock(); |
||
119 | $next->expects($this->once())->method('__invoke') |
||
120 | ->with($request, $response) |
||
121 | ->willReturnCallback(function() { |
||
122 | \this_function_does_not_exist(); |
||
123 | }); |
||
124 | |||
125 | $errorHandler = $this->errorHandler; |
||
126 | |||
127 | $result = $errorHandler($request, $response, $next); |
||
128 | |||
129 | $this->assertSame($errorResponse, $result); |
||
130 | |||
131 | $error = $errorHandler->getError(); |
||
132 | $this->assertEquals("Call to undefined function this_function_does_not_exist()", $error->getMessage()); |
||
133 | } |
||
134 | |||
135 | |||
136 | public function testSetLogger() |
||
137 | { |
||
138 | $logger = $this->createMock(LoggerInterface::class); |
||
139 | |||
140 | $errorHandler = $this->errorHandler; |
||
141 | $errorHandler->setLogger($logger); |
||
142 | |||
143 | $this->assertSame($logger, $errorHandler->getLogger()); |
||
144 | } |
||
145 | |||
146 | |||
147 | public function testInvokeLog() |
||
148 | { |
||
149 | $request = $this->createMock(ServerRequestInterface::class); |
||
150 | $response = $this->createMock(ResponseInterface::class); |
||
151 | $stream = $this->createMock(StreamInterface::class); |
||
152 | |||
153 | $response->method('withStatus')->willReturnSelf(); |
||
154 | $response->method('getBody')->willReturn($stream); |
||
155 | |||
156 | $exception = $this->createMock(\Exception::class); |
||
157 | |||
158 | $message = $this->stringStartsWith('Uncaught Exception ' . get_class($exception)); |
||
159 | $context = ['exception' => $exception]; |
||
160 | |||
161 | $logger = $this->createMock(LoggerInterface::class); |
||
162 | $logger->expects($this->once())->method('log') |
||
163 | ->with(LogLevel::ERROR, $message, $context); |
||
164 | |||
165 | $errorHandler = $this->errorHandler; |
||
166 | $errorHandler->setLogger($logger); |
||
167 | |||
168 | $next = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock(); |
||
169 | $next->expects($this->once())->method('__invoke') |
||
170 | ->with($request, $response) |
||
171 | ->willThrowException($exception); |
||
172 | |||
173 | $errorHandler($request, $response, $next); |
||
174 | } |
||
175 | |||
176 | public function errorProvider() |
||
177 | { |
||
178 | return [ |
||
179 | [E_ERROR, LogLevel::ERROR, 'Fatal error'], |
||
180 | [E_USER_ERROR, LogLevel::ERROR, 'Fatal error'], |
||
181 | [E_RECOVERABLE_ERROR, LogLevel::ERROR, 'Fatal error'], |
||
182 | [E_WARNING, LogLevel::WARNING, 'Warning'], |
||
183 | [E_USER_WARNING, LogLevel::WARNING, 'Warning'], |
||
184 | [E_PARSE, LogLevel::CRITICAL, 'Parse error'], |
||
185 | [E_NOTICE, LogLevel::NOTICE, 'Notice'], |
||
186 | [E_USER_NOTICE, LogLevel::NOTICE, 'Notice'], |
||
187 | [E_CORE_ERROR, LogLevel::CRITICAL, 'Core error'], |
||
188 | [E_CORE_WARNING, LogLevel::WARNING, 'Core warning'], |
||
189 | [E_COMPILE_ERROR, LogLevel::CRITICAL, 'Compile error'], |
||
190 | [E_COMPILE_WARNING, LogLevel::WARNING, 'Compile warning'], |
||
191 | [E_STRICT, LogLevel::INFO, 'Strict standards'], |
||
192 | [E_DEPRECATED, LogLevel::INFO, 'Deprecated'], |
||
193 | [E_USER_DEPRECATED, LogLevel::INFO, 'Deprecated'], |
||
194 | [99999999, LogLevel::ERROR, 'Unknown error'] |
||
195 | ]; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @dataProvider errorProvider |
||
200 | * |
||
201 | * @param int $code |
||
202 | * @param string $level |
||
203 | * @param string $type |
||
204 | */ |
||
205 | public function testLogError($code, $level, $type) |
||
206 | { |
||
207 | $error = new \ErrorException("no good", 0, $code, "foo.php", 42); |
||
208 | $context = ['error' => $error, 'code' => $code, 'message' => "no good", 'file' => 'foo.php', 'line' => 42]; |
||
209 | |||
210 | $logger = $this->createMock(LoggerInterface::class); |
||
211 | $logger->expects($this->once())->method('log') |
||
212 | ->with($level, "$type: no good at foo.php line 42", $context); |
||
213 | |||
214 | $errorHandler = $this->errorHandler; |
||
215 | $errorHandler->setLogger($logger); |
||
216 | |||
217 | $errorHandler->log($error); |
||
218 | } |
||
219 | |||
220 | public function testLogException() |
||
221 | { |
||
222 | $exception = $this->createMock(\Exception::class); |
||
223 | |||
224 | $message = $this->stringStartsWith('Uncaught Exception ' . get_class($exception)); |
||
225 | $context = ['exception' => $exception]; |
||
226 | |||
227 | $logger = $this->createMock(LoggerInterface::class); |
||
228 | $logger->expects($this->once())->method('log') |
||
229 | ->with(LogLevel::ERROR, $message, $context); |
||
230 | |||
231 | $errorHandler = $this->errorHandler; |
||
232 | $errorHandler->setLogger($logger); |
||
233 | |||
234 | $errorHandler->log($exception); |
||
235 | } |
||
236 | |||
237 | public function testLogString() |
||
238 | { |
||
239 | $logger = $this->createMock(LoggerInterface::class); |
||
240 | $logger->expects($this->once())->method('log')->with(LogLevel::WARNING, "Unable to log a string"); |
||
241 | |||
242 | $errorHandler = $this->errorHandler; |
||
243 | $errorHandler->setLogger($logger); |
||
534 |