Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/sdk-php. php sdk for openstatus www.openstatus.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125<?php
declare(strict_types=1);
namespace Openstatus\Sdk\Tests\Unit\Internal;
use GuzzleHttp\Psr7\Response;use Openstatus\Sdk\Exception\AuthenticationException;use Openstatus\Sdk\Exception\InvalidArgumentException;use Openstatus\Sdk\Exception\NotFoundException;use Openstatus\Sdk\Exception\OpenstatusException;use Openstatus\Sdk\Exception\PermissionDeniedException;use Openstatus\Sdk\Exception\RateLimitException;use Openstatus\Sdk\Exception\ServiceUnavailableException;use Openstatus\Sdk\Internal\ConnectErrorMapper;use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;
final class ConnectErrorMapperTest extends TestCase{ /** * @return iterable<string, array{string, class-string<OpenstatusException>}> */ public static function codeToExceptionClassProvider(): iterable { yield 'unauthenticated -> AuthenticationException' => ['unauthenticated', AuthenticationException::class]; yield 'not_found -> NotFoundException' => ['not_found', NotFoundException::class]; yield 'invalid_argument -> InvalidArgumentException' => ['invalid_argument', InvalidArgumentException::class]; yield 'permission_denied -> PermissionDeniedException' => ['permission_denied', PermissionDeniedException::class]; yield 'resource_exhausted -> RateLimitException' => ['resource_exhausted', RateLimitException::class]; yield 'unavailable -> ServiceUnavailableException' => ['unavailable', ServiceUnavailableException::class]; yield 'aborted -> base OpenstatusException' => ['aborted', OpenstatusException::class]; yield 'unknown -> base OpenstatusException' => ['unknown', OpenstatusException::class]; }
/** * @param class-string<OpenstatusException> $expectedClass */ #[DataProvider('codeToExceptionClassProvider')] public function testRethrowMapsCodeToExceptionClass(string $code, string $expectedClass): void { $body = json_encode(['code' => $code, 'message' => 'something happened']); self::assertIsString($body); $response = new Response(400, [], $body);
try { ConnectErrorMapper::rethrow($response); } catch (OpenstatusException $e) { self::assertSame($expectedClass, $e::class); self::assertSame('something happened', $e->getMessage()); self::assertSame($code, $e->connectCode()); self::assertSame(400, $e->httpStatus()); self::assertSame($body, $e->rawBody()); } }
public function testRethrowParsesDetails(): void { $body = json_encode([ 'code' => 'invalid_argument', 'message' => 'validation failed', 'details' => [ ['type' => 'buf.validate.Violations', 'value' => 'aGVsbG8='], ['type' => 'debug.example', 'value' => '{}', 'debug' => 'extra context'], ['type' => 'malformed-missing-value'], 'not-an-object', ], ]); self::assertIsString($body); $response = new Response(400, [], $body);
try { ConnectErrorMapper::rethrow($response); } catch (InvalidArgumentException $e) { $details = $e->details(); self::assertCount(2, $details); self::assertSame('buf.validate.Violations', $details[0]->type); self::assertSame('aGVsbG8=', $details[0]->value); self::assertNull($details[0]->debug); self::assertSame('debug.example', $details[1]->type); self::assertSame('{}', $details[1]->value); self::assertSame('extra context', $details[1]->debug); } }
public function testRethrowFallsBackToServiceUnavailableForInvalidJson(): void { $response = new Response(502, [], '<html>Bad Gateway</html>');
try { ConnectErrorMapper::rethrow($response); } catch (ServiceUnavailableException $e) { self::assertSame('unavailable', $e->connectCode()); self::assertSame(502, $e->httpStatus()); self::assertSame('<html>Bad Gateway</html>', $e->rawBody()); self::assertStringContainsString('HTTP 502', $e->getMessage()); } }
public function testRethrowFallsBackToServiceUnavailableForJsonMissingCode(): void { $response = new Response(500, [], '{"foo":"bar"}');
try { ConnectErrorMapper::rethrow($response); } catch (ServiceUnavailableException $e) { self::assertSame('unavailable', $e->connectCode()); self::assertSame(500, $e->httpStatus()); } }
public function testRethrowFallsBackToServiceUnavailableForEmptyBody(): void { $response = new Response(503, [], '');
try { ConnectErrorMapper::rethrow($response); } catch (ServiceUnavailableException $e) { self::assertSame('unavailable', $e->connectCode()); self::assertSame(503, $e->httpStatus()); self::assertSame('', $e->rawBody()); } }}