Skip to content

Commit

Permalink
minor #23781 [Workflow] do not emit not needed guard events (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[Workflow] do not emit not needed guard events

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23677
| License       | MIT
| Doc PR        |

Commits
-------

47c68e1 [Workflow] do not emit not needed guard events
  • Loading branch information
fabpot committed Aug 5, 2017
2 parents a12ebf7 + 47c68e1 commit 457d57b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Expand Up @@ -137,6 +137,31 @@ public function testCanWithGuard()
$this->assertFalse($workflow->can($subject, 't1'));
}

public function testCanDoesNotTriggerGuardEventsForNotEnabledTransitions()
{
$definition = $this->createComplexWorkflowDefinition();
$subject = new \stdClass();
$subject->marking = null;

$dispatchedEvents = array();
$eventDispatcher = new EventDispatcher();

$workflow = new Workflow($definition, new MultipleStateMarkingStore(), $eventDispatcher, 'workflow_name');
$workflow->apply($subject, 't1');
$workflow->apply($subject, 't2');

$eventDispatcher->addListener('workflow.workflow_name.guard.t3', function () use (&$dispatchedEvents) {
$dispatchedEvents[] = 'workflow_name.guard.t3';
});
$eventDispatcher->addListener('workflow.workflow_name.guard.t4', function () use (&$dispatchedEvents) {
$dispatchedEvents[] = 'workflow_name.guard.t4';
});

$workflow->can($subject, 't3');

$this->assertSame(array('workflow_name.guard.t3'), $dispatchedEvents);
}

/**
* @expectedException \Symfony\Component\Workflow\Exception\LogicException
* @expectedExceptionMessage Unable to apply transition "t2" for workflow "unnamed".
Expand Down
13 changes: 11 additions & 2 deletions src/Symfony/Component/Workflow/Workflow.php
Expand Up @@ -92,10 +92,19 @@ public function getMarking($subject)
*/
public function can($subject, $transitionName)
{
$transitions = $this->getEnabledTransitions($subject);
$transitions = $this->definition->getTransitions();
$marking = $this->getMarking($subject);

foreach ($transitions as $transition) {
if ($transitionName === $transition->getName()) {
foreach ($transition->getFroms() as $place) {
if (!$marking->has($place)) {
// do not emit guard events for transitions where the marking does not contain
// all "from places" (thus the transition couldn't be applied anyway)
continue 2;
}
}

if ($transitionName === $transition->getName() && $this->doCan($subject, $marking, $transition)) {
return true;
}
}
Expand Down

0 comments on commit 457d57b

Please sign in to comment.