|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Functional; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Symfony\Bundle\Test\Client; |
| 18 | + |
| 19 | +class DocumentationActionTest extends ApiTestCase |
| 20 | +{ |
| 21 | + protected static ?bool $alwaysBootKernel = true; |
| 22 | + |
| 23 | + private function createClientWithEnv(string $env): Client |
| 24 | + { |
| 25 | + return self::createClient(['environment' => $env]); |
| 26 | + } |
| 27 | + |
| 28 | + public function testHtmlDocumentationIsNotAccessibleWhenSwaggerUiIsDisabled(): void |
| 29 | + { |
| 30 | + $client = $this->createClientWithEnv('swagger_ui_disabled'); |
| 31 | + |
| 32 | + $client->request('GET', '/docs', ['headers' => ['Accept' => 'text/html']]); |
| 33 | + $this->assertResponseStatusCodeSame(404); |
| 34 | + $this->assertStringContainsString('Swagger UI is disabled.', $client->getResponse()->getContent(false)); |
| 35 | + } |
| 36 | + |
| 37 | + public function testJsonDocumentationIsAccessibleWhenSwaggerUiIsDisabled(): void |
| 38 | + { |
| 39 | + $client = $this->createClientWithEnv('swagger_ui_disabled'); |
| 40 | + |
| 41 | + $client->request('GET', '/docs.jsonopenapi', ['headers' => ['Accept' => 'application/vnd.openapi+json']]); |
| 42 | + $this->assertResponseIsSuccessful(); |
| 43 | + $this->assertJsonContains(['openapi' => '3.1.0']); |
| 44 | + $this->assertJsonContains(['info' => ['title' => 'My Dummy API']]); |
| 45 | + } |
| 46 | + |
| 47 | + public function testHtmlDocumentationIsAccessibleWhenSwaggerUiIsEnabled(): void |
| 48 | + { |
| 49 | + $client = $this->createClientWithEnv('swagger_ui_enabled'); |
| 50 | + |
| 51 | + $client->request('GET', '/docs', ['headers' => ['Accept' => 'text/html']]); |
| 52 | + $this->assertResponseIsSuccessful(); |
| 53 | + $this->assertStringNotContainsString('Swagger UI is disabled.', $client->getResponse()->getContent(false)); |
| 54 | + } |
| 55 | + |
| 56 | + public function testJsonDocumentationIsAccessibleWhenSwaggerUiIsEnabled(): void |
| 57 | + { |
| 58 | + $client = $this->createClientWithEnv('swagger_ui_enabled'); |
| 59 | + |
| 60 | + $client->request('GET', '/docs.jsonopenapi', ['headers' => ['Accept' => 'application/vnd.openapi+json']]); |
| 61 | + $this->assertResponseIsSuccessful(); |
| 62 | + $this->assertJsonContains(['openapi' => '3.1.0']); |
| 63 | + $this->assertJsonContains(['info' => ['title' => 'My Dummy API']]); |
| 64 | + } |
| 65 | +} |
0 commit comments