|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Maatify\Exceptions\Tests\Unit\Core; |
| 6 | + |
| 7 | +use Maatify\Exceptions\Contracts\ErrorCategoryInterface; |
| 8 | +use Maatify\Exceptions\Contracts\ErrorCodeInterface; |
| 9 | +use Maatify\Exceptions\Contracts\ErrorPolicyInterface; |
| 10 | +use Maatify\Exceptions\Contracts\EscalationPolicyInterface; |
| 11 | +use Maatify\Exceptions\Enum\ErrorCategoryEnum; |
| 12 | +use Maatify\Exceptions\Enum\ErrorCodeEnum; |
| 13 | +use Maatify\Exceptions\Exception\MaatifyException; |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +#[CoversClass(MaatifyException::class)] |
| 19 | +final class ConstructorMatrixTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @return iterable<string, array{ |
| 23 | + * string, |
| 24 | + * int, |
| 25 | + * ?\Throwable, |
| 26 | + * ?ErrorCodeInterface, |
| 27 | + * ?int, |
| 28 | + * ?bool, |
| 29 | + * ?bool, |
| 30 | + * array<string, mixed>, |
| 31 | + * ?ErrorPolicyInterface, |
| 32 | + * ?EscalationPolicyInterface |
| 33 | + * }> |
| 34 | + */ |
| 35 | + public static function constructorProvider(): iterable |
| 36 | + { |
| 37 | + // 1. Minimal |
| 38 | + yield 'Minimal' => [ |
| 39 | + 'Msg', 0, null, null, null, null, null, [], null, null |
| 40 | + ]; |
| 41 | + |
| 42 | + // 2. Full Overrides |
| 43 | + yield 'Full Overrides' => [ |
| 44 | + 'Msg', 123, new \Exception(), ErrorCodeEnum::DATABASE_CONNECTION_FAILED, 503, true, true, ['k' => 'v'], null, null |
| 45 | + ]; |
| 46 | + |
| 47 | + // 3. Custom Policy |
| 48 | + $policy = new class implements ErrorPolicyInterface { |
| 49 | + public function validate(ErrorCodeInterface $code, ErrorCategoryInterface $category): void {} |
| 50 | + public function severity(ErrorCategoryInterface $category): int { return 100; } |
| 51 | + }; |
| 52 | + yield 'Custom Policy' => [ |
| 53 | + 'Msg', 0, null, ErrorCodeEnum::MAATIFY_ERROR, null, null, null, [], $policy, null |
| 54 | + ]; |
| 55 | + |
| 56 | + // 4. Custom Escalation Policy |
| 57 | + $escPolicy = new class implements EscalationPolicyInterface { |
| 58 | + public function escalateCategory(ErrorCategoryInterface $c, ErrorCategoryInterface $p, ErrorPolicyInterface $pol): ErrorCategoryInterface { return $c; } |
| 59 | + public function escalateHttpStatus(int $c, int $p): int { return $c; } |
| 60 | + }; |
| 61 | + yield 'Custom Escalation' => [ |
| 62 | + 'Msg', 0, null, null, null, null, null, [], null, $escPolicy |
| 63 | + ]; |
| 64 | + |
| 65 | + // 5. Previous ApiAware Exception |
| 66 | + $prevApi = new class extends MaatifyException { |
| 67 | + protected function defaultCategory(): ErrorCategoryInterface { return ErrorCategoryEnum::SYSTEM; } |
| 68 | + protected function defaultErrorCode(): ErrorCodeInterface { return ErrorCodeEnum::MAATIFY_ERROR; } |
| 69 | + protected function defaultHttpStatus(): int { return 500; } |
| 70 | + }; |
| 71 | + yield 'Previous ApiAware' => [ |
| 72 | + 'Msg', 0, $prevApi, null, null, null, null, [], null, null |
| 73 | + ]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param array<string, mixed> $meta |
| 78 | + */ |
| 79 | + #[DataProvider('constructorProvider')] |
| 80 | + public function testConstructorMatrix( |
| 81 | + string $message, |
| 82 | + int $code, |
| 83 | + ?\Throwable $previous, |
| 84 | + ?ErrorCodeInterface $errorCodeOverride, |
| 85 | + ?int $httpStatusOverride, |
| 86 | + ?bool $isSafeOverride, |
| 87 | + ?bool $isRetryableOverride, |
| 88 | + array $meta, |
| 89 | + ?ErrorPolicyInterface $policy, |
| 90 | + ?EscalationPolicyInterface $escalationPolicy |
| 91 | + ): void { |
| 92 | + // We need a concrete implementation to instantiate |
| 93 | + $exception = new class( |
| 94 | + $message, |
| 95 | + $code, |
| 96 | + $previous, |
| 97 | + $errorCodeOverride, |
| 98 | + $httpStatusOverride, |
| 99 | + $isSafeOverride, |
| 100 | + $isRetryableOverride, |
| 101 | + $meta, |
| 102 | + $policy, |
| 103 | + $escalationPolicy |
| 104 | + ) extends MaatifyException { |
| 105 | + protected function defaultCategory(): ErrorCategoryInterface { |
| 106 | + return ErrorCategoryEnum::SYSTEM; |
| 107 | + } |
| 108 | + |
| 109 | + protected function defaultErrorCode(): ErrorCodeInterface { |
| 110 | + return ErrorCodeEnum::MAATIFY_ERROR; |
| 111 | + } |
| 112 | + |
| 113 | + protected function defaultHttpStatus(): int { |
| 114 | + return 500; |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + $this->assertSame($message, $exception->getMessage()); |
| 119 | + $this->assertSame($code, $exception->getCode()); |
| 120 | + $this->assertSame($previous, $exception->getPrevious()); |
| 121 | + |
| 122 | + if ($errorCodeOverride) { |
| 123 | + $this->assertSame($errorCodeOverride, $exception->getErrorCode()); |
| 124 | + } |
| 125 | + |
| 126 | + if ($httpStatusOverride) { |
| 127 | + $this->assertSame($httpStatusOverride, $exception->getHttpStatus()); |
| 128 | + } |
| 129 | + |
| 130 | + if ($isSafeOverride !== null) { |
| 131 | + $this->assertSame($isSafeOverride, $exception->isSafe()); |
| 132 | + } |
| 133 | + |
| 134 | + if ($isRetryableOverride !== null) { |
| 135 | + $this->assertSame($isRetryableOverride, $exception->isRetryable()); |
| 136 | + } |
| 137 | + |
| 138 | + $this->assertSame($meta, $exception->getMeta()); |
| 139 | + } |
| 140 | +} |
0 commit comments