Skip to content

Commit 82d0fa5

Browse files
committed
[Workflow] Fix dispatch of entered event when the subject is already in this marking
1 parent d7b0d33 commit 82d0fa5

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Tests/WorkflowTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\Workflow\Definition;
17+
use Symfony\Component\Workflow\Event\EnteredEvent;
1718
use Symfony\Component\Workflow\Event\Event;
1819
use Symfony\Component\Workflow\Event\GuardEvent;
1920
use Symfony\Component\Workflow\Event\TransitionEvent;
@@ -685,6 +686,44 @@ public function testEventDefaultInitialContext()
685686
$workflow->apply($subject, 't1');
686687
}
687688

689+
public function testEventWhenAlreadyInThisPlace()
690+
{
691+
// ┌──────┐ ┌──────────────────────┐ ┌───┐ ┌─────────────┐ ┌───┐
692+
// │ init │ ──▶ │ from_init_to_a_and_b │ ──▶ │ B │ ──▶ │ from_b_to_c │ ──▶ │ C │
693+
// └──────┘ └──────────────────────┘ └───┘ └─────────────┘ └───┘
694+
// │
695+
// │
696+
// ▼
697+
// ┌───────────────────────────────┐
698+
// │ A │
699+
// └───────────────────────────────┘
700+
$definition = new Definition(
701+
['init', 'A', 'B', 'C'],
702+
[
703+
new Transition('from_init_to_a_and_b', 'init', ['A', 'B']),
704+
new Transition('from_b_to_c', 'B', 'C'),
705+
],
706+
);
707+
708+
$subject = new Subject();
709+
$dispatcher = new EventDispatcher();
710+
$name = 'workflow_name';
711+
$workflow = new Workflow($definition, new MethodMarkingStore(), $dispatcher, $name);
712+
713+
$calls = [];
714+
$listener = function (Event $event) use (&$calls) {
715+
$calls[] = $event;
716+
};
717+
$dispatcher->addListener("workflow.$name.entered.A", $listener);
718+
719+
$workflow->apply($subject, 'from_init_to_a_and_b');
720+
$workflow->apply($subject, 'from_b_to_c');
721+
722+
$this->assertCount(1, $calls);
723+
$this->assertInstanceOf(EnteredEvent::class, $calls[0]);
724+
$this->assertSame('from_init_to_a_and_b', $calls[0]->getTransition()->getName());
725+
}
726+
688727
public function testMarkingStateOnApplyWithEventDispatcher()
689728
{
690729
$definition = new Definition(range('a', 'f'), [new Transition('t', range('a', 'c'), range('d', 'f'))]);

Workflow.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,13 @@ private function entered(object $subject, ?Transition $transition, Marking $mark
391391
$this->dispatcher->dispatch($event, WorkflowEvents::ENTERED);
392392
$this->dispatcher->dispatch($event, sprintf('workflow.%s.entered', $this->name));
393393

394-
foreach ($marking->getPlaces() as $placeName => $nbToken) {
394+
$placeNames = [];
395+
if ($transition) {
396+
$placeNames = $transition->getTos();
397+
} elseif ($this->definition->getInitialPlaces()) {
398+
$placeNames = $this->definition->getInitialPlaces();
399+
}
400+
foreach ($placeNames as $placeName) {
395401
$this->dispatcher->dispatch($event, sprintf('workflow.%s.entered.%s', $this->name, $placeName));
396402
}
397403
}

0 commit comments

Comments
 (0)