|
13 | 13 | use Attributes\Wp\Exceptions\ExceptionHandler; |
14 | 14 | use Attributes\Wp\Exceptions\HttpException; |
15 | 15 | use Attributes\Wp\Exceptions\Tests\Helpers\Helpers; |
| 16 | +use Brain\Monkey\Functions; |
16 | 17 | use Exception; |
| 18 | +use Mockery; |
| 19 | +use WP_Error; |
17 | 20 |
|
18 | 21 | $previousHandler = fn () => true; |
19 | 22 |
|
|
25 | 28 | $attributes_wp_exceptions_exception_handler = null; |
26 | 29 | }); |
27 | 30 |
|
| 31 | +function set(string $name, mixed $value): mixed |
| 32 | +{ |
| 33 | + global $attributes_wp_exceptions_exception_handler; |
| 34 | + |
| 35 | + return Helpers::setNonPublicClassProperty($attributes_wp_exceptions_exception_handler, $name, $value); |
| 36 | +} |
| 37 | + |
28 | 38 | function get(string $name): mixed |
29 | 39 | { |
30 | 40 | global $attributes_wp_exceptions_exception_handler; |
@@ -164,3 +174,62 @@ class CustomException extends Exception {} |
164 | 174 | expect($handler)->toBeNull(); |
165 | 175 | }) |
166 | 176 | ->group('exception', 'handler', 'getExceptionHandler'); |
| 177 | + |
| 178 | +// __invoke |
| 179 | + |
| 180 | +test('Invokes custom handler', function () { |
| 181 | + $exceptionHandler = Mockery::mock(ExceptionHandler::class) |
| 182 | + ->shouldAllowMockingProtectedMethods() |
| 183 | + ->makePartial(); |
| 184 | + $ex = new Exception('Ignore message'); |
| 185 | + $exceptionHandler->shouldReceive('getExceptionHandler') |
| 186 | + ->with($ex) |
| 187 | + ->once() |
| 188 | + ->andReturn(fn ($ex) => throw new Exception('Working')); |
| 189 | + |
| 190 | + call_user_func($exceptionHandler, $ex); |
| 191 | +}) |
| 192 | + ->throws(Exception::class, 'Working') |
| 193 | + ->group('exception', 'handler', '__invoke'); |
| 194 | + |
| 195 | +test('Invokes previous exception handler', function () { |
| 196 | + $exceptionHandler = ExceptionHandler::register(); |
| 197 | + set('previousHandler', fn ($ex) => throw new Exception('Working')); |
| 198 | + $ex = new Exception('Ignore message'); |
| 199 | + call_user_func($exceptionHandler, $ex); |
| 200 | +}) |
| 201 | + ->throws(Exception::class, 'Working') |
| 202 | + ->group('exception', 'handler', '__invoke'); |
| 203 | + |
| 204 | +test('No handler to invoke', function () { |
| 205 | + $exceptionHandler = ExceptionHandler::register(); |
| 206 | + set('previousHandler', null); |
| 207 | + $ex = new Exception('Working'); |
| 208 | + call_user_func($exceptionHandler, $ex); |
| 209 | +}) |
| 210 | + ->throws(Exception::class, 'Working') |
| 211 | + ->group('exception', 'handler', '__invoke'); |
| 212 | + |
| 213 | +test('Custom invoker', function () { |
| 214 | + $exceptionHandler = ExceptionHandler::register(); |
| 215 | + set('invoker', fn ($handler, $ex) => throw new Exception('Working')); |
| 216 | + $ex = new HttpException(500, 'Ignore message'); |
| 217 | + call_user_func($exceptionHandler, $ex); |
| 218 | +}) |
| 219 | + ->throws(Exception::class, 'Working') |
| 220 | + ->group('exception', 'handler', '__invoke'); |
| 221 | + |
| 222 | +// handleHttpException |
| 223 | + |
| 224 | +test('Handles HTTP exceptions', function () { |
| 225 | + Functions\expect('wp_die')->once()->andReturnUsing(fn ($wpError) => expect($wpError) |
| 226 | + ->toBeInstanceOf(WP_Error::class) |
| 227 | + ->and($wpError->get_error_code())->toBe(501) |
| 228 | + ->and($wpError->get_error_message())->toBe('My custom error message') |
| 229 | + ->and($wpError->get_error_data())->toMatchArray(['custom' => true, 'status' => 501]) |
| 230 | + ); |
| 231 | + $exceptionHandler = ExceptionHandler::register(); |
| 232 | + $ex = new HttpException(501, 'My custom error message', data: ['custom' => true], headers: ['key' => 'value']); |
| 233 | + call_user_func($exceptionHandler, $ex); |
| 234 | +}) |
| 235 | + ->group('exception', 'handler', 'handleHttpException'); |
0 commit comments