Skip to content
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
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