-
-
Notifications
You must be signed in to change notification settings - Fork 901
/
Copy pathOpenApiCommandTest.php
159 lines (129 loc) · 4.85 KB
/
OpenApiCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Tests\Symfony\Bundle\Command;
use ApiPlatform\OpenApi\OpenApi;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
/**
* @author Amrouche Hamza <[email protected]>
*
* TODO Remove group legacy in 4.0
*
* @group legacy
*/
class OpenApiCommandTest extends KernelTestCase
{
use ExpectDeprecationTrait;
private ApplicationTester $tester;
protected function setUp(): void
{
self::bootKernel();
$application = new Application(static::$kernel);
$application->setCatchExceptions(false);
$application->setAutoExit(false);
$this->tester = new ApplicationTester($application);
$this->handleDeprecations();
}
public function testExecute(): void
{
$this->tester->run(['command' => 'api:openapi:export']);
$this->assertJson($this->tester->getDisplay());
}
public function testExecuteWithYaml(): void
{
$this->tester->run(['command' => 'api:openapi:export', '--yaml' => true]);
$result = $this->tester->getDisplay();
$this->assertYaml($result);
$operationId = 'api_dummy_cars_get_collection';
$expected = <<<YAML
/dummy_cars:
get:
operationId: $operationId
tags:
- DummyCar
YAML;
$this->assertStringContainsString(str_replace(\PHP_EOL, "\n", $expected), $result, 'nested object should be present.');
$operationId = 'api_dummy_cars_id_get';
$expected = <<<YAML
'/dummy_cars/{id}':
get:
operationId: $operationId
tags: []
YAML;
$this->assertStringContainsString(str_replace(\PHP_EOL, "\n", $expected), $result, 'arrays should be correctly formatted.');
$this->assertStringContainsString('openapi: '.OpenApi::VERSION, $result);
$expected = <<<YAML
info:
title: 'My Dummy API'
YAML;
$this->assertStringContainsString(str_replace(\PHP_EOL, "\n", $expected), $result, 'multiline formatting must be preserved (using literal style).');
$expected = <<<YAML
This is a test API.
Made with love
version: 0.0.0
YAML;
$this->assertStringContainsString(str_replace(\PHP_EOL, "\n", $expected), $result);
$expected = <<<YAML
security:
-
JWT:
- CURRENCY_READ
YAML;
$this->assertStringContainsString(str_replace(\PHP_EOL, "\n", $expected), $result);
}
public function testWriteToFile(): void
{
/** @var string $tmpFile */
$tmpFile = tempnam(sys_get_temp_dir(), 'test_write_to_file');
$this->tester->run(['command' => 'api:openapi:export', '--output' => $tmpFile]);
$this->assertJson((string) @file_get_contents($tmpFile));
@unlink($tmpFile);
}
/**
* Test issue #6317.
*/
public function testBackedEnumExamplesAreNotLost(): void
{
$this->tester->run(['command' => 'api:openapi:export']);
$result = $this->tester->getDisplay();
$json = json_decode($result, true, 512, \JSON_THROW_ON_ERROR);
$assertExample = function (array $properties, string $id): void {
$this->assertArrayHasKey('example', $properties[$id]); // default
$this->assertArrayHasKey('example', $properties['cardinal']); // openapiContext
$this->assertArrayNotHasKey('example', $properties['name']); // jsonSchemaContext
$this->assertArrayNotHasKey('example', $properties['ordinal']); // jsonldContext
};
$assertExample($json['components']['schemas']['Issue6317']['properties'], 'id');
$assertExample($json['components']['schemas']['Issue6317.jsonld.output']['properties'], 'id');
$assertExample($json['components']['schemas']['Issue6317.jsonapi']['properties']['data']['properties']['attributes']['properties'], '_id');
$assertExample($json['components']['schemas']['Issue6317.jsonhal']['properties'], 'id');
}
private function assertYaml(string $data): void
{
try {
Yaml::parse($data);
} catch (ParseException $exception) {
$this->fail('Is not valid YAML: '.$exception->getMessage());
}
$this->addToAssertionCount(1);
}
/**
* TODO Remove in 4.0.
*/
private function handleDeprecations(): void
{
$this->expectDeprecation('Since api-platform/core 3.1: The "%s" option is deprecated, use "openapi" instead.');
}
}