|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenSolid\Api\Tests\Controller; |
| 6 | + |
| 7 | +use OpenSolid\Api\Tests\Fixtures\Functional\FunctionalTestKernel; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 10 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 11 | + |
| 12 | +class ApiRouteControllerTest extends WebTestCase |
| 13 | +{ |
| 14 | + private KernelBrowser $client; |
| 15 | + |
| 16 | + protected static function getKernelClass(): string |
| 17 | + { |
| 18 | + return FunctionalTestKernel::class; |
| 19 | + } |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + $this->client = self::createClient(); |
| 24 | + } |
| 25 | + |
| 26 | + protected function tearDown(): void |
| 27 | + { |
| 28 | + parent::tearDown(); |
| 29 | + restore_exception_handler(); |
| 30 | + } |
| 31 | + |
| 32 | + private function getJsonResponse(): array |
| 33 | + { |
| 34 | + return json_decode($this->client->getInternalResponse()->getContent(), true); |
| 35 | + } |
| 36 | + |
| 37 | + #[Test] |
| 38 | + public function getResource(): void |
| 39 | + { |
| 40 | + $this->client->request('GET', '/items/abc-123', server: [ |
| 41 | + 'HTTP_ACCEPT' => 'application/json', |
| 42 | + ]); |
| 43 | + |
| 44 | + self::assertResponseIsSuccessful(); |
| 45 | + self::assertResponseHeaderSame('Content-Type', 'application/json'); |
| 46 | + |
| 47 | + $data = $this->getJsonResponse(); |
| 48 | + self::assertSame('abc-123', $data['id']); |
| 49 | + self::assertSame('Test Item', $data['name']); |
| 50 | + self::assertSame(1000, $data['price']); |
| 51 | + } |
| 52 | + |
| 53 | + #[Test] |
| 54 | + public function postResource(): void |
| 55 | + { |
| 56 | + $this->client->request('POST', '/items', server: [ |
| 57 | + 'CONTENT_TYPE' => 'application/json', |
| 58 | + 'HTTP_ACCEPT' => 'application/json', |
| 59 | + ], content: json_encode([ |
| 60 | + 'name' => 'New Item', |
| 61 | + 'price' => 1500, |
| 62 | + ])); |
| 63 | + |
| 64 | + self::assertResponseStatusCodeSame(201); |
| 65 | + self::assertResponseHeaderSame('Content-Type', 'application/json'); |
| 66 | + |
| 67 | + $data = $this->getJsonResponse(); |
| 68 | + self::assertSame('new-id', $data['id']); |
| 69 | + self::assertSame('New Item', $data['name']); |
| 70 | + self::assertSame(1500, $data['price']); |
| 71 | + } |
| 72 | + |
| 73 | + #[Test] |
| 74 | + public function patchResource(): void |
| 75 | + { |
| 76 | + $this->client->request('PATCH', '/items/abc-123', server: [ |
| 77 | + 'CONTENT_TYPE' => 'application/json', |
| 78 | + 'HTTP_ACCEPT' => 'application/json', |
| 79 | + ], content: json_encode([ |
| 80 | + 'name' => 'Updated Item', |
| 81 | + ])); |
| 82 | + |
| 83 | + self::assertResponseIsSuccessful(); |
| 84 | + self::assertResponseHeaderSame('Content-Type', 'application/json'); |
| 85 | + |
| 86 | + $data = $this->getJsonResponse(); |
| 87 | + self::assertSame('abc-123', $data['id']); |
| 88 | + self::assertSame('Updated Item', $data['name']); |
| 89 | + } |
| 90 | + |
| 91 | + #[Test] |
| 92 | + public function putResourceReturns201WhenCreated(): void |
| 93 | + { |
| 94 | + $this->client->request('PUT', '/items/abc-123', server: [ |
| 95 | + 'CONTENT_TYPE' => 'application/json', |
| 96 | + 'HTTP_ACCEPT' => 'application/json', |
| 97 | + ], content: json_encode([ |
| 98 | + 'name' => 'Replaced Item', |
| 99 | + 'price' => 3000, |
| 100 | + ])); |
| 101 | + |
| 102 | + self::assertResponseStatusCodeSame(201); |
| 103 | + self::assertResponseHeaderSame('Content-Type', 'application/json'); |
| 104 | + |
| 105 | + $data = $this->getJsonResponse(); |
| 106 | + self::assertSame('abc-123', $data['id']); |
| 107 | + self::assertSame('Replaced Item', $data['name']); |
| 108 | + self::assertSame(3000, $data['price']); |
| 109 | + } |
| 110 | + |
| 111 | + #[Test] |
| 112 | + public function deleteResourceReturns204(): void |
| 113 | + { |
| 114 | + $this->client->request('DELETE', '/items/abc-123', server: [ |
| 115 | + 'HTTP_ACCEPT' => 'application/json', |
| 116 | + ]); |
| 117 | + |
| 118 | + self::assertResponseStatusCodeSame(204); |
| 119 | + self::assertSame('', $this->client->getInternalResponse()->getContent()); |
| 120 | + } |
| 121 | + |
| 122 | + #[Test] |
| 123 | + public function getCollectionReturnsPaginatedResponse(): void |
| 124 | + { |
| 125 | + $this->client->request('GET', '/items', server: [ |
| 126 | + 'HTTP_ACCEPT' => 'application/json', |
| 127 | + ]); |
| 128 | + |
| 129 | + self::assertResponseIsSuccessful(); |
| 130 | + self::assertResponseHeaderSame('Content-Type', 'application/json'); |
| 131 | + |
| 132 | + $data = $this->getJsonResponse(); |
| 133 | + self::assertArrayHasKey('items', $data); |
| 134 | + self::assertArrayHasKey('totalItems', $data); |
| 135 | + self::assertCount(2, $data['items']); |
| 136 | + self::assertSame(2, $data['totalItems']); |
| 137 | + self::assertSame('Item 1', $data['items'][0]['name']); |
| 138 | + self::assertSame('Item 2', $data['items'][1]['name']); |
| 139 | + } |
| 140 | + |
| 141 | + #[Test] |
| 142 | + public function responseIncludesVaryHeaders(): void |
| 143 | + { |
| 144 | + $this->client->request('GET', '/items/abc-123', server: [ |
| 145 | + 'HTTP_ACCEPT' => 'application/json', |
| 146 | + ]); |
| 147 | + |
| 148 | + $response = $this->client->getResponse(); |
| 149 | + |
| 150 | + self::assertResponseIsSuccessful(); |
| 151 | + self::assertNotEmpty($response->getVary()); |
| 152 | + self::assertContains('Content-Type', $response->getVary()); |
| 153 | + self::assertContains('Authorization', $response->getVary()); |
| 154 | + self::assertContains('Origin', $response->getVary()); |
| 155 | + } |
| 156 | + |
| 157 | + #[Test] |
| 158 | + public function postResourceWithCustomStatusCode(): void |
| 159 | + { |
| 160 | + $this->client->request('POST', '/items/import', server: [ |
| 161 | + 'CONTENT_TYPE' => 'application/json', |
| 162 | + 'HTTP_ACCEPT' => 'application/json', |
| 163 | + ], content: '{}'); |
| 164 | + |
| 165 | + self::assertResponseStatusCodeSame(202); |
| 166 | + self::assertResponseHeaderSame('Content-Type', 'application/json'); |
| 167 | + |
| 168 | + $data = $this->getJsonResponse(); |
| 169 | + self::assertSame('imported', $data['id']); |
| 170 | + } |
| 171 | + |
| 172 | + #[Test] |
| 173 | + public function controllerReturningJsonResponsePassesThrough(): void |
| 174 | + { |
| 175 | + $this->client->request('GET', '/health', server: [ |
| 176 | + 'HTTP_ACCEPT' => 'application/json', |
| 177 | + ]); |
| 178 | + |
| 179 | + self::assertResponseIsSuccessful(); |
| 180 | + |
| 181 | + $data = $this->getJsonResponse(); |
| 182 | + self::assertSame('ok', $data['status']); |
| 183 | + } |
| 184 | + |
| 185 | + #[Test] |
| 186 | + public function wrongMethodReturns405(): void |
| 187 | + { |
| 188 | + $this->client->request('POST', '/items/abc-123', server: [ |
| 189 | + 'HTTP_ACCEPT' => 'application/json', |
| 190 | + ]); |
| 191 | + |
| 192 | + self::assertResponseStatusCodeSame(405); |
| 193 | + } |
| 194 | +} |
0 commit comments