Skip to content

Commit

Permalink
IBX-7055: Made BeforeTrashEvent::getResult return value nullable (#297)
Browse files Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-7055 and #297

Key changes:

* Allowed to return `null` from `BeforeTrashEvent::getResult` when content Location is not trashed

* [Tests] Added test coverage
  • Loading branch information
mikadamczyk authored Dec 6, 2023
1 parent 1044c97 commit e22d595
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/Repository/Events/Trash/BeforeTrashEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
35 changes: 35 additions & 0 deletions tests/lib/Event/TrashServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e22d595

Please sign in to comment.