Skip to content

Commit

Permalink
[Workflow] Added a way to not fire the annonce event
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Jan 13, 2020
1 parent ddc0169 commit d31939d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG


* Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()` * Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
* Added `Registry::has()` to check if a workflow exists * Added `Registry::has()` to check if a workflow exists
* Added support for `$context[Workflow::DISABLE_ANNOUNCE_EVENT] = true` when calling `workflow->apply()` to not fire the announce event


5.0.0 5.0.0
----- -----
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -426,6 +426,30 @@ public function testApplyWithEventDispatcher()
$this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents); $this->assertSame($eventNameExpected, $eventDispatcher->dispatchedEvents);
} }


public function provideApplyWithEventDispatcherForAnnounceTests()
{
yield [false, [Workflow::DISABLE_ANNOUNCE_EVENT => true]];
yield [true, [Workflow::DISABLE_ANNOUNCE_EVENT => false]];
yield [true, []];
}

/** @dataProvider provideApplyWithEventDispatcherForAnnounceTests */
public function testApplyWithEventDispatcherForAnnounce(bool $fired, array $context)
{
$definition = $this->createComplexWorkflowDefinition();
$subject = new Subject();
$eventDispatcher = new EventDispatcherMock();
$workflow = new Workflow($definition, new MethodMarkingStore(), $eventDispatcher, 'workflow_name');

$workflow->apply($subject, 't1', $context);

if ($fired) {
$this->assertContains('workflow.workflow_name.announce', $eventDispatcher->dispatchedEvents);
} else {
$this->assertNotContains('workflow.workflow_name.announce', $eventDispatcher->dispatchedEvents);
}
}

public function testApplyDoesNotTriggerExtraGuardWithEventDispatcher() public function testApplyDoesNotTriggerExtraGuardWithEventDispatcher()
{ {
$transitions[] = new Transition('a-b', 'a', 'b'); $transitions[] = new Transition('a-b', 'a', 'b');
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Workflow/Workflow.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
*/ */
class Workflow implements WorkflowInterface class Workflow implements WorkflowInterface
{ {
public const DISABLE_ANNOUNCE_EVENT = 'workflow_disable_announce_event';

private $definition; private $definition;
private $markingStore; private $markingStore;
private $dispatcher; private $dispatcher;
Expand Down Expand Up @@ -207,7 +209,9 @@ public function apply(object $subject, string $transitionName, array $context =


$this->completed($subject, $transition, $marking); $this->completed($subject, $transition, $marking);


$this->announce($subject, $transition, $marking); if (!($context[self::DISABLE_ANNOUNCE_EVENT] ?? false)) {
$this->announce($subject, $transition, $marking);
}
} }


return $marking; return $marking;
Expand Down

0 comments on commit d31939d

Please sign in to comment.