-
-
Notifications
You must be signed in to change notification settings - Fork 838
/
IdentifiersExtractorTest.php
256 lines (196 loc) · 12.1 KB
/
IdentifiersExtractorTest.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?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\Api;
use ApiPlatform\Core\Api\IdentifiersExtractor;
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Doctrine\Generator\Uuid;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Model\ResourceInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Model\ResourceInterfaceImplementation;
use ApiPlatform\Core\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
/**
* @author Antoine Bluchet <soyuka@gmail.com>
*/
class IdentifiersExtractorTest extends TestCase
{
use ProphecyTrait;
public function testGetIdentifiersFromResourceClass()
{
[$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy] = $this->getMetadataFactoryProphecies(Dummy::class, ['id']);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
$this->assertSame(['id'], $identifiersExtractor->getIdentifiersFromResourceClass(Dummy::class));
}
public function testGetCompositeIdentifiersFromResourceClass()
{
[$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy] = $this->getMetadataFactoryProphecies(Dummy::class, ['id', 'name']);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
$this->assertSame(['id', 'name'], $identifiersExtractor->getIdentifiersFromResourceClass(Dummy::class));
}
public function itemProvider()
{
$dummy = new Dummy();
$dummy->setId(1);
yield [$dummy, ['id' => 1]];
$uuid = new Uuid();
$dummy = new Dummy();
$dummy->setId($uuid);
yield [$dummy, ['id' => $uuid]];
}
/**
* @dataProvider itemProvider
*/
public function testGetIdentifiersFromItem($item, $expected)
{
[$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy] = $this->getMetadataFactoryProphecies(Dummy::class, ['id']);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($item)->willReturn(Dummy::class);
$resourceClassResolverProphecy->isResourceClass(Uuid::class)->willReturn(false);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
$this->assertSame($expected, $identifiersExtractor->getIdentifiersFromItem($item));
}
public function itemProviderComposite()
{
$dummy = new Dummy();
$dummy->setId(1);
$dummy->setName('foo');
yield [$dummy, ['id' => 1, 'name' => 'foo']];
$dummy = new Dummy();
$dummy->setId($uuid = new Uuid());
$dummy->setName('foo');
yield [$dummy, ['id' => $uuid, 'name' => 'foo']];
}
/**
* @dataProvider itemProviderComposite
*/
public function testGetCompositeIdentifiersFromItem($item, $expected)
{
[$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy] = $this->getMetadataFactoryProphecies(Dummy::class, ['id', 'name']);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($item)->willReturn(Dummy::class);
$resourceClassResolverProphecy->isResourceClass(Uuid::class)->willReturn(false);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
$this->assertSame($expected, $identifiersExtractor->getIdentifiersFromItem($item));
}
public function itemProviderRelated()
{
$related = new RelatedDummy();
$related->setId(2);
$dummy = new Dummy();
$dummy->setId(1);
$dummy->setRelatedDummy($related);
yield [$dummy, ['id' => 1, 'relatedDummy' => 2]];
$uuid2 = new Uuid();
$related = new RelatedDummy();
$related->setId($uuid2);
$uuid = new Uuid();
$dummy = new Dummy();
$dummy->setId($uuid);
$dummy->setRelatedDummy($related);
yield [$dummy, ['id' => $uuid, 'relatedDummy' => $uuid2]];
}
/**
* @dataProvider itemProviderRelated
*/
public function testGetRelatedIdentifiersFromItem($item, $expected)
{
$prophecies = $this->getMetadataFactoryProphecies(Dummy::class, ['id', 'relatedDummy']);
[$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy] = $this->getMetadataFactoryProphecies(RelatedDummy::class, ['id'], $prophecies);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($item)->willReturn(Dummy::class);
$resourceClassResolverProphecy->getResourceClass(Argument::type(RelatedDummy::class))->willReturn(RelatedDummy::class);
$resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
$resourceClassResolverProphecy->isResourceClass(Uuid::class)->willReturn(false);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
$this->assertSame($expected, $identifiersExtractor->getIdentifiersFromItem($item));
}
public function testThrowNoIdentifierFromItem()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('No identifier found in "ApiPlatform\\Core\\Tests\\Fixtures\\TestBundle\\Entity\\RelatedDummy" through relation "relatedDummy" of "ApiPlatform\\Core\\Tests\\Fixtures\\TestBundle\\Entity\\Dummy" used as identifier');
$related = new RelatedDummy();
$related->setId(2);
$dummy = new Dummy();
$dummy->setId(1);
$dummy->setRelatedDummy($related);
$prophecies = $this->getMetadataFactoryProphecies(Dummy::class, ['id', 'relatedDummy']);
[$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy] = $this->getMetadataFactoryProphecies(RelatedDummy::class, [], $prophecies);
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($dummy)->willReturn(Dummy::class);
$resourceClassResolverProphecy->getResourceClass(Argument::type(RelatedDummy::class))->willReturn(RelatedDummy::class);
$resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
$identifiersExtractor->getIdentifiersFromItem($dummy);
}
public function testGetsIdentifiersFromCorrectResourceClass(): void
{
$item = new ResourceInterfaceImplementation();
$item->setFoo('woot');
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(ResourceInterface::class)->willReturn(new PropertyNameCollection(['foo', 'fooz']));
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(ResourceInterface::class, 'foo')->willReturn((new PropertyMetadata())->withIdentifier(true));
$propertyMetadataFactoryProphecy->create(ResourceInterface::class, 'fooz')->willReturn(new PropertyMetadata());
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass($item)->willReturn(ResourceInterface::class);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
$identifiersExtractor->getIdentifiersFromItem($item);
$this->assertSame(['foo' => 'woot'], $identifiersExtractor->getIdentifiersFromItem($item));
}
public function testNoIdentifiers(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('No identifier found in "ApiPlatform\\Core\\Tests\\Fixtures\\TestBundle\\Entity\\Dummy"');
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class)->willReturn(new PropertyNameCollection(['foo']));
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'foo')->willReturn(new PropertyMetadata());
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), null, $resourceClassResolverProphecy->reveal());
$identifiersExtractor->getIdentifiersFromResourceClass(Dummy::class);
}
/**
* @group legacy
* @expectedDeprecation Not injecting ApiPlatform\Core\Api\ResourceClassResolverInterface in the IdentifiersExtractor might introduce cache issues with object identifiers.
*/
public function testLegacyGetIdentifiersFromItem()
{
[$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy] = $this->getMetadataFactoryProphecies(Dummy::class, ['id']);
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal());
$dummy = new Dummy();
$dummy->setId(1);
$this->assertSame(['id' => 1], $identifiersExtractor->getIdentifiersFromItem($dummy));
}
private function getMetadataFactoryProphecies($class, $identifiers, array $prophecies = null)
{
//adds a random property that is not an identifier
$properties = array_merge(['foo'], $identifiers);
if (!$prophecies) {
$prophecies = [$this->prophesize(PropertyNameCollectionFactoryInterface::class), $this->prophesize(PropertyMetadataFactoryInterface::class)];
}
[$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy] = $prophecies;
$propertyNameCollectionFactoryProphecy->create($class)->willReturn(new PropertyNameCollection($properties));
foreach ($properties as $prop) {
$metadata = new PropertyMetadata();
$propertyMetadataFactoryProphecy->create($class, $prop)->willReturn($metadata->withIdentifier(\in_array($prop, $identifiers, true)));
}
return [$propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy];
}
}