From a6c4698162866e2f6856074cf4e23be73b56e825 Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Tue, 16 Feb 2021 08:31:35 +0100 Subject: [PATCH] add event-subscriber to throw flushed workflow events --- .../Workflow/FlushedEventSubscriber.php | 76 +++++++++++++++++++ DependencyInjection/SuluContentExtension.php | 2 + Resources/config/services.xml | 8 ++ Tests/Application/config/config.yml | 10 --- .../Workflow/FlushedEventSubscriberTest.php | 70 +++++++++++++++++ 5 files changed, 156 insertions(+), 10 deletions(-) create mode 100644 Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriber.php create mode 100644 Tests/Unit/Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriberTest.php diff --git a/Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriber.php b/Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriber.php new file mode 100644 index 00000000..c3757921 --- /dev/null +++ b/Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriber.php @@ -0,0 +1,76 @@ + 'onCompleted', + ]; + } + + /** + * @var EventDispatcherInterface + */ + private $eventDispatcher; + + /** + * @var mixed[] + */ + private $eventsToDispatch = []; + + public function __construct(EventDispatcherInterface $eventDispatcher) + { + $this->eventDispatcher = $eventDispatcher; + } + + public function onCompleted(Event $event): void + { + if (WorkflowInterface::WORKFLOW_DEFAULT_NAME !== $event->getWorkflowName()) { + return; + } + + $eventName = sprintf('workflow.%s.flushed', $event->getWorkflowName()); + + $this->eventsToDispatch[] = [$event, $eventName]; + $this->eventsToDispatch[] = [$event, sprintf('%s.%s', $eventName, $event->getTransition()->getName())]; + } + + public function postFlush(): void + { + foreach ($this->eventsToDispatch as $item) { + $this->eventDispatcher->dispatch($item[0], $item[1]); + } + + $this->eventsToDispatch = []; + } + + public function onClear(): void + { + $this->eventsToDispatch = []; + } +} diff --git a/DependencyInjection/SuluContentExtension.php b/DependencyInjection/SuluContentExtension.php index 91951ee8..822a38a5 100644 --- a/DependencyInjection/SuluContentExtension.php +++ b/DependencyInjection/SuluContentExtension.php @@ -19,6 +19,7 @@ use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\HttpKernel\Bundle\Bundle; class SuluContentExtension extends Extension implements PrependExtensionInterface { @@ -54,6 +55,7 @@ public function load(array $configs, ContainerBuilder $container): void if ($container->hasParameter('kernel.bundles')) { // TODO FIXME add test here // @codeCoverageIgnoreStart + /** @var array $bundles */ $bundles = $container->getParameter('kernel.bundles'); if (\array_key_exists('SuluAutomationBundle', $bundles)) { diff --git a/Resources/config/services.xml b/Resources/config/services.xml index fd41fa84..586880e1 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -166,5 +166,13 @@ + + + + + + + + diff --git a/Tests/Application/config/config.yml b/Tests/Application/config/config.yml index cad22d0f..58deced6 100644 --- a/Tests/Application/config/config.yml +++ b/Tests/Application/config/config.yml @@ -1,16 +1,6 @@ swiftmailer: disable_delivery: true -doctrine: - orm: - mappings: - gedmo_tree: - type: xml - prefix: Gedmo\Tree\Entity - dir: "%kernel.project_dir%/../../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity" - alias: GedmoTree - is_bundle: false - #sulu_core: # content: # structure: diff --git a/Tests/Unit/Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriberTest.php b/Tests/Unit/Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriberTest.php new file mode 100644 index 00000000..6330487d --- /dev/null +++ b/Tests/Unit/Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriberTest.php @@ -0,0 +1,70 @@ +prophesize(Transition::class); + $transition->getName()->willReturn('published'); + + $event = $this->prophesize(Event::class); + $event->getWorkflowName()->willReturn(WorkflowInterface::WORKFLOW_DEFAULT_NAME); + $event->getTransition()->willReturn($transition->reveal()); + + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(Argument::any())->shouldNotBeCalled(); + + $subscriber = $this->createFlushedEventSubscriber($eventDispatcher->reveal()); + $subscriber->onCompleted($event->reveal()); + + $eventName = sprintf('workflow.%s.flushed', WorkflowInterface::WORKFLOW_DEFAULT_NAME); + $eventDispatcher->dispatch($event->reveal(), $eventName)->shouldBeCalledOnce(); + $eventDispatcher->dispatch($event->reveal(), $eventName . '.published')->shouldBeCalledOnce(); + + $subscriber->postFlush(); + } + + public function testOnCompleteAndClear(): void + { + $transition = $this->prophesize(Transition::class); + $transition->getName()->willReturn('published'); + + $event = $this->prophesize(Event::class); + $event->getWorkflowName()->willReturn(WorkflowInterface::WORKFLOW_DEFAULT_NAME); + $event->getTransition()->willReturn($transition->reveal()); + + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher->dispatch(Argument::any())->shouldNotBeCalled(); + + $subscriber = $this->createFlushedEventSubscriber($eventDispatcher->reveal()); + $subscriber->onCompleted($event->reveal()); + + $subscriber->onClear(); + } +}