|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +namespace Tests\Feature\Facades; |
| 4 | + |
3 | 5 | use Illuminate\Support\Facades\Facade;
|
4 | 6 | use Illuminate\Support\Facades\Log;
|
5 | 7 | use Laravel\Nightwatch\Core;
|
6 | 8 | use Laravel\Nightwatch\Facades\Nightwatch;
|
| 9 | +use ReflectionClass; |
| 10 | +use RuntimeException; |
| 11 | +use Tests\TestCase; |
| 12 | +use Throwable; |
| 13 | + |
| 14 | +use function expect; |
| 15 | + |
| 16 | +class NightwatchTest extends TestCase |
| 17 | +{ |
| 18 | + public function test_it_resolves_to_bound_singleton_instance_of_the_core_class() |
| 19 | + { |
| 20 | + expect(Nightwatch::getFacadeRoot())->toBeInstanceOf(Core::class); |
| 21 | + |
| 22 | + expect(Nightwatch::getFacadeRoot())->toBe($this->app[Core::class]); |
| 23 | + |
| 24 | + Facade::clearResolvedInstances(); |
| 25 | + expect(Nightwatch::getFacadeRoot())->toBe($this->app[Core::class]); |
| 26 | + } |
| 27 | + |
| 28 | + public function test_it_silently_discards_unrecoverable_exceptions_by_default() |
| 29 | + { |
| 30 | + (new ReflectionClass(Nightwatch::class))->getProperty('handleUnrecoverableExceptionsUsing')->setValue(null); |
| 31 | + $calls = 0; |
| 32 | + Log::listen(function () use (&$calls) { |
| 33 | + $calls++; |
| 34 | + }); |
| 35 | + |
| 36 | + Nightwatch::unrecoverableExceptionOccurred(new RuntimeException('Whoops!')); |
| 37 | + |
| 38 | + expect($calls)->toBe(0); |
| 39 | + } |
| 40 | + |
| 41 | + public function test_it_can_register_a_callback_to_handle_unrecoverable_exceptions() |
| 42 | + { |
| 43 | + $handled = []; |
| 44 | + Nightwatch::handleUnrecoverableExceptionsUsing(function (Throwable $e) use (&$handled) { |
| 45 | + $handled[] = $e; |
| 46 | + }); |
| 47 | + |
| 48 | + Nightwatch::unrecoverableExceptionOccurred($first = new RuntimeException('Whoops!')); |
| 49 | + Nightwatch::unrecoverableExceptionOccurred($second = new RuntimeException('Whoops!')); |
| 50 | + |
| 51 | + expect($handled)->toBe([ |
| 52 | + $first, |
| 53 | + $second, |
| 54 | + ]); |
| 55 | + } |
| 56 | + |
| 57 | + public function test_it_handles_unrecoverable_exceptions_statelessly() |
| 58 | + { |
| 59 | + $this->app->forgetInstance(Core::class); |
| 60 | + $resolved = false; |
| 61 | + Nightwatch::resolved(function () use (&$resolved) { |
| 62 | + $resolved = true; |
| 63 | + }); |
| 64 | + |
| 65 | + $handled = []; |
| 66 | + Nightwatch::handleUnrecoverableExceptionsUsing(function (Throwable $e) use (&$handled) { |
| 67 | + $handled[] = $e; |
| 68 | + }); |
| 69 | + Nightwatch::unrecoverableExceptionOccurred($first = new RuntimeException('Whoops!')); |
| 70 | + |
| 71 | + expect($resolved)->toBeFalse(); |
| 72 | + expect($handled)->toHaveCount(1); |
| 73 | + expect($this->app->resolved(Core::class))->toBeFalse(); |
| 74 | + } |
| 75 | + |
| 76 | + public function test_it_silences_exceptions_thrown_while_handling_exceptions() |
| 77 | + { |
| 78 | + Nightwatch::handleUnrecoverableExceptionsUsing(function (): object { |
| 79 | + // Should return an object. Returning an int to cause an exception. |
| 80 | + return 5; |
| 81 | + }); |
| 82 | + |
| 83 | + Nightwatch::unrecoverableExceptionOccurred(new RuntimeException('Whoops!')); |
7 | 84 |
|
8 |
| -it('resolves to bound singleton instance of the Core class', function () { |
9 |
| - expect(Nightwatch::getFacadeRoot())->toBeInstanceOf(Core::class); |
10 |
| - |
11 |
| - expect(Nightwatch::getFacadeRoot())->toBe(app(Core::class)); |
12 |
| - |
13 |
| - Facade::clearResolvedInstances(); |
14 |
| - expect(Nightwatch::getFacadeRoot())->toBe(app(Core::class)); |
15 |
| -}); |
16 |
| - |
17 |
| -it('silently discards unrecoverable exceptions by default', function () { |
18 |
| - (new ReflectionClass(Nightwatch::class))->getProperty('handleUnrecoverableExceptionsUsing')->setValue(null); |
19 |
| - $calls = 0; |
20 |
| - Log::listen(function () use (&$calls) { |
21 |
| - $calls++; |
22 |
| - }); |
23 |
| - |
24 |
| - Nightwatch::unrecoverableExceptionOccurred(new RuntimeException('Whoops!')); |
25 |
| - |
26 |
| - expect($calls)->toBe(0); |
27 |
| -}); |
28 |
| - |
29 |
| -it('can register a callback to handle unrecoverable exceptions', function () { |
30 |
| - $handled = []; |
31 |
| - Nightwatch::handleUnrecoverableExceptionsUsing(function (Throwable $e) use (&$handled) { |
32 |
| - $handled[] = $e; |
33 |
| - }); |
34 |
| - |
35 |
| - Nightwatch::unrecoverableExceptionOccurred($first = new RuntimeException('Whoops!')); |
36 |
| - Nightwatch::unrecoverableExceptionOccurred($second = new RuntimeException('Whoops!')); |
37 |
| - |
38 |
| - expect($handled)->toBe([ |
39 |
| - $first, |
40 |
| - $second, |
41 |
| - ]); |
42 |
| -}); |
43 |
| - |
44 |
| -it('handles unrecoverable exceptions statelessly', function () { |
45 |
| - app()->forgetInstance(Core::class); |
46 |
| - $resolved = false; |
47 |
| - Nightwatch::resolved(function () use (&$resolved) { |
48 |
| - $resolved = true; |
49 |
| - }); |
50 |
| - |
51 |
| - $handled = []; |
52 |
| - Nightwatch::handleUnrecoverableExceptionsUsing(function (Throwable $e) use (&$handled) { |
53 |
| - $handled[] = $e; |
54 |
| - }); |
55 |
| - Nightwatch::unrecoverableExceptionOccurred($first = new RuntimeException('Whoops!')); |
56 |
| - |
57 |
| - expect($resolved)->toBeFalse(); |
58 |
| - expect($handled)->toHaveCount(1); |
59 |
| - expect(app()->resolved(Core::class))->toBeFalse(); |
60 |
| -}); |
61 |
| - |
62 |
| -it('silences exceptions thrown while handling exceptions', function () { |
63 |
| - Nightwatch::handleUnrecoverableExceptionsUsing(function (): object { |
64 |
| - // Should return an object. Returning an int to cause an exception. |
65 |
| - return 5; |
66 |
| - }); |
67 |
| - |
68 |
| - Nightwatch::unrecoverableExceptionOccurred(new RuntimeException('Whoops!')); |
69 |
| -}); |
| 85 | + $this->assertTrue(true); |
| 86 | + } |
| 87 | +} |
0 commit comments