Skip to content

Commit

Permalink
refactor #823 Fix event dispatcher provider (loic425)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.11 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Bug fix?        | yes (but on non-released feature)
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets |
| License         | MIT

Extracted from #817

Commits
-------

460da65 Fix event dispatcher provider
  • Loading branch information
lchrusciel authored Dec 18, 2023
2 parents ceff06c + 460da65 commit bc658a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/Component/src/State/Provider/EventDispatcherProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@
final class EventDispatcherProvider implements ProviderInterface
{
public function __construct(
private ProviderInterface $decorated,
private ProviderInterface $provider,
private OperationEventDispatcherInterface $operationEventDispatcher,
) {
}

public function provide(Operation $operation, Context $context): object|array|null
{
$data = $this->provider->provide($operation, $context);

if (
$operation instanceof CollectionOperationInterface ||
$operation instanceof ShowOperationInterface
) {
$this->operationEventDispatcher->dispatch(null, $operation, $context);
}

return $this->decorated->provide($operation, $context);
return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
final class EventDispatcherProviderSpec extends ObjectBehavior
{
function let(
ProviderInterface $decorated,
ProviderInterface $provider,
OperationEventDispatcherInterface $operationEventDispatcher,
): void {
$this->beConstructedWith($decorated, $operationEventDispatcher);
$this->beConstructedWith($provider, $operationEventDispatcher);
}

function it_is_initializable(): void
Expand All @@ -38,51 +38,51 @@ function it_is_initializable(): void
}

function it_dispatches_events_for_index_operation(
ProviderInterface $decorated,
ProviderInterface $provider,
OperationEventDispatcherInterface $operationEventDispatcher,
): void {
$operation = new Index(provider: '\App\Provider');
$context = new Context();

$operationEvent = new OperationEvent();

$decorated->provide($operation, $context)->shouldBeCalled();
$provider->provide($operation, $context)->shouldBeCalled();

$operationEventDispatcher->dispatch(null, $operation, $context)->willReturn($operationEvent)->shouldBeCalled();
$decorated->provide($operation, $context)->shouldBeCalled();
$provider->provide($operation, $context)->shouldBeCalled();

$operationEventDispatcher->dispatch(null, $operation, $context)->shouldBeCalled();

$this->provide($operation, $context);
}

function it_dispatches_events_for_show_operation(
ProviderInterface $decorated,
ProviderInterface $provider,
OperationEventDispatcherInterface $operationEventDispatcher,
): void {
$operation = new Show(provider: '\App\Provider');
$context = new Context();

$operationEvent = new OperationEvent();

$decorated->provide($operation, $context)->shouldBeCalled();
$provider->provide($operation, $context)->shouldBeCalled();

$operationEventDispatcher->dispatch(null, $operation, $context)->willReturn($operationEvent)->shouldBeCalled();
$decorated->provide($operation, $context)->shouldBeCalled();
$provider->provide($operation, $context)->shouldBeCalled();

$operationEventDispatcher->dispatch(null, $operation, $context)->shouldBeCalled();

$this->provide($operation, $context);
}

function it_does_not_dispatch_events_for_create_operation(
ProviderInterface $decorated,
ProviderInterface $provider,
OperationEventDispatcherInterface $operationEventDispatcher,
): void {
$operation = new Create(provider: '\App\Provider');
$context = new Context();

$decorated->provide($operation, $context)->shouldBeCalled();
$provider->provide($operation, $context)->shouldBeCalled();

$operationEventDispatcher->dispatch(null, $operation, $context)->shouldNotBeCalled();

Expand Down

0 comments on commit bc658a8

Please sign in to comment.