Skip to content

Commit 9201e57

Browse files
committed
test: add tests in tests/Functional/DocumentationActionTest.php
1 parent 0836b83 commit 9201e57

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
imports:
2+
- { resource: config_test.yml }
3+
4+
api_platform:
5+
enable_swagger: true
6+
enable_swagger_ui: false
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
imports:
2+
- { resource: config_test.yml }
3+
4+
api_platform:
5+
enable_swagger: true
6+
enable_swagger_ui: true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_main:
2+
resource: routing_test.yml
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_main:
2+
resource: routing_test.yml
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)