|
2 | 2 |
|
3 | 3 | namespace ErrorHeroModule\Spec\Listener; |
4 | 4 |
|
| 5 | +use Closure; |
5 | 6 | use ErrorException; |
6 | 7 | use ErrorHeroModule\Handler\Logging; |
7 | 8 | use ErrorHeroModule\Listener\Mvc; |
|
249 | 250 |
|
250 | 251 | }); |
251 | 252 |
|
| 253 | + describe('->phpFatalErrorHandler()', function () { |
| 254 | + |
| 255 | + it('returns buffer on no error', function () { |
| 256 | + |
| 257 | + allow('error_get_last')->toBeCalled()->andReturn(null); |
| 258 | + expect($this->listener->phpFatalErrorHandler('test'))->toBe('test'); |
| 259 | + |
| 260 | + }); |
| 261 | + |
| 262 | + it('returns buffer on error has "Uncaught" prefix', function () { |
| 263 | + |
| 264 | + allow('error_get_last')->toBeCalled()->andReturn([ |
| 265 | + 'message' => 'Uncaught', |
| 266 | + 'type' => 3, |
| 267 | + ]); |
| 268 | + expect($this->listener->phpFatalErrorHandler('Uncaught'))->toBe('Uncaught'); |
| 269 | + |
| 270 | + }); |
| 271 | + |
| 272 | + it('returns message value on error not has "Uncaught" prefix and result is empty', function () { |
| 273 | + |
| 274 | + allow('error_get_last')->toBeCalled()->andReturn([ |
| 275 | + 'message' => 'Fatal', |
| 276 | + ]); |
| 277 | + |
| 278 | + expect($this->listener->phpFatalErrorHandler('Fatal'))->toBe('Fatal'); |
| 279 | + |
| 280 | + }); |
| 281 | + |
| 282 | + }); |
| 283 | + |
252 | 284 | describe('->execOnShutdown()', function () { |
253 | 285 |
|
254 | | - it('call error_get_last() and return nothing', function () { |
| 286 | + it('call error_get_last() and return nothing and no result', function () { |
255 | 287 |
|
256 | 288 | allow('error_get_last')->toBeCalled()->andReturn(null); |
257 | 289 | expect($this->listener->execOnShutdown())->toBeNull(); |
258 | 290 |
|
259 | 291 | }); |
260 | 292 |
|
261 | | - it('call error_get_last() and return error', function () { |
| 293 | + it('call error_get_last() and return nothing on result with "Uncaught" prefix', function () { |
262 | 294 |
|
263 | 295 | allow('error_get_last')->toBeCalled()->andReturn([ |
264 | | - 'type' => 8, |
265 | | - 'message' => 'Undefined variable: a', |
| 296 | + 'message' => 'Uncaught', |
| 297 | + 'type' => 3, |
| 298 | + ]); |
| 299 | + expect($this->listener->execOnShutdown())->toBeNull(); |
| 300 | + |
| 301 | + }); |
| 302 | + |
| 303 | + it('call error_get_last() and property_exists() after null check passed', function () { |
| 304 | + |
| 305 | + allow('error_get_last')->toBeCalled()->andReturn([ |
| 306 | + 'type' => 3, |
| 307 | + 'message' => 'class@anonymous cannot implement stdClass - it is not an interface', |
266 | 308 | 'file' => '/var/www/zf/module/Application/Module.php', |
267 | 309 | 'line' => 2 |
268 | 310 | ]); |
|
368 | 410 | $logging, |
369 | 411 | $this->renderer |
370 | 412 | ); |
| 413 | + allow('property_exists')->toBeCalled()->with($listener, 'request')->andReturn(false); |
| 414 | + allow('property_exists')->toBeCalled()->with($listener, 'mvcEvent')->andReturn(true); |
371 | 415 |
|
372 | | - $closure = function () use ($listener) { |
373 | | - $listener->execOnShutdown(); |
374 | | - }; |
375 | | - expect($closure)->toThrow(new ErrorException('Undefined variable: a', 0, 1, '/var/www/zf/module/Application/Module.php', 2)); |
| 416 | + $mvcEvent = & Closure::bind(function & ($listener) { |
| 417 | + return $listener->mvcEvent; |
| 418 | + }, null, $listener)($listener); |
| 419 | + $mvcEvent = Double::instance(['extends' => MvcEvent::class, 'methods' => '__construct']); |
| 420 | + allow($mvcEvent)->toReceive('getRequest')->andReturn(new Request()); |
376 | 421 |
|
| 422 | + expect($listener->execOnShutdown())->toBeNull(); |
377 | 423 |
|
378 | 424 | }); |
379 | 425 |
|
|
402 | 448 |
|
403 | 449 | }); |
404 | 450 |
|
| 451 | + it('throws ErrorException on non excluded php errors', function () { |
| 452 | + |
| 453 | + $closure = function () { |
| 454 | + $this->listener->phpErrorHandler(\E_WARNING, 'warning', 'file.php', 1); |
| 455 | + }; |
| 456 | + expect($closure)->toThrow(new \ErrorException('warning', 0, \E_WARNING, 'file.php', 1)); |
| 457 | + |
| 458 | + }); |
| 459 | + |
405 | 460 | }); |
406 | 461 |
|
407 | 462 | }); |
0 commit comments