10000 [Workflow] Fix dispatch of entered event when the subject is already in this marking by lyrixx · Pull Request #60194 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Workflow] Fix dispatch of entered event when the subject is already in this marking #60194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Workflow] Fix dispatch of entered event when the subject is already …
…in this marking
  • Loading branch information
lyrixx committed Apr 10, 2025
commit 7db98c9fbe1a0f0653d1d6f6ad6119fec791ba28
39 changes: 39 additions & 0 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Event\EnteredEvent;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Event\TransitionEvent;
Expand Down Expand Up @@ -685,6 +686,44 @@ public function testEventDefaultInitialContext()
$workflow->apply($subject, 't1');
}

public function testEventWhenAlreadyInThisPlace()
{
// ┌──────┐ ┌──────────────────────┐ ┌───┐ ┌─────────────┐ ┌───┐
// │ init │ ──▶ │ from_init_to_a_and_b │ ──▶ │ B │ ──▶ │ from_b_to_c │ ──▶ │ C │
// └──────┘ └──────────────────────┘ └───┘ └─────────────┘ └───┘
// │
// │
// ▼
// ┌───────────────────────────────┐
// │ A │
// └───────────────────────────────┘
$definition = new Definition(
['init', 'A', 'B', 'C'],
[
new Transition('from_init_to_a_and_b', 'init', ['A', 'B']),
new Transition('from_b_to_c', 'B', 'C'),
],
);

$subject = new Subject();
$dispatcher = new EventDispatcher();
$name = 'workflow_name';
$workflow = new Workflow($definition, new MethodMarkingStore(), $dispatcher, $name);

$calls = [];
$listener = function (Event $event) use (&$calls) {
$calls[] = $event;
};
$dispatcher->addListener("workflow.$name.entered.A", $listener);

$workflow->apply($subject, 'from_init_to_a_and_b');
$workflow->apply($subject, 'from_b_to_c');

$this->assertCount(1, $calls);
$this->assertInstanceOf(EnteredEvent::class, $calls[0]);
$this->assertSame('from_init_to_a_and_b', $calls[0]->getTransition()->getName());
}

public function testMarkingStateOnApplyWithEventDispatcher()
{
$definition = new Definition(range('a', 'f'), [new Transition('t', range('a', 'c'), range('d', 'f'))]);
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/Workflow/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,13 @@ private function entered(object $subject, ?Transition $transition, Marking $mark
$this->dispatcher->dispatch($event, WorkflowEvents::ENTERED);
$this->dispatcher->dispatch($event, sprintf('workflow.%s.entered', $this->name));

foreach ($marking->getPlaces() as $placeName => $nbToken) {
$placeNames = [];
if ($transition) {
$placeNames = $transition->getTos();
} elseif ($this->definition->getInitialPlaces()) {
$placeNames = $this->definition->getInitialPlaces();
}
foreach ($placeNames as $placeName) {
$this->dispatcher->dispatch($event, sprintf('workflow.%s.entered.%s', $this->name, $placeName));
}
}
Expand Down
Loading
0