|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Detain\MyAdminPlesk\Tests; |
| 6 | + |
| 7 | +use Detain\MyAdminPlesk\ApiRequestException; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use ReflectionClass; |
| 10 | + |
| 11 | +/** |
| 12 | + * Unit tests for the ApiRequestException class. |
| 13 | + * |
| 14 | + * Tests verify correct class hierarchy, instantiation, and |
| 15 | + * exception behavior. |
| 16 | + */ |
| 17 | +class ApiRequestExceptionTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var ReflectionClass<ApiRequestException> |
| 21 | + */ |
| 22 | + private ReflectionClass $reflection; |
| 23 | + |
| 24 | + /** |
| 25 | + * Set up reflection for each test. |
| 26 | + * |
| 27 | + * @return void |
| 28 | + */ |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + parent::setUp(); |
| 32 | + $this->reflection = new ReflectionClass(ApiRequestException::class); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Verify the ApiRequestException class exists. |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function testClassExists(): void |
| 41 | + { |
| 42 | + $this->assertTrue(class_exists(ApiRequestException::class)); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Verify the class resides in the correct namespace. |
| 47 | + * |
| 48 | + * @return void |
| 49 | + */ |
| 50 | + public function testClassNamespace(): void |
| 51 | + { |
| 52 | + $this->assertSame('Detain\MyAdminPlesk', $this->reflection->getNamespaceName()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Verify ApiRequestException extends the base Exception class. |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function testExtendsException(): void |
| 61 | + { |
| 62 | + $this->assertTrue($this->reflection->isSubclassOf(\Exception::class)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Verify ApiRequestException implements Throwable. |
| 67 | + * |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + public function testImplementsThrowable(): void |
| 71 | + { |
| 72 | + $this->assertTrue($this->reflection->implementsInterface(\Throwable::class)); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Verify the exception can be instantiated with a message. |
| 77 | + * |
| 78 | + * @return void |
| 79 | + */ |
| 80 | + public function testCanBeInstantiatedWithMessage(): void |
| 81 | + { |
| 82 | + $exception = new ApiRequestException('Test error message'); |
| 83 | + $this->assertSame('Test error message', $exception->getMessage()); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Verify the exception can be instantiated with a message and code. |
| 88 | + * |
| 89 | + * @return void |
| 90 | + */ |
| 91 | + public function testCanBeInstantiatedWithMessageAndCode(): void |
| 92 | + { |
| 93 | + $exception = new ApiRequestException('Connection failed', 7); |
| 94 | + $this->assertSame('Connection failed', $exception->getMessage()); |
| 95 | + $this->assertSame(7, $exception->getCode()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Verify the exception can be instantiated with no arguments. |
| 100 | + * |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + public function testCanBeInstantiatedWithNoArguments(): void |
| 104 | + { |
| 105 | + $exception = new ApiRequestException(); |
| 106 | + $this->assertSame('', $exception->getMessage()); |
| 107 | + $this->assertSame(0, $exception->getCode()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Verify the exception can be thrown and caught. |
| 112 | + * |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + public function testCanBeThrownAndCaught(): void |
| 116 | + { |
| 117 | + $this->expectException(ApiRequestException::class); |
| 118 | + $this->expectExceptionMessage('API request failed'); |
| 119 | + throw new ApiRequestException('API request failed'); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Verify the exception can be caught as a base Exception. |
| 124 | + * |
| 125 | + * @return void |
| 126 | + */ |
| 127 | + public function testCanBeCaughtAsBaseException(): void |
| 128 | + { |
| 129 | + $caught = false; |
| 130 | + try { |
| 131 | + throw new ApiRequestException('test'); |
| 132 | + } catch (\Exception $e) { |
| 133 | + $caught = true; |
| 134 | + $this->assertInstanceOf(ApiRequestException::class, $e); |
| 135 | + } |
| 136 | + $this->assertTrue($caught); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Verify the exception supports a previous exception in the chain. |
| 141 | + * |
| 142 | + * @return void |
| 143 | + */ |
| 144 | + public function testSupportsPreviousException(): void |
| 145 | + { |
| 146 | + $previous = new \RuntimeException('Root cause'); |
| 147 | + $exception = new ApiRequestException('Wrapper', 0, $previous); |
| 148 | + $this->assertSame($previous, $exception->getPrevious()); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Verify the class is not abstract. |
| 153 | + * |
| 154 | + * @return void |
| 155 | + */ |
| 156 | + public function testClassIsNotAbstract(): void |
| 157 | + { |
| 158 | + $this->assertFalse($this->reflection->isAbstract()); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Verify the class is not final. |
| 163 | + * |
| 164 | + * @return void |
| 165 | + */ |
| 166 | + public function testClassIsNotFinal(): void |
| 167 | + { |
| 168 | + $this->assertFalse($this->reflection->isFinal()); |
| 169 | + } |
| 170 | +} |
0 commit comments