19
19
use ApiPlatform \Core \Metadata \Property \PropertyNameCollection ;
20
20
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Dummy ;
21
21
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \RelatedDummy ;
22
+ use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \UnknownDummy ;
22
23
use Doctrine \ORM \EntityManager ;
23
24
use Doctrine \ORM \Mapping \ClassMetadata ;
24
25
use Doctrine \ORM \QueryBuilder ;
@@ -96,9 +97,10 @@ public function testApplyToItem()
96
97
{
97
98
$ propertyNameCollectionFactoryProphecy = $ this ->prophesize (PropertyNameCollectionFactoryInterface::class);
98
99
99
- $ relatedNameCollection = new PropertyNameCollection (['id ' , 'name ' , 'notindatabase ' , 'notreadable ' ]);
100
+ $ relatedNameCollection = new PropertyNameCollection (['id ' , 'name ' , 'notindatabase ' , 'notreadable ' , ' relation ' ]);
100
101
101
102
$ propertyNameCollectionFactoryProphecy ->create (RelatedDummy::class)->willReturn ($ relatedNameCollection )->shouldBeCalled ();
103
+ $ propertyNameCollectionFactoryProphecy ->create (UnknownDummy::class)->willReturn (new PropertyNameCollection (['id ' ]))->shouldBeCalled ();
102
104
103
105
$ propertyMetadataFactoryProphecy = $ this ->prophesize (PropertyMetadataFactoryInterface::class);
104
106
$ relationPropertyMetadata = new PropertyMetadata ();
@@ -122,16 +124,19 @@ public function testApplyToItem()
122
124
$ propertyMetadataFactoryProphecy ->create (RelatedDummy::class, 'name ' )->willReturn ($ namePropertyMetadata )->shouldBeCalled ();
123
125
$ propertyMetadataFactoryProphecy ->create (RelatedDummy::class, 'notindatabase ' )->willReturn ($ notInDatabasePropertyMetadata )->shouldBeCalled ();
124
126
$ propertyMetadataFactoryProphecy ->create (RelatedDummy::class, 'notreadable ' )->willReturn ($ notReadablePropertyMetadata )->shouldBeCalled ();
127
+ $ propertyMetadataFactoryProphecy ->create (RelatedDummy::class, 'relation ' )->willReturn ($ relationPropertyMetadata )->shouldBeCalled ();
128
+ $ propertyMetadataFactoryProphecy ->create (UnknownDummy::class, 'id ' )->willReturn ($ idPropertyMetadata )->shouldBeCalled ();
125
129
126
130
$ queryBuilderProphecy = $ this ->prophesize (QueryBuilder::class);
127
131
128
132
$ classMetadataProphecy = $ this ->prophesize (ClassMetadata::class);
129
- $ classMetadataProphecy ->getAssociationNames ()->shouldBeCalled ()->willReturn ([0 => 'relatedDummy ' , 'relatedDummy2 ' , 'relatedDummy3 ' , 'relatedDummy4 ' ]);
133
+ $ classMetadataProphecy ->getAssociationNames ()->shouldBeCalled ()->willReturn (['relatedDummy ' , 'relatedDummy2 ' , 'relatedDummy3 ' , 'relatedDummy4 ' ]);
130
134
$ classMetadataProphecy ->associationMappings = [
131
135
'relatedDummy ' => ['fetch ' => 3 , 'joinColumns ' => [['nullable ' => true ]], 'targetEntity ' => RelatedDummy::class],
132
- 'relatedDummy2 ' => ['fetch ' => 3 , 'joinColumns ' => [['nullable ' => false ]], 'targetEntity ' => RelatedDummy::class],
133
- 'relatedDummy3 ' => ['fetch ' => 3 , 'joinTable ' => ['joinColumns ' => [['nullable ' => false ]]], 'targetEntity ' => RelatedDummy::class],
134
- 'relatedDummy4 ' => ['fetch ' => 3 , 'targetEntity ' => RelatedDummy::class],
136
+ 'relatedDummy2 ' => ['fetch ' => 3 , 'joinColumns ' => [['nullable ' => false ]], 'targetEntity ' => UnknownDummy::class],
137
+ 'relatedDummy3 ' => ['fetch ' => 3 , 'joinTable ' => ['joinColumns ' => [['nullable ' => false ]]], 'targetEntity ' => UnknownDummy::class],
138
+ 'relatedDummy4 ' => ['fetch ' => 3 , 'targetEntity ' => UnknownDummy::class],
139
+ 'relatedDummy5 ' => ['fetch ' => 2 , 'targetEntity ' => UnknownDummy::class],
135
140
];
136
141
137
142
$ relatedClassMetadataProphecy = $ this ->prophesize (ClassMetadata::class);
@@ -142,26 +147,35 @@ public function testApplyToItem()
142
147
}
143
148
}
144
149
145
- $ relatedClassMetadataProphecy ->getAssociationNames ()->shouldBeCalled ()->willReturn ([]);
150
+ $ relatedClassMetadataProphecy ->getAssociationNames ()->shouldBeCalled ()->willReturn (['relation ' ]);
151
+
152
+ $ relatedClassMetadataProphecy ->associationMappings = [
153
+ 'relation ' => ['fetch ' => 3 , 'joinColumns ' => [['nullable ' => false ]], 'targetEntity ' => UnknownDummy::class],
154
+ ];
155
+
156
+ $ unknownClassMetadataProphecy = $ this ->prophesize (ClassMetadata::class);
157
+ $ unknownClassMetadataProphecy ->getAssociationNames ()->shouldBeCalled ()->willReturn ([]);
146
158
147
159
$ emProphecy = $ this ->prophesize (EntityManager::class);
148
160
$ emProphecy ->getClassMetadata (Dummy::class)->shouldBeCalled ()->willReturn ($ classMetadataProphecy ->reveal ());
149
161
$ emProphecy ->getClassMetadata (RelatedDummy::class)->shouldBeCalled ()->willReturn ($ relatedClassMetadataProphecy ->reveal ());
162
+ $ emProphecy ->getClassMetadata (UnknownDummy::class)->shouldBeCalled ()->willReturn ($ unknownClassMetadataProphecy ->reveal ());
150
163
151
164
$ queryBuilderProphecy ->leftJoin ('o.relatedDummy ' , 'a0 ' )->shouldBeCalled (1 );
152
- $ queryBuilderProphecy ->innerJoin ('o.relatedDummy2 ' , 'a11 ' )->shouldBeCalled (1 );
153
- $ queryBuilderProphecy ->innerJoin ('o.relatedDummy3 ' , 'a122 ' )->shouldBeCalled (1 );
154
- $ queryBuilderProphecy ->leftJoin ('o.relatedDummy4 ' , 'a1233 ' )->shouldBeCalled (1 );
165
+ $ queryBuilderProphecy ->leftJoin ('a0.relation ' , 'a10 ' )->shouldBeCalled (1 );
166
+ $ queryBuilderProphecy ->innerJoin ('o.relatedDummy2 ' , 'a111 ' )->shouldBeCalled (1 );
167
+ $ queryBuilderProphecy ->innerJoin ('o.relatedDummy3 ' , 'a1122 ' )->shouldBeCalled (1 );
168
+ $ queryBuilderProphecy ->leftJoin ('o.relatedDummy4 ' , 'a11233 ' )->shouldBeCalled (1 );
155
169
$ queryBuilderProphecy ->addSelect ('partial a0.{id,name} ' )->shouldBeCalled (1 );
156
- $ queryBuilderProphecy ->addSelect ('partial a11.{id,name} ' )->shouldBeCalled (1 );
157
- $ queryBuilderProphecy ->addSelect ('partial a122.{id,name} ' )->shouldBeCalled (1 );
158
- $ queryBuilderProphecy ->addSelect ('partial a1233.{id,name} ' )->shouldBeCalled (1 );
170
+ $ queryBuilderProphecy ->addSelect ('partial a10.{id} ' )->shouldBeCalled (1 );
171
+ $ queryBuilderProphecy ->addSelect ('partial a111.{id} ' )->shouldBeCalled (1 );
172
+ $ queryBuilderProphecy ->addSelect ('partial a1122.{id} ' )->shouldBeCalled (1 );
173
+ $ queryBuilderProphecy ->addSelect ('partial a11233.{id} ' )->shouldBeCalled (1 );
159
174
160
175
$ queryBuilderProphecy ->getEntityManager ()->shouldBeCalled (2 )->willReturn ($ emProphecy ->reveal ());
161
176
162
177
$ queryBuilder = $ queryBuilderProphecy ->reveal ();
163
178
$ orderExtensionTest = new EagerLoadingExtension ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
164
- $ orderExtensionTest ->applyToCollection ($ queryBuilder , new QueryNameGenerator (), Dummy::class);
165
179
166
180
$ orderExtensionTest ->applyToItem ($ queryBuilder , new QueryNameGenerator (), Dummy::class, []);
167
181
}
0 commit comments