/
ApiTestCaseTest.php
223 lines (188 loc) · 7.26 KB
/
ApiTestCaseTest.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?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\Core\Tests\Bridge\Symfony\Bundle\Test;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Dummy as DummyDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoInputOutput;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Model\ResourceInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Runner\Version;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\HttpClient\CurlHttpClient;
class ApiTestCaseTest extends ApiTestCase
{
use ExpectDeprecationTrait;
/**
* TODO: remove the deprecation HttpClientInterface::withOptions will be available in symfony:^5.3.
*
* @group legacy
*/
public function testAssertJsonContains(): void
{
if (version_compare(Version::id(), '8.0.0', '<')) {
$this->markTestSkipped('Requires PHPUnit 8');
}
$client = self::createClient();
if (!method_exists(CurlHttpClient::class, 'withOptions')) {
$this->expectDeprecation(sprintf('Class %s should implement method "Symfony\Contracts\HttpClient\HttpClientInterface::withOptions(array $options)": Returns a new instance of the client with new default options.', get_class($client)));
}
$client->request('GET', '/');
$this->assertJsonContains(['@context' => '/contexts/Entrypoint']);
}
public function testAssertJsonContainsWithJsonObjectString(): void
{
if (version_compare(Version::id(), '8.0.0', '<')) {
$this->markTestSkipped('Requires PHPUnit 8');
}
self::createClient()->request('GET', '/');
$this->assertJsonContains(<<<JSON
{
"@context": "/contexts/Entrypoint"
}
JSON
);
}
public function testAssertJsonContainsWithJsonScalarString(): void
{
if (version_compare(Version::id(), '8.0.0', '<')) {
$this->markTestSkipped('Requires PHPUnit 8');
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('$subset must be array or string (JSON array or JSON object)');
self::createClient()->request('GET', '/');
$this->assertJsonContains(<<<JSON
"/contexts/Entrypoint"
JSON
);
}
public function testAssertJsonEquals(): void
{
self::createClient()->request('GET', '/contexts/Address');
$this->assertJsonEquals([
'@context' => [
'@vocab' => 'http://example.com/docs.jsonld#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
'name' => 'Address/name',
],
]);
}
public function testAssertJsonEqualsWithJsonObjectString(): void
{
self::createClient()->request('GET', '/contexts/Address');
$this->assertJsonEquals(<<<JSON
{
"@context": {
"@vocab": "http://example.com/docs.jsonld#",
"hydra": "http://www.w3.org/ns/hydra/core#",
"name": "Address/name"
}
}
JSON
);
}
public function testAssertJsonEqualsWithJsonScalarString(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('$json must be array or string (JSON array or JSON object)');
self::createClient()->request('GET', '/contexts/Address');
$this->assertJsonEquals(<<<JSON
"Address/name"
JSON
);
}
public function testAssertMatchesJsonSchema(): void
{
$jsonSchema = <<<JSON
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/Entrypoint"},
"@id": {"pattern": "^/$"},
"@type": {"pattern": "^Entrypoint$"},
"dummy": {}
},
"additionalProperties": true,
"required": ["@context", "@id", "@type", "dummy"]
}
JSON;
self::createClient()->request('GET', '/');
$this->assertMatchesJsonSchema($jsonSchema);
$this->assertMatchesJsonSchema(json_decode($jsonSchema, true));
}
public function testAssertMatchesResourceCollectionJsonSchema(): void
{
self::createClient()->request('GET', '/resource_interfaces');
$this->assertMatchesResourceCollectionJsonSchema(ResourceInterface::class);
}
public function testAssertMatchesResourceItemJsonSchema(): void
{
self::createClient()->request('GET', '/resource_interfaces/some-id');
$this->assertMatchesResourceItemJsonSchema(ResourceInterface::class);
}
public function testAssertMatchesResourceItemJsonSchemaOutput(): void
{
$this->recreateSchema();
/** @var EntityManagerInterface $manager */
$manager = self::$container->get('doctrine')->getManager();
$dummyDtoInputOutput = new DummyDtoInputOutput();
$dummyDtoInputOutput->str = 'lorem';
$dummyDtoInputOutput->num = 54;
$manager->persist($dummyDtoInputOutput);
$manager->flush();
self::createClient()->request('GET', '/dummy_dto_input_outputs/1');
$this->assertMatchesResourceItemJsonSchema(DummyDtoInputOutput::class);
}
// Next tests have been imported from dms/phpunit-arraysubset-asserts, because the original constraint has been deprecated.
public function testAssertArraySubsetPassesStrictConfig(): void
{
if (version_compare(Version::id(), '8.0.0', '<')) {
$this->markTestSkipped('Requires PHPUnit 8');
}
$this->expectException(ExpectationFailedException::class);
$this->assertArraySubset(['bar' => 0], ['bar' => '0'], true);
}
public function testAssertArraySubsetDoesNothingForValidScenario(): void
{
if (version_compare(Version::id(), '8.0.0', '<')) {
$this->markTestSkipped('Requires PHPUnit 8');
}
$this->assertArraySubset([1, 2], [1, 2, 3]);
}
public function testFindIriBy(): void
{
$this->recreateSchema();
self::createClient()->request('POST', '/dummies', [
'headers' => [
'content-type' => 'application/json',
'accept' => 'text/xml',
],
'body' => '{"name": "Kevin"}',
]);
$this->assertResponseIsSuccessful();
$resource = 'mongodb' === self::$container->getParameter('kernel.environment') ? DummyDocument::class : Dummy::class;
$this->assertMatchesRegularExpression('~^/dummies/\d+~', self::findIriBy($resource, ['name' => 'Kevin']));
$this->assertNull(self::findIriBy($resource, ['name' => 'not-exist']));
}
private function recreateSchema(): void
{
self::bootKernel();
/** @var EntityManagerInterface $manager */
$manager = self::$container->get('doctrine')->getManager();
/** @var \Doctrine\ORM\Mapping\ClassMetadata[] $classes */
$classes = $manager->getMetadataFactory()->getAllMetadata();
$schemaTool = new SchemaTool($manager);
$schemaTool->dropSchema($classes);
$schemaTool->createSchema($classes);
}
}