22
22
use ApiPlatform \Metadata \UrlGeneratorInterface ;
23
23
use ApiPlatform \Symfony \Doctrine \EventListener \PurgeHttpCacheListener ;
24
24
use ApiPlatform \Symfony \Tests \Fixtures \MappedEntity ;
25
+ use ApiPlatform \Symfony \Tests \Fixtures \MappedResource ;
25
26
use ApiPlatform \Symfony \Tests \Fixtures \NotAResource ;
26
27
use ApiPlatform \Symfony \Tests \Fixtures \TestBundle \Entity \ContainNonResource ;
27
28
use ApiPlatform \Symfony \Tests \Fixtures \TestBundle \Entity \Dummy ;
35
36
use PHPUnit \Framework \TestCase ;
36
37
use Prophecy \Argument ;
37
38
use Prophecy \PhpUnit \ProphecyTrait ;
39
+ use Symfony \Component \ObjectMapper \ObjectMapperInterface ;
38
40
use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
39
41
40
42
/**
@@ -215,7 +217,8 @@ public function testNotAResourceClass(): void
215
217
$ propertyAccessorProphecy ->getValue (Argument::type (ContainNonResource::class), 'notAResource ' )->shouldBeCalled ()->willReturn ($ nonResource1 );
216
218
$ propertyAccessorProphecy ->getValue (Argument::type (ContainNonResource::class), 'collectionOfNotAResource ' )->shouldBeCalled ()->willReturn ($ collectionOfNotAResource );
217
219
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 ());
219
222
$ listener ->onFlush ($ eventArgs );
220
223
$ listener ->postFlush ();
221
224
}
@@ -229,7 +232,7 @@ public function testAddTagsForCollection(): void
229
232
$ collection = [$ dummy1 , $ dummy2 ];
230
233
231
234
$ purgerProphecy = $ this ->prophesize (PurgerInterface::class);
232
- $ purgerProphecy ->purge (['/dummies ' , '/dummies/1 ' , '/dummies/2 ' ])->shouldBeCalled ();
235
+ $ purgerProphecy ->purge (['/dummies/1 ' , '/dummies/2 ' , '/dummies ' ])->shouldBeCalled ();
233
236
234
237
$ iriConverterProphecy = $ this ->prophesize (IriConverterInterface::class);
235
238
$ iriConverterProphecy ->getIriFromResource (Argument::type (Dummy::class), UrlGeneratorInterface::ABS_PATH , new GetCollection ())->willReturn ('/dummies ' )->shouldBeCalled ();
@@ -270,15 +273,30 @@ public function testAddTagsForCollection(): void
270
273
public function testMappedResources (): void
271
274
{
272
275
$ mappedEntity = new MappedEntity ();
276
+ $ mappedEntity ->setFirstName ('first ' );
277
+ $ mappedEntity ->setlastName ('last ' );
278
+
279
+ $ mappedResource = new MappedResource ();
280
+ $ mappedResource ->username = $ mappedEntity ->getFirstName ().' ' .$ mappedEntity ->getLastName ();
273
281
274
282
$ purgerProphecy = $ this ->prophesize (PurgerInterface::class);
275
283
$ purgerProphecy ->purge (['/mapped_ressources ' ])->shouldBeCalled ();
276
284
277
285
$ 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 ();
279
294
280
295
$ 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 );
282
300
283
301
$ uowProphecy = $ this ->prophesize (UnitOfWork::class);
284
302
$ uowProphecy ->getScheduledEntityInsertions ()->willReturn ([$ mappedEntity ])->shouldBeCalled ();
@@ -294,7 +312,10 @@ public function testMappedResources(): void
294
312
295
313
$ propertyAccessorProphecy = $ this ->prophesize (PropertyAccessorInterface::class);
296
314
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
+ );
298
319
$ listener ->onFlush ($ eventArgs );
299
320
$ listener ->postFlush ();
300
321
}
0 commit comments