-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add event-subscriber to throw flushed workflow events
- Loading branch information
1 parent
91b8b90
commit a6c4698
Showing
5 changed files
with
156 additions
and
10 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Symfony\Workflow; | ||
|
||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Workflow\Event\Event; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class FlushedEventSubscriber implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @return string[] | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
'workflow.completed' => '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 = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
Tests/Unit/Content/Infrastructure/Symfony/Workflow/FlushedEventSubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) Sulu GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ContentBundle\Tests\Unit\Content\Infrastructure\Symfony\Workflow; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Symfony\Workflow\FlushedEventSubscriber; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\Workflow\Event\Event; | ||
use Symfony\Component\Workflow\Transition; | ||
|
||
class FlushedEventSubscriberTest extends TestCase | ||
{ | ||
protected function createFlushedEventSubscriber(EventDispatcherInterface $eventDispatcher): FlushedEventSubscriber | ||
{ | ||
return new FlushedEventSubscriber($eventDispatcher); | ||
} | ||
|
||
public function testOnCompleteAndFlush(): 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()); | ||
|
||
$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(); | ||
} | ||
} |