|
| 1 | +<?php |
| 2 | +// file generated with AI assistance: Claude Code - 2026-06-13 23:14:54 UTC |
| 3 | + |
| 4 | +declare(strict_types=1); |
| 5 | + |
| 6 | +namespace Dmstr\Bc4Client\Tests\Exception; |
| 7 | + |
| 8 | +use Dmstr\Bc4Client\Exception\Bc4ApiException; |
| 9 | +use Dmstr\Bc4Client\Exception\InvalidGrantException; |
| 10 | +use Dmstr\Bc4Client\Exception\RequestException; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +/** |
| 14 | + * Unit tests for the BC4 client exception hierarchy — verifies the type |
| 15 | + * relationships callers rely on for catch-block dispatch, plus the |
| 16 | + * RequestException payload accessors. |
| 17 | + */ |
| 18 | +final class ExceptionTest extends TestCase |
| 19 | +{ |
| 20 | + public function testBc4ApiExceptionIsRuntimeException(): void |
| 21 | + { |
| 22 | + self::assertInstanceOf(\RuntimeException::class, new Bc4ApiException('boom')); |
| 23 | + } |
| 24 | + |
| 25 | + public function testInvalidGrantExceptionExtendsBc4ApiException(): void |
| 26 | + { |
| 27 | + $exception = new InvalidGrantException('refresh token rejected'); |
| 28 | + |
| 29 | + self::assertInstanceOf(Bc4ApiException::class, $exception); |
| 30 | + self::assertInstanceOf(\RuntimeException::class, $exception); |
| 31 | + self::assertSame('refresh token rejected', $exception->getMessage()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testRequestExceptionExposesStatusAndBody(): void |
| 35 | + { |
| 36 | + $exception = new RequestException('Not Found', 404, '{"error":"missing"}'); |
| 37 | + |
| 38 | + self::assertInstanceOf(Bc4ApiException::class, $exception); |
| 39 | + self::assertSame('Not Found', $exception->getMessage()); |
| 40 | + self::assertSame(404, $exception->getStatusCode()); |
| 41 | + self::assertSame('{"error":"missing"}', $exception->getResponseBody()); |
| 42 | + self::assertSame(0, $exception->getCode()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testRequestExceptionResponseBodyDefaultsToEmpty(): void |
| 46 | + { |
| 47 | + self::assertSame('', (new RequestException('Server Error', 500))->getResponseBody()); |
| 48 | + } |
| 49 | + |
| 50 | + public function testRequestExceptionPreservesPreviousThrowable(): void |
| 51 | + { |
| 52 | + $previous = new \LogicException('root cause'); |
| 53 | + $exception = new RequestException('Bad Gateway', 502, '', $previous); |
| 54 | + |
| 55 | + self::assertSame($previous, $exception->getPrevious()); |
| 56 | + } |
| 57 | +} |
0 commit comments