diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index bbc1b4a434..0af59c60ea 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -6220,11 +6220,6 @@ parameters: count: 1 path: src/contracts/Repository/Events/Trash/BeforeRecoverEvent.php - - - message: "#^Method Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Events\\\\Trash\\\\BeforeTrashEvent\\:\\:getResult\\(\\) should return Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\TrashItem but returns Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\TrashItem\\|null\\.$#" - count: 1 - path: src/contracts/Repository/Events/Trash/BeforeTrashEvent.php - - message: "#^Property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Events\\\\Trash\\\\RecoverEvent\\:\\:\\$newParentLocation \\(Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Location\\) does not accept Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Location\\|null\\.$#" count: 1 diff --git a/src/contracts/Repository/Events/Trash/BeforeTrashEvent.php b/src/contracts/Repository/Events/Trash/BeforeTrashEvent.php index 0e7b70a7cb..89e6fd6261 100644 --- a/src/contracts/Repository/Events/Trash/BeforeTrashEvent.php +++ b/src/contracts/Repository/Events/Trash/BeforeTrashEvent.php @@ -34,7 +34,7 @@ public function getLocation(): Location return $this->location; } - public function getResult(): TrashItem + public function getResult(): ?TrashItem { if (!$this->isResultSet()) { throw new UnexpectedValueException(sprintf('Return value is not set or not of type %s (or null). Check isResultSet() or set it using setResult() before you call the getter.', TrashItem::class)); diff --git a/tests/lib/Event/TrashServiceTest.php b/tests/lib/Event/TrashServiceTest.php index 688926d049..34fddd915a 100644 --- a/tests/lib/Event/TrashServiceTest.php +++ b/tests/lib/Event/TrashServiceTest.php @@ -218,6 +218,41 @@ public function testTrashStopPropagationInBeforeEvents() ]); } + public function testTrashStopPropagationInBeforeEventsSetsNullResult(): void + { + $traceableEventDispatcher = $this->getEventDispatcher( + BeforeTrashEvent::class, + TrashEvent::class + ); + + $parameters = [ + $this->createMock(Location::class), + ]; + + $innerServiceMock = $this->createMock(TrashServiceInterface::class); + $innerServiceMock->expects(self::never())->method('trash'); + + $traceableEventDispatcher->addListener(BeforeTrashEvent::class, static function (BeforeTrashEvent $event) { + $event->setResult(null); + $event->stopPropagation(); + }, 10); + + $service = new TrashService($innerServiceMock, $traceableEventDispatcher); + $result = $service->trash(...$parameters); + + $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners()); + $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners()); + + self::assertNull($result); + self::assertSame($calledListeners, [ + [BeforeTrashEvent::class, 10], + ]); + self::assertSame($notCalledListeners, [ + [BeforeTrashEvent::class, 0], + [TrashEvent::class, 0], + ]); + } + public function testRecoverEvents() { $traceableEventDispatcher = $this->getEventDispatcher(