2222use ApiPlatform \Metadata \UrlGeneratorInterface ;
2323use ApiPlatform \Symfony \Doctrine \EventListener \PurgeHttpCacheListener ;
2424use ApiPlatform \Symfony \Tests \Fixtures \MappedEntity ;
25+ use ApiPlatform \Symfony \Tests \Fixtures \MappedResource ;
2526use ApiPlatform \Symfony \Tests \Fixtures \NotAResource ;
2627use ApiPlatform \Symfony \Tests \Fixtures \TestBundle \Entity \ContainNonResource ;
2728use ApiPlatform \Symfony \Tests \Fixtures \TestBundle \Entity \Dummy ;
3536use PHPUnit \Framework \TestCase ;
3637use Prophecy \Argument ;
3738use Prophecy \PhpUnit \ProphecyTrait ;
39+ use Symfony \Component \ObjectMapper \ObjectMapperInterface ;
3840use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
3941
4042/**
@@ -215,7 +217,8 @@ public function testNotAResourceClass(): void
215217 $ propertyAccessorProphecy ->getValue (Argument::type (ContainNonResource::class), 'notAResource ' )->shouldBeCalled ()->willReturn ($ nonResource1 );
216218 $ propertyAccessorProphecy ->getValue (Argument::type (ContainNonResource::class), 'collectionOfNotAResource ' )->shouldBeCalled ()->willReturn ($ collectionOfNotAResource );
217219
218- $ listener = new PurgeHttpCacheListener ($ purgerProphecy ->reveal (), $ iriConverterProphecy ->reveal (), $ resourceClassResolverProphecy ->reveal (), $ propertyAccessorProphecy ->reveal ());
220+ $ listener = new PurgeHttpCacheListener ($ purgerProphecy ->reveal (), $ iriConverterProphecy ->reveal (),
221+ $ resourceClassResolverProphecy ->reveal (), $ propertyAccessorProphecy ->reveal ());
219222 $ listener ->onFlush ($ eventArgs );
220223 $ listener ->postFlush ();
221224 }
@@ -229,7 +232,7 @@ public function testAddTagsForCollection(): void
229232 $ collection = [$ dummy1 , $ dummy2 ];
230233
231234 $ purgerProphecy = $ this ->prophesize (PurgerInterface::class);
232- $ purgerProphecy ->purge (['/dummies ' , '/dummies/1 ' , '/dummies/2 ' ])->shouldBeCalled ();
235+ $ purgerProphecy ->purge (['/dummies/1 ' , '/dummies/2 ' , '/dummies ' ])->shouldBeCalled ();
233236
234237 $ iriConverterProphecy = $ this ->prophesize (IriConverterInterface::class);
235238 $ iriConverterProphecy ->getIriFromResource (Argument::type (Dummy::class), UrlGeneratorInterface::ABS_PATH , new GetCollection ())->willReturn ('/dummies ' )->shouldBeCalled ();
@@ -270,15 +273,30 @@ public function testAddTagsForCollection(): void
270273 public function testMappedResources (): void
271274 {
272275 $ mappedEntity = new MappedEntity ();
276+ $ mappedEntity ->setFirstName ('first ' );
277+ $ mappedEntity ->setlastName ('last ' );
278+
279+ $ mappedResource = new MappedResource ();
280+ $ mappedResource ->username = $ mappedEntity ->getFirstName ().' ' .$ mappedEntity ->getLastName ();
273281
274282 $ purgerProphecy = $ this ->prophesize (PurgerInterface::class);
275283 $ purgerProphecy ->purge (['/mapped_ressources ' ])->shouldBeCalled ();
276284
277285 $ iriConverterProphecy = $ this ->prophesize (IriConverterInterface::class);
278- $ iriConverterProphecy ->getIriFromResource (Argument::type (MappedEntity::class), UrlGeneratorInterface::ABS_PATH , new GetCollection ())->willReturn ('/mapped_ressources ' )->shouldBeCalled ();
286+ // the entity is not a resource, shouldn't be called
287+ $ iriConverterProphecy ->getIriFromResource (
288+ Argument::type (MappedEntity::class), UrlGeneratorInterface::ABS_PATH , new GetCollection ()
289+ )->shouldNotBeCalled ();
290+ // this should be called instead
291+ $ iriConverterProphecy ->getIriFromResource (
292+ Argument::type (MappedResource::class), UrlGeneratorInterface::ABS_PATH , new GetCollection ()
293+ )->willReturn ('/mapped_ressources ' )->shouldBeCalled ();
279294
280295 $ resourceClassResolverProphecy = $ this ->prophesize (ResourceClassResolverInterface::class);
281- $ resourceClassResolverProphecy ->isResourceClass (MappedEntity::class)->willReturn (true )->shouldBeCalled ();
296+ $ resourceClassResolverProphecy ->isResourceClass (MappedEntity::class)->willReturn (false )->shouldBeCalled ();
297+
298+ $ objectMapperProphecy = $ this ->prophesize (ObjectMapperInterface::class);
299+ $ objectMapperProphecy ->map ($ mappedEntity , MappedResource::class)->shouldBeCalled ()->willReturn ($ mappedResource );
282300
283301 $ uowProphecy = $ this ->prophesize (UnitOfWork::class);
284302 $ uowProphecy ->getScheduledEntityInsertions ()->willReturn ([$ mappedEntity ])->shouldBeCalled ();
@@ -294,7 +312,10 @@ public function testMappedResources(): void
294312
295313 $ propertyAccessorProphecy = $ this ->prophesize (PropertyAccessorInterface::class);
296314
297- $ listener = new PurgeHttpCacheListener ($ purgerProphecy ->reveal (), $ iriConverterProphecy ->reveal (), $ resourceClassResolverProphecy ->reveal (), $ propertyAccessorProphecy ->reveal ());
315+ $ listener = new PurgeHttpCacheListener ($ purgerProphecy ->reveal (), $ iriConverterProphecy ->reveal (),
316+ $ resourceClassResolverProphecy ->reveal (), $ propertyAccessorProphecy ->reveal (),
317+ $ objectMapperProphecy ->reveal ()
318+ );
298319 $ listener ->onFlush ($ eventArgs );
299320 $ listener ->postFlush ();
300321 }
0 commit comments