Skip to content

Commit

Permalink
Fix purging HTTP cache for unreadable relations
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque authored and alanpoulain committed Mar 4, 2021
1 parent 4cc8871 commit a744e18
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Bridge/Doctrine/EventListener/PurgeHttpCacheListener.php
Expand Up @@ -125,7 +125,9 @@ private function gatherRelationTags(EntityManagerInterface $em, $entity): void
{
$associationMappings = $em->getClassMetadata(ClassUtils::getClass($entity))->getAssociationMappings();
foreach (array_keys($associationMappings) as $property) {
$this->addTagsFor($this->propertyAccessor->getValue($entity, $property));
if ($this->propertyAccessor->isReadable($entity, $property)) {
$this->addTagsFor($this->propertyAccessor->getValue($entity, $property));
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions tests/Bridge/Doctrine/EventListener/PurgeHttpCacheListenerTest.php
Expand Up @@ -29,6 +29,7 @@
use Doctrine\ORM\UnitOfWork;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
Expand Down Expand Up @@ -78,11 +79,22 @@ public function testOnFlush()

$emProphecy = $this->prophesize(EntityManagerInterface::class);
$emProphecy->getUnitOfWork()->willReturn($uowProphecy->reveal())->shouldBeCalled();
$emProphecy->getClassMetadata(Dummy::class)->willReturn(new ClassMetadata(Dummy::class))->shouldBeCalled();
$dummyClassMetadata = new ClassMetadata(Dummy::class);
$dummyClassMetadata->associationMappings = [
'relatedDummy' => [],
'relatedOwningDummy' => [],
];
$emProphecy->getClassMetadata(Dummy::class)->willReturn($dummyClassMetadata)->shouldBeCalled();
$emProphecy->getClassMetadata(DummyNoGetOperation::class)->willReturn(new ClassMetadata(DummyNoGetOperation::class))->shouldBeCalled();
$eventArgs = new OnFlushEventArgs($emProphecy->reveal());

$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal());
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
$propertyAccessorProphecy->isReadable(Argument::type(Dummy::class), 'relatedDummy')->willReturn(true);
$propertyAccessorProphecy->isReadable(Argument::type(Dummy::class), 'relatedOwningDummy')->willReturn(false);
$propertyAccessorProphecy->getValue(Argument::type(Dummy::class), 'relatedDummy')->shouldBeCalled();
$propertyAccessorProphecy->getValue(Argument::type(Dummy::class), 'relatedOwningDummy')->shouldNotBeCalled();

$listener = new PurgeHttpCacheListener($purgerProphecy->reveal(), $iriConverterProphecy->reveal(), $resourceClassResolverProphecy->reveal(), $propertyAccessorProphecy->reveal());
$listener->onFlush($eventArgs);
$listener->postFlush();
}
Expand Down

0 comments on commit a744e18

Please sign in to comment.