Skip to content

Commit ed2671d

Browse files
committed
tests: Add missing unit tests for ExceptionHandler
1 parent c36e52d commit ed2671d

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tests/Unit/ExceptionHandlerTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
use Attributes\Wp\Exceptions\ExceptionHandler;
1414
use Attributes\Wp\Exceptions\HttpException;
1515
use Attributes\Wp\Exceptions\Tests\Helpers\Helpers;
16+
use Brain\Monkey\Functions;
1617
use Exception;
18+
use Mockery;
19+
use WP_Error;
1720

1821
$previousHandler = fn () => true;
1922

@@ -25,6 +28,13 @@
2528
$attributes_wp_exceptions_exception_handler = null;
2629
});
2730

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+
2838
function get(string $name): mixed
2939
{
3040
global $attributes_wp_exceptions_exception_handler;
@@ -164,3 +174,62 @@ class CustomException extends Exception {}
164174
expect($handler)->toBeNull();
165175
})
166176
->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

Comments
 (0)