Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Symfony/Doctrine/EventListener/PurgeHttpCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ private function gatherRelationTags(EntityManagerInterface $em, object $entity):
/** @var array|AssociationMapping $associationMapping according to the version of doctrine orm */
foreach ($associationMappings as $property => $associationMapping) {
if ($associationMapping instanceof AssociationMapping && !$this->resourceClassResolver->isResourceClass($associationMapping->targetEntity)) {
return;
continue;
}
if (!$this->propertyAccessor->isReadable($entity, $property)) {
return;
continue;
}

if (
Expand All @@ -192,16 +192,16 @@ private function gatherRelationTags(EntityManagerInterface $em, object $entity):
&& !$this->resourceClassResolver->isResourceClass($targetEntity)
) {
if (!$this->objectMapper) {
return;
continue;
}

$targetRefl = new \ReflectionClass($targetEntity);
if ($this->objectMapperMetadata) {
if (!$this->objectMapperMetadata->create($targetRefl->newInstanceWithoutConstructor())) {
return;
continue;
}
} elseif (!$targetRefl->getAttributes(Map::class)) {
return;
continue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,51 @@ static function (object $resource, int $referenceType = UrlGeneratorInterface::A
$listener->preUpdate($eventArgs);
$listener->postFlush();
}

public function testSkippedRelationDoesNotStopGatheringOtherRelations(): void
{
$dummy = new Dummy();
$dummy->setId(1);
$relatedDummy = new RelatedDummy();
$relatedDummy->setId(2);

$purger = $this->createMock(PurgerInterface::class);
$purger->expects($this->once())->method('purge')->with($this->callback(static function (array $tags): bool {
return \in_array('/related_dummies/2', $tags, true);
}));

$iriConverter = $this->createStub(IriConverterInterface::class);
$iriConverter->method('getIriFromResource')->willReturnCallback(
static fn (object $resource): string => $resource instanceof RelatedDummy ? '/related_dummies/2' : '/dummies/1'
);

$resourceClassResolver = $this->createStub(ResourceClassResolverInterface::class);
$resourceClassResolver->method('isResourceClass')->willReturnCallback(static fn (string $class): bool => NotAResource::class !== $class);

$classMetadata = new ClassMetadata(Dummy::class);
// @phpstan-ignore-next-line
$classMetadata->associationMappings = [
'notAResource' => ['targetEntity' => NotAResource::class],
'relatedDummy' => ['targetEntity' => RelatedDummy::class],
];

$unitOfWork = $this->createStub(UnitOfWork::class);
$unitOfWork->method('getScheduledEntityInsertions')->willReturn([]);
$unitOfWork->method('getScheduledEntityUpdates')->willReturn([$dummy]);
$unitOfWork->method('getScheduledEntityDeletions')->willReturn([]);

$em = $this->createStub(EntityManagerInterface::class);
$em->method('getUnitOfWork')->willReturn($unitOfWork);
$em->method('getClassMetadata')->willReturn($classMetadata);

$propertyAccessor = $this->createStub(PropertyAccessorInterface::class);
$propertyAccessor->method('isReadable')->willReturn(true);
$propertyAccessor->method('getValue')->willReturnCallback(
static fn (object $object, string $property): object => 'relatedDummy' === $property ? $relatedDummy : new NotAResource('foo', 'bar')
);

$listener = new PurgeHttpCacheListener($purger, $iriConverter, $resourceClassResolver, $propertyAccessor);
$listener->onFlush(new OnFlushEventArgs($em));
$listener->postFlush();
}
}
Loading