|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +* This file is part of https://github.com/josantonius/php-exception-handler repository. |
| 5 | +* |
| 6 | +* (c) Josantonius <hello@josantonius.dev> |
| 7 | +* |
| 8 | +* For the full copyright and license information, please view the LICENSE |
| 9 | +* file that was distributed with this source code. |
| 10 | +*/ |
| 11 | + |
| 12 | +namespace Josantonius\ErrorHandler\Tests; |
| 13 | + |
| 14 | +use ReflectionClass; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Josantonius\ExceptionHandler\ExceptionHandler; |
| 17 | +use Josantonius\ExceptionHandler\Tests\Resources\Handler; |
| 18 | +use Josantonius\ExceptionHandler\Tests\Resources\History; |
| 19 | +use Josantonius\ExceptionHandler\Exceptions\WrongMethodNameException; |
| 20 | +use Josantonius\ExceptionHandler\Exceptions\NotCallableException; |
| 21 | +use Josantonius\ExceptionHandler\Tests\Resources\Exceptions\TestException; |
| 22 | + |
| 23 | +class ExceptionHandlerTest extends TestCase |
| 24 | +{ |
| 25 | + private Handler $handler; |
| 26 | + |
| 27 | + public function setUp(): void |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + $this->handler = new Handler(); |
| 32 | + } |
| 33 | + |
| 34 | + public function testShouldFailIfCallableCallbackIsNotPassed(): void |
| 35 | + { |
| 36 | + $this->expectException(NotCallableException::class); |
| 37 | + |
| 38 | + new ExceptionHandler(callback: 'foo'); |
| 39 | + } |
| 40 | + |
| 41 | + public function testShouldFailIfTheMethodNamesNotContainValidDataType(): void |
| 42 | + { |
| 43 | + $this->expectException(WrongMethodNameException::class); |
| 44 | + |
| 45 | + new ExceptionHandler( |
| 46 | + callback: $this->handler->init(...), |
| 47 | + runBeforeCallback: [0, 8] |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function testShouldSetTheHandlerOnlyWithTheCallback(): void |
| 52 | + { |
| 53 | + $this->assertInstanceOf( |
| 54 | + ExceptionHandler::class, |
| 55 | + new ExceptionHandler(callback: $this->handler->init(...)) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public function testShouldSetTheHandlerOnlyWithCallsToRunBefore(): void |
| 60 | + { |
| 61 | + $this->assertInstanceOf( |
| 62 | + ExceptionHandler::class, |
| 63 | + new ExceptionHandler( |
| 64 | + callback: $this->handler->init(...), |
| 65 | + runBeforeCallback: ['context'] |
| 66 | + ) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public function testShouldSetTheHandlerOnlyWithCallsToAfter(): void |
| 71 | + { |
| 72 | + $this->assertInstanceOf( |
| 73 | + ExceptionHandler::class, |
| 74 | + new ExceptionHandler( |
| 75 | + callback: $this->handler->init(...), |
| 76 | + runAfterCallback: ['report', 'render'] |
| 77 | + ) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function testShouldCallTheCallbackWhenAnExceptionIsThrow(): void |
| 82 | + { |
| 83 | + $exceptionHandler = new ExceptionHandler(callback: $this->handler->init(...)); |
| 84 | + |
| 85 | + History::clear(); |
| 86 | + |
| 87 | + $this->simulateException($exceptionHandler); |
| 88 | + |
| 89 | + $this->assertCount(1, History::get()); |
| 90 | + |
| 91 | + $this->assertInstanceOf(Handler::class, History::get(0)); |
| 92 | + $this->assertInstanceOf(TestException::class, History::get(0)->exception); |
| 93 | + |
| 94 | + $this->assertEquals('init', History::get(0)->methodName); |
| 95 | + } |
| 96 | + |
| 97 | + public function testShouldCallTheCallbackBeforeRunMethodsWhenAnExceptionIsThrow(): void |
| 98 | + { |
| 99 | + $exceptionHandler = new ExceptionHandler( |
| 100 | + callback: $this->handler->init(...), |
| 101 | + runBeforeCallback: ['context', 'unknown'], |
| 102 | + ); |
| 103 | + |
| 104 | + History::clear(); |
| 105 | + |
| 106 | + $this->simulateException($exceptionHandler); |
| 107 | + |
| 108 | + $this->assertCount(2, History::get()); |
| 109 | + |
| 110 | + $this->assertInstanceOf(TestException::class, History::get(0)); |
| 111 | + $this->assertInstanceOf(Handler::class, History::get(1)); |
| 112 | + |
| 113 | + $this->assertEquals('context', History::get(0)->methodName); |
| 114 | + } |
| 115 | + |
| 116 | + public function testShouldCallTheCallbackAfterRunMethodsWhenAnExceptionIsThrow(): void |
| 117 | + { |
| 118 | + $exceptionHandler = new ExceptionHandler( |
| 119 | + callback: $this->handler->init(...), |
| 120 | + runAfterCallback: ['report', 'render', 'unknown'], |
| 121 | + ); |
| 122 | + |
| 123 | + History::clear(); |
| 124 | + |
| 125 | + $this->simulateException($exceptionHandler); |
| 126 | + |
| 127 | + $this->assertCount(3, History::get()); |
| 128 | + |
| 129 | + $this->assertInstanceOf(Handler::class, History::get(0)); |
| 130 | + $this->assertInstanceOf(TestException::class, History::get(1)); |
| 131 | + $this->assertInstanceOf(TestException::class, History::get(2)); |
| 132 | + |
| 133 | + $this->assertEquals('report', History::get(1)->methodName); |
| 134 | + $this->assertEquals('render', History::get(2)->methodName); |
| 135 | + } |
| 136 | + |
| 137 | + public function testShouldCallTheCallbackAfterAndBeforeRunMethodsWhenAnExceptionIsThrow(): void |
| 138 | + { |
| 139 | + $exceptionHandler = new ExceptionHandler( |
| 140 | + callback: $this->handler->init(...), |
| 141 | + runBeforeCallback: ['context', 'unknown'], |
| 142 | + runAfterCallback: ['report', 'render', 'unknown'], |
| 143 | + ); |
| 144 | + |
| 145 | + History::clear(); |
| 146 | + |
| 147 | + $this->simulateException($exceptionHandler); |
| 148 | + |
| 149 | + $this->assertCount(4, History::get()); |
| 150 | + |
| 151 | + $this->assertInstanceOf(TestException::class, History::get(0)); |
| 152 | + $this->assertInstanceOf(Handler::class, History::get(1)); |
| 153 | + $this->assertInstanceOf(TestException::class, History::get(2)); |
| 154 | + $this->assertInstanceOf(TestException::class, History::get(3)); |
| 155 | + |
| 156 | + $this->assertEquals('context', History::get(0)->methodName); |
| 157 | + $this->assertEquals('report', History::get(2)->methodName); |
| 158 | + $this->assertEquals('render', History::get(3)->methodName); |
| 159 | + } |
| 160 | + |
| 161 | + private function simulateException(ExceptionHandler $object): void |
| 162 | + { |
| 163 | + $reflection = new ReflectionClass($object); |
| 164 | + $reflection = $reflection->getMethod('handler'); |
| 165 | + $reflection->setAccessible(true); |
| 166 | + $reflection->invoke($object, new TestException()); |
| 167 | + } |
| 168 | +} |
0 commit comments