21
21
use ApiPlatform \Core \Metadata \Property \PropertyNameCollection ;
22
22
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Dummy ;
23
23
use Doctrine \Common \Persistence \ManagerRegistry ;
24
- use Doctrine \Common \Persistence \ObjectManager ;
25
24
use Doctrine \Common \Persistence \ObjectRepository ;
25
+ use Doctrine \DBAL \Connection ;
26
+ use Doctrine \DBAL \Platforms \AbstractPlatform ;
27
+ use Doctrine \DBAL \Types \Type as DBALType ;
26
28
use Doctrine \ORM \AbstractQuery ;
29
+ use Doctrine \ORM \EntityManagerInterface ;
27
30
use Doctrine \ORM \EntityRepository ;
28
31
use Doctrine \ORM \Mapping \ClassMetadata ;
29
32
use Doctrine \ORM \Query \Expr ;
36
39
*/
37
40
class ItemDataProviderTest extends \PHPUnit_Framework_TestCase
38
41
{
39
- private function getManagerRegistryProphecy (QueryBuilder $ queryBuilder , array $ identifiers , string $ resourceClass )
40
- {
41
- $ classMetadataProphecy = $ this ->prophesize (ClassMetadata::class);
42
- $ classMetadataProphecy ->getIdentifier ()->willReturn ($ identifiers )->shouldBeCalled ();
43
-
44
- $ repositoryProphecy = $ this ->prophesize (EntityRepository::class);
45
- $ repositoryProphecy ->createQueryBuilder ('o ' )->willReturn ($ queryBuilder )->shouldBeCalled ();
46
-
47
- $ managerProphecy = $ this ->prophesize (ObjectManager::class);
48
- $ managerProphecy ->getClassMetadata ($ resourceClass )->shouldBeCalled ()->willReturn ($ classMetadataProphecy ->reveal ());
49
- $ managerProphecy ->getRepository ($ resourceClass )->willReturn ($ repositoryProphecy ->reveal ())->shouldBeCalled ();
50
-
51
- $ managerRegistryProphecy = $ this ->prophesize (ManagerRegistry::class);
52
- $ managerRegistryProphecy ->getManagerForClass (Dummy::class)->willReturn ($ managerProphecy ->reveal ())->shouldBeCalled ();
53
-
54
- return $ managerRegistryProphecy ->reveal ();
55
- }
56
-
57
- private function getMetadataProphecies (array $ identifiers , string $ resourceClass )
58
- {
59
- $ propertyNameCollectionFactoryProphecy = $ this ->prophesize (PropertyNameCollectionFactoryInterface::class);
60
- $ propertyMetadataFactoryProphecy = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
61
-
62
- $ nameCollection = ['foobar ' ];
63
-
64
- foreach ($ identifiers as $ identifier ) {
65
- $ metadata = new PropertyMetadata ();
66
- $ metadata = $ metadata ->withIdentifier (true );
67
- $ propertyMetadataFactoryProphecy ->create ($ resourceClass , $ identifier )->willReturn ($ metadata );
68
-
69
- $ nameCollection [] = $ identifier ;
70
- }
71
-
72
- //random property to prevent the use of non-identifiers metadata while looping
73
- $ propertyMetadataFactoryProphecy ->create ($ resourceClass , 'foobar ' )->willReturn (new PropertyMetadata ());
74
-
75
- $ propertyNameCollectionFactoryProphecy ->create ($ resourceClass )->willReturn (new PropertyNameCollection ($ nameCollection ));
76
-
77
- return [$ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ()];
78
- }
79
-
80
42
public function testGetItemSingleIdentifier ()
81
43
{
82
44
$ context = ['foo ' => 'bar ' , 'fetch_data ' => true ];
@@ -97,9 +59,14 @@ public function testGetItemSingleIdentifier()
97
59
98
60
$ queryBuilder = $ queryBuilderProphecy ->reveal ();
99
61
100
- $ identifiers = ['id ' ];
101
- list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataProphecies ($ identifiers , Dummy::class);
102
- $ managerRegistry = $ this ->getManagerRegistryProphecy ($ queryBuilder , $ identifiers , Dummy::class);
62
+ list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataFactories (Dummy::class, [
63
+ 'id ' ,
64
+ ]);
65
+ $ managerRegistry = $ this ->getManagerRegistry (Dummy::class, [
66
+ 'id ' => [
67
+ 'type ' => DBALType::INTEGER ,
68
+ ],
69
+ ], $ queryBuilder );
103
70
104
71
$ extensionProphecy = $ this ->prophesize (QueryItemExtensionInterface::class);
105
72
$ extensionProphecy ->applyToItem ($ queryBuilder , Argument::type (QueryNameGeneratorInterface::class), Dummy::class, ['id ' => 1 ], 'foo ' , $ context )->shouldBeCalled ();
@@ -131,9 +98,18 @@ public function testGetItemDoubleIdentifier()
131
98
132
99
$ queryBuilder = $ queryBuilderProphecy ->reveal ();
133
100
134
- $ identifiers = ['ida ' , 'idb ' ];
135
- list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataProphecies ($ identifiers , Dummy::class);
136
- $ managerRegistry = $ this ->getManagerRegistryProphecy ($ queryBuilder , $ identifiers , Dummy::class);
101
+ list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataFactories (Dummy::class, [
102
+ 'ida ' ,
103
+ 'idb ' ,
104
+ ]);
105
+ $ managerRegistry = $ this ->getManagerRegistry (Dummy::class, [
106
+ 'ida ' => [
107
+ 'type ' => DBALType::INTEGER ,
108
+ ],
109
+ 'idb ' => [
110
+ 'type ' => DBALType::INTEGER ,
111
+ ],
112
+ ], $ queryBuilder );
137
113
138
114
$ extensionProphecy = $ this ->prophesize (QueryItemExtensionInterface::class);
139
115
$ extensionProphecy ->applyToItem ($ queryBuilder , Argument::type (QueryNameGeneratorInterface::class), Dummy::class, ['ida ' => 1 , 'idb ' => 2 ], 'foo ' , [])->shouldBeCalled ();
@@ -158,9 +134,14 @@ public function testQueryResultExtension()
158
134
159
135
$ queryBuilder = $ queryBuilderProphecy ->reveal ();
160
136
161
- $ identifiers = ['id ' ];
162
- list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataProphecies ($ identifiers , Dummy::class);
163
- $ managerRegistry = $ this ->getManagerRegistryProphecy ($ queryBuilder , $ identifiers , Dummy::class);
137
+ list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataFactories (Dummy::class, [
138
+ 'id ' ,
139
+ ]);
140
+ $ managerRegistry = $ this ->getManagerRegistry (Dummy::class, [
141
+ 'id ' => [
142
+ 'type ' => DBALType::INTEGER ,
143
+ ],
144
+ ], $ queryBuilder );
164
145
165
146
$ extensionProphecy = $ this ->prophesize (QueryResultItemExtensionInterface::class);
166
147
$ extensionProphecy ->applyToItem ($ queryBuilder , Argument::type (QueryNameGeneratorInterface::class), Dummy::class, ['id ' => 1 ], 'foo ' , [])->shouldBeCalled ();
@@ -182,8 +163,9 @@ public function testThrowResourceClassNotSupportedException()
182
163
183
164
$ extensionProphecy = $ this ->prophesize (QueryItemExtensionInterface::class);
184
165
185
- $ identifiers = ['id ' ];
186
- list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataProphecies ($ identifiers , Dummy::class);
166
+ list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataFactories (Dummy::class, [
167
+ 'id ' ,
168
+ ]);
187
169
188
170
$ dataProvider = new ItemDataProvider ($ managerRegistryProphecy ->reveal (), $ propertyNameCollectionFactory , $ propertyMetadataFactory , [$ extensionProphecy ->reveal ()]);
189
171
$ dataProvider ->getItem (Dummy::class, 'foo ' );
@@ -195,22 +177,100 @@ public function testThrowResourceClassNotSupportedException()
195
177
*/
196
178
public function testCannotCreateQueryBuilder ()
197
179
{
198
- $ identifiers = ['id ' ];
199
-
200
180
$ repositoryProphecy = $ this ->prophesize (ObjectRepository::class);
201
181
$ classMetadataProphecy = $ this ->prophesize (ClassMetadata::class);
202
- $ classMetadataProphecy ->getIdentifier ()->willReturn ($ identifiers )->shouldBeCalled ();
203
- $ managerProphecy = $ this ->prophesize (ObjectManager::class);
204
- $ managerProphecy ->getClassMetadata (Dummy::class)->shouldBeCalled ()->willReturn ($ classMetadataProphecy ->reveal ());
205
- $ managerProphecy ->getRepository (Dummy::class)->willReturn ($ repositoryProphecy ->reveal ())->shouldBeCalled ();
182
+ $ classMetadataProphecy ->getIdentifier ()->willReturn ([
183
+ 'id ' ,
184
+ ]);
185
+ $ classMetadataProphecy ->getTypeOfField ('id ' )->willReturn (DBALType::INTEGER );
186
+
187
+ $ platformProphecy = $ this ->prophesize (AbstractPlatform::class);
188
+
189
+ $ connectionProphecy = $ this ->prophesize (Connection::class);
190
+ $ connectionProphecy ->getDatabasePlatform ()->willReturn ($ platformProphecy );
191
+
192
+ $ managerProphecy = $ this ->prophesize (EntityManagerInterface::class);
193
+ $ managerProphecy ->getClassMetadata (Dummy::class)->willReturn ($ classMetadataProphecy ->reveal ());
194
+ $ managerProphecy ->getConnection ()->willReturn ($ connectionProphecy );
195
+ $ managerProphecy ->getRepository (Dummy::class)->willReturn ($ repositoryProphecy ->reveal ());
206
196
207
197
$ managerRegistryProphecy = $ this ->prophesize (ManagerRegistry::class);
208
- $ managerRegistryProphecy ->getManagerForClass (Dummy::class)->willReturn ($ managerProphecy ->reveal ())-> shouldBeCalled () ;
198
+ $ managerRegistryProphecy ->getManagerForClass (Dummy::class)->willReturn ($ managerProphecy ->reveal ());
209
199
210
200
$ extensionProphecy = $ this ->prophesize (QueryItemExtensionInterface::class);
211
201
212
- list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataProphecies ($ identifiers , Dummy::class);
202
+ list ($ propertyNameCollectionFactory , $ propertyMetadataFactory ) = $ this ->getMetadataFactories (Dummy::class, [
203
+ 'id ' ,
204
+ ]);
213
205
214
206
(new ItemDataProvider ($ managerRegistryProphecy ->reveal (), $ propertyNameCollectionFactory , $ propertyMetadataFactory , [$ extensionProphecy ->reveal ()]))->getItem (Dummy::class, 'foo ' );
215
207
}
208
+
209
+ /**
210
+ * Gets mocked metadata factories.
211
+ *
212
+ * @param string $resourceClass
213
+ * @param array $identifiers
214
+ *
215
+ * @return array
216
+ */
217
+ private function getMetadataFactories (string $ resourceClass , array $ identifiers ): array
218
+ {
219
+ $ propertyNameCollectionFactoryProphecy = $ this ->prophesize (PropertyNameCollectionFactoryInterface::class);
220
+ $ propertyMetadataFactoryProphecy = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
221
+
222
+ $ nameCollection = ['foobar ' ];
223
+
224
+ foreach ($ identifiers as $ identifier ) {
225
+ $ metadata = new PropertyMetadata ();
226
+ $ metadata = $ metadata ->withIdentifier (true );
227
+ $ propertyMetadataFactoryProphecy ->create ($ resourceClass , $ identifier )->willReturn ($ metadata );
228
+
229
+ $ nameCollection [] = $ identifier ;
230
+ }
231
+
232
+ //random property to prevent the use of non-identifiers metadata while looping
233
+ $ propertyMetadataFactoryProphecy ->create ($ resourceClass , 'foobar ' )->willReturn (new PropertyMetadata ());
234
+
235
+ $ propertyNameCollectionFactoryProphecy ->create ($ resourceClass )->willReturn (new PropertyNameCollection ($ nameCollection ));
236
+
237
+ return [$ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ()];
238
+ }
239
+
240
+ /**
241
+ * Gets a mocked manager registry.
242
+ *
243
+ * @param string $resourceClass
244
+ * @param array $identifierFields
245
+ * @param QueryBuilder $queryBuilder
246
+ *
247
+ * @return ManagerRegistry
248
+ */
249
+ private function getManagerRegistry (string $ resourceClass , array $ identifierFields , QueryBuilder $ queryBuilder ): ManagerRegistry
250
+ {
251
+ $ classMetadataProphecy = $ this ->prophesize (ClassMetadata::class);
252
+ $ classMetadataProphecy ->getIdentifier ()->willReturn (array_keys ($ identifierFields ));
253
+
254
+ foreach ($ identifierFields as $ name => $ field ) {
255
+ $ classMetadataProphecy ->getTypeOfField ($ name )->willReturn ($ field ['type ' ]);
256
+ }
257
+
258
+ $ platformProphecy = $ this ->prophesize (AbstractPlatform::class);
259
+
260
+ $ connectionProphecy = $ this ->prophesize (Connection::class);
261
+ $ connectionProphecy ->getDatabasePlatform ()->willReturn ($ platformProphecy );
262
+
263
+ $ repositoryProphecy = $ this ->prophesize (EntityRepository::class);
264
+ $ repositoryProphecy ->createQueryBuilder ('o ' )->willReturn ($ queryBuilder );
265
+
266
+ $ managerProphecy = $ this ->prophesize (EntityManagerInterface::class);
267
+ $ managerProphecy ->getClassMetadata ($ resourceClass )->willReturn ($ classMetadataProphecy ->reveal ());
268
+ $ managerProphecy ->getConnection ()->willReturn ($ connectionProphecy );
269
+ $ managerProphecy ->getRepository ($ resourceClass )->willReturn ($ repositoryProphecy ->reveal ());
270
+
271
+ $ managerRegistryProphecy = $ this ->prophesize (ManagerRegistry::class);
272
+ $ managerRegistryProphecy ->getManagerForClass (Dummy::class)->willReturn ($ managerProphecy ->reveal ());
273
+
274
+ return $ managerRegistryProphecy ->reveal ();
275
+ }
216
276
}
0 commit comments