|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GeneaLabs\LaravelSignInWithApple\Tests\Unit; |
| 4 | + |
| 5 | +use GeneaLabs\LaravelSignInWithApple\Providers\SignInWithAppleProvider; |
| 6 | +use GeneaLabs\LaravelSignInWithApple\Tests\UnitTestCase; |
| 7 | +use GuzzleHttp\Exception\ServerException; |
| 8 | +use GuzzleHttp\Psr7\Response; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use ReflectionMethod; |
| 11 | + |
| 12 | +class ServerErrorHandlingTest extends UnitTestCase |
| 13 | +{ |
| 14 | + protected function makeProvider(): SignInWithAppleProvider |
| 15 | + { |
| 16 | + return new SignInWithAppleProvider( |
| 17 | + Request::create('/'), |
| 18 | + 'test-client-id', |
| 19 | + 'test-client-secret', |
| 20 | + 'https://example.com/callback' |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + public function testHandles500ServerError(): void |
| 25 | + { |
| 26 | + $provider = $this->makeProvider(); |
| 27 | + |
| 28 | + $response = new Response(500, [], 'Internal Server Error'); |
| 29 | + $exception = new ServerException('Server error', new \GuzzleHttp\Psr7\Request('POST', '/'), $response); |
| 30 | + |
| 31 | + $method = new ReflectionMethod($provider, 'handleServerError'); |
| 32 | + $method->setAccessible(true); |
| 33 | + |
| 34 | + try { |
| 35 | + $method->invoke($provider, $exception); |
| 36 | + $this->fail('Expected RuntimeException'); |
| 37 | + } catch (\RuntimeException $e) { |
| 38 | + $this->assertStringContainsString('temporarily unavailable', $e->getMessage()); |
| 39 | + $this->assertStringContainsString('500', $e->getMessage()); |
| 40 | + $this->assertSame($exception, $e->getPrevious()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public function testHandles503ServiceUnavailable(): void |
| 45 | + { |
| 46 | + $provider = $this->makeProvider(); |
| 47 | + |
| 48 | + $response = new Response(503, [], 'Service Unavailable'); |
| 49 | + $exception = new ServerException('Server error', new \GuzzleHttp\Psr7\Request('POST', '/'), $response); |
| 50 | + |
| 51 | + $method = new ReflectionMethod($provider, 'handleServerError'); |
| 52 | + $method->setAccessible(true); |
| 53 | + |
| 54 | + try { |
| 55 | + $method->invoke($provider, $exception); |
| 56 | + $this->fail('Expected RuntimeException'); |
| 57 | + } catch (\RuntimeException $e) { |
| 58 | + $this->assertStringContainsString('503', $e->getMessage()); |
| 59 | + $this->assertStringContainsString('try again', $e->getMessage()); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments