2121use ApiPlatform \Core \Bridge \Symfony \Bundle \DependencyInjection \Compiler \AnnotationFilterPass ;
2222use ApiPlatform \Core \Exception \InvalidArgumentException ;
2323use ApiPlatform \Core \Serializer \Filter \GroupFilter ;
24+ use ApiPlatform \Core \Tests \Fixtures \DummyFilterWithoutConstructor ;
2425use ApiPlatform \Core \Tests \Fixtures \TestBundle \Doctrine \Orm \Filter \AnotherDummyFilter ;
2526use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Dummy ;
2627use Doctrine \Common \Annotations \Reader ;
@@ -53,15 +54,20 @@ public function testProcess()
5354
5455 $ reader ->getPropertyAnnotations (Argument::type (\ReflectionProperty::class))->will (function ($ args ) {
5556 if (Dummy::class === $ args [0 ]->class && 'dummyDate ' === $ args [0 ]->name ) {
56- return [new ApiFilter (['value ' => DateFilter::class]), new ApiProperty ()];
57+ return [
58+ new ApiFilter (['value ' => DateFilter::class]),
59+ new ApiProperty (),
60+ ];
5761 }
5862
5963 return [];
6064 });
6165
6266 $ reader ->getClassAnnotations (Argument::type (\ReflectionClass::class))->will (function ($ args ) {
6367 if (Dummy::class === $ args [0 ]->name ) {
64- return [new ApiFilter (['value ' => SearchFilter::class, 'strategy ' => 'exact ' , 'properties ' => ['description ' , 'relatedDummy.name ' , 'name ' ]]), new ApiResource ([]), new ApiFilter (['value ' => GroupFilter::class, 'arguments ' => ['parameterName ' => 'foobar ' ]])];
68+ return [new ApiFilter (['value ' => SearchFilter::class, 'strategy ' => 'exact ' , 'properties ' => ['description ' , 'relatedDummy.name ' , 'name ' ]]), new ApiResource ([]),
69+ new ApiFilter (['value ' => GroupFilter::class, 'arguments ' => ['parameterName ' => 'foobar ' ]]),
70+ ];
6571 }
6672
6773 return [];
@@ -81,6 +87,7 @@ public function testProcess()
8187
8288 $ containerBuilderProphecy ->getReflectionClass (SearchFilter::class, false )->willReturn (new \ReflectionClass (SearchFilter::class))->shouldBeCalled ();
8389 $ containerBuilderProphecy ->getReflectionClass (GroupFilter::class, false )->willReturn (new \ReflectionClass (GroupFilter::class))->shouldBeCalled ();
90+ $ containerBuilderProphecy ->getReflectionClass (DateFilter::class, false )->willReturn (new \ReflectionClass (GroupFilter::class))->shouldBeCalled ();
8491
8592 $ containerBuilderProphecy ->setDefinition ('annotated_api_platform_core_tests_fixtures_test_bundle_entity_dummy_api_platform_core_bridge_doctrine_orm_filter_search_filter ' , Argument::that (function ($ def ) {
8693 $ this ->assertInstanceOf (Definition::class, $ def );
@@ -207,4 +214,54 @@ public function testProcessInvalidFilterClass()
207214
208215 $ annotationFilterPass ->process ($ containerBuilderProphecy ->reveal ());
209216 }
217+
218+ public function testProcessFilterWithoutConstructor ()
219+ {
220+ $ annotationFilterPass = new AnnotationFilterPass ();
221+
222+ $ this ->assertInstanceOf (CompilerPassInterface::class, $ annotationFilterPass );
223+
224+ $ containerBuilderProphecy = $ this ->prophesize (ContainerBuilder::class);
225+
226+ $ containerBuilderProphecy ->getParameter ('api_platform.resource_class_directories ' )->shouldBeCalled ()->willReturn ([
227+ __DIR__ .'/../../../../../Fixtures/TestBundle/Entity/ ' ,
228+ ]);
229+
230+ $ reader = $ this ->prophesize (Reader::class);
231+
232+ $ reader ->getPropertyAnnotations (Argument::type (\ReflectionProperty::class))->will (function ($ args ) {
233+ if (Dummy::class === $ args [0 ]->class && 'dummyDate ' === $ args [0 ]->name ) {
234+ return [new ApiProperty (), new ApiFilter (['value ' => DummyFilterWithoutConstructor::class])];
235+ }
236+
237+ return [];
238+ });
239+
240+ $ reader ->getClassAnnotations (Argument::type (\ReflectionClass::class))->will (function ($ args ) {
241+ if (Dummy::class === $ args [0 ]->name ) {
242+ return [new ApiResource ([])];
243+ }
244+
245+ return [];
246+ });
247+
248+ $ containerBuilderProphecy ->get ('annotation_reader ' )->shouldBeCalled ()->willReturn ($ reader ->reveal ());
249+
250+ $ containerBuilderProphecy ->has ('annotated_api_platform_core_tests_fixtures_test_bundle_entity_dummy_api_platform_core_tests_fixtures_dummy_filter_without_constructor ' )->shouldBeCalled ()->willReturn (false );
251+
252+ $ containerBuilderProphecy ->has (DummyFilterWithoutConstructor::class)->willReturn (true )->shouldBeCalled ();
253+
254+ $ containerBuilderProphecy ->getReflectionClass (DummyFilterWithoutConstructor::class, false )->willReturn (new \ReflectionClass (DummyFilterWithoutConstructor::class))->shouldBeCalled ();
255+ $ containerBuilderProphecy ->findDefinition (DummyFilterWithoutConstructor::class)->willReturn ((new Definition (DummyFilterWithoutConstructor::class))->setAbstract (false ))->shouldBeCalled ();
256+
257+ $ containerBuilderProphecy ->setDefinition ('annotated_api_platform_core_tests_fixtures_test_bundle_entity_dummy_api_platform_core_tests_fixtures_dummy_filter_without_constructor ' , Argument::that (function ($ def ) {
258+ $ this ->assertInstanceOf (Definition::class, $ def );
259+ $ this ->assertEquals (DummyFilterWithoutConstructor::class, $ def ->getClass ());
260+ $ this ->assertEquals ([], $ def ->getArguments ());
261+
262+ return true ;
263+ }))->shouldBeCalled ();
264+
265+ $ annotationFilterPass ->process ($ containerBuilderProphecy ->reveal ());
266+ }
210267}
0 commit comments