Skip to content

Commit c11061c

Browse files
bug #60194 [Workflow] Fix dispatch of entered event when the subject is already in this marking (lyrixx)
This PR was merged into the 6.4 branch. Discussion ---------- [Workflow] Fix dispatch of entered event when the subject is already in this marking | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #57924 | License | MIT This was wrong: symfony/symfony#37813 Commits ------- 7db98c9fbe1 [Workflow] Fix dispatch of entered event when the subject is already in this marking
2 parents 492ec93 + 82d0fa5 commit c11061c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Tests/WorkflowTest.php

Lines changed: 39 additions & 0 deletions
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

Lines changed: 7 additions & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy