feature #35322 [Workflow] Added a way to not fire the announce event (lyrixx)
This PR was merged into the 5.1-dev branch.
Discussion
----------
[Workflow] Added a way to not fire the announce event
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix #35286
| License | MIT
| Doc PR |
Commits
-------
d31939d01d
[Workflow] Added a way to not fire the annonce event
This commit is contained in:
commit
b350c80fc3
@ -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
|
||||||
-----
|
-----
|
||||||
|
@ -426,6 +426,30 @@ class WorkflowTest extends TestCase
|
|||||||
$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');
|
||||||
|
@ -33,6 +33,8 @@ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
|||||||
*/
|
*/
|
||||||
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;
|
||||||
@ -207,7 +209,9 @@ class Workflow implements WorkflowInterface
|
|||||||
|
|
||||||
$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;
|
||||||
|
Reference in New Issue
Block a user