-
-
Notifications
You must be signed in to change notification settings - Fork 863
/
JsonSchemaGenerateCommandTest.php
180 lines (145 loc) · 7.42 KB
/
JsonSchemaGenerateCommandTest.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\JsonSchema\Command;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\Dummy as DocumentDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\ApplicationTester;
/**
* @author Jacques Lefebvre <jacques@les-tilleuls.coop>
*/
class JsonSchemaGenerateCommandTest extends KernelTestCase
{
private ApplicationTester $tester;
private string $entityClass;
protected function setUp(): void
{
$kernel = self::bootKernel();
$application = new Application(static::$kernel);
$application->setCatchExceptions(true);
$application->setAutoExit(false);
$this->entityClass = 'mongodb' === $kernel->getEnvironment() ? DocumentDummy::class : Dummy::class;
$this->tester = new ApplicationTester($application);
}
public function testExecuteWithoutOption(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass]);
$this->assertJson($this->tester->getDisplay());
}
public function testExecuteWithItemOperationGet(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies/{id}{._format}_get', '--type' => 'output']);
$this->assertJson($this->tester->getDisplay());
}
public function testExecuteWithCollectionOperationGet(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies{._format}_get_collection', '--type' => 'output']);
$this->assertJson($this->tester->getDisplay());
}
public function testExecuteWithJsonldFormatOption(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies{._format}_post', '--format' => 'jsonld', '--type' => 'output']);
$result = $this->tester->getDisplay();
$this->assertStringContainsString('@id', $result);
$this->assertStringContainsString('@context', $result);
$this->assertStringContainsString('@type', $result);
}
public function testExecuteWithJsonldTypeInput(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => $this->entityClass, '--operation' => '_api_/dummies{._format}_post', '--format' => 'jsonld', '--type' => 'input']);
$result = $this->tester->getDisplay();
$this->assertStringNotContainsString('@id', $result);
$this->assertStringNotContainsString('@context', $result);
$this->assertStringNotContainsString('@type', $result);
}
/**
* Test issue #5501, the locations relation inside BrokenDocs is a Resource (named Related) but its only operation is a NotExposed.
* Still, serializer groups are set, and therefore it is a "readableLink" so we actually want to compute the schema, even if it's not accessible
* directly, it is accessible through that relation.
*/
public function testExecuteWithNotExposedResourceAndReadableLink(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5501\BrokenDocs', '--type' => 'output']);
$result = $this->tester->getDisplay();
$this->assertStringContainsString('Related.jsonld-location.read_collection', $result);
}
/**
* When serializer groups are present the Schema should have an embed resource. #5470 breaks array references when serializer groups are present.
*/
public function testArraySchemaWithReference(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5793\BagOfTests', '--type' => 'input']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['tests'], [
'type' => 'string',
'foo' => 'bar',
]);
$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['nonResourceTests'], [
'type' => 'array',
'items' => [
'$ref' => '#/definitions/NonResourceTestEntity.jsonld-write',
],
]);
$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['description'], [
'maxLength' => 255,
]);
$this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['type'], [
'$ref' => '#/definitions/TestEntity.jsonld-write',
]);
}
public function testArraySchemaWithMultipleUnionTypes(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6212\Nest', '--type' => 'output']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['Nest.jsonld']['properties']['owner']['anyOf'], [
['$ref' => '#/definitions/Wren.jsonld'],
['$ref' => '#/definitions/Robin.jsonld'],
['type' => 'null'],
]);
$this->assertArrayHasKey('Wren.jsonld', $json['definitions']);
$this->assertArrayHasKey('Robin.jsonld', $json['definitions']);
}
/**
* TODO: add deprecation (TypeFactory will be deprecated in api platform 3.3).
*
* @group legacy
*/
public function testArraySchemaWithTypeFactory(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5896\Foo', '--type' => 'output']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['Foo.jsonld']['properties']['expiration'], ['type' => 'string', 'format' => 'date']);
}
/**
* Test issue #5998.
*/
public function testWritableNonResourceRef(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5998\SaveProduct', '--type' => 'input']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertEquals($json['definitions']['SaveProduct.jsonld']['properties']['codes']['items']['$ref'], '#/definitions/ProductCode.jsonld');
}
/**
* Test related Schema keeps json-ld context.
*/
public function testSubSchemaJsonLd(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy', '--type' => 'output', '--format' => 'jsonld']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertArrayHasKey('@id', $json['definitions']['ThirdLevel.jsonld-friends']['properties']);
}
}