Skip to content

Commit 18f73ed

Browse files
authored
Merge pull request #5 from laurenkt/master
Add support for multiple transitions with the same name
2 parents 876075f + 05a41c4 commit 18f73ed

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/WorkflowRegistry.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function __construct(array $config)
4848
$builder = new DefinitionBuilder($workflowData['places']);
4949

5050
foreach ($workflowData['transitions'] as $transitionName => $transition) {
51+
if (!is_string($transitionName)) {
52+
$transitionName = $transition['name'];
53+
}
54+
5155
$builder->addTransition(new Transition($transitionName, $transition['from'], $transition['to']));
5256
}
5357

tests/WorkflowRegistryTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,50 @@ public function testIfStateMachineIsRegistered()
7979
$this->assertTrue($workflow instanceof StateMachine);
8080
$this->assertTrue($markingStore instanceof MultipleStateMarkingStore);
8181
}
82+
83+
public function testIfTransitionsWithSameNameCanBothBeUsed()
84+
{
85+
$config = [
86+
'straight' => [
87+
'type' => 'state_machine',
88+
'supports' => ['Tests\Fixtures\TestObject'],
89+
'places' => ['a', 'b', 'c'],
90+
'transitions' => [
91+
[
92+
'name' => 't1',
93+
'from' => 'a',
94+
'to' => 'b',
95+
],
96+
[
97+
'name' => 't1',
98+
'from' => 'c',
99+
'to' => 'b',
100+
],
101+
[
102+
'name' => 't2',
103+
'from' => 'b',
104+
'to' => 'c',
105+
]
106+
],
107+
]
108+
];
109+
110+
$registry = new WorkflowRegistry($config);
111+
$subject = new TestObject;
112+
$workflow = $registry->get($subject);
113+
114+
$markingStoreProp = new ReflectionProperty(Workflow::class, 'markingStore');
115+
$markingStoreProp->setAccessible(true);
116+
117+
$markingStore = $markingStoreProp->getValue($workflow);
118+
119+
$this->assertTrue($workflow instanceof StateMachine);
120+
$this->assertTrue($markingStore instanceof SingleStateMarkingStore);
121+
$this->assertTrue($workflow->can($subject, 't1'));
122+
123+
$workflow->apply($subject, 't1');
124+
$workflow->apply($subject, 't2');
125+
126+
$this->assertTrue($workflow->can($subject, 't1'));
127+
}
82128
}

tests/WorkflowSubscriberTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
class WorkflowSubscriberTest extends TestCase
99
{
10-
public function testIfWorkflowIsRegisrter()
10+
public function testIfWorkflowEmitsEvents()
1111
{
1212
global $events;
1313

14+
$events = [];
15+
1416
$config = [
1517
'straight' => [
1618
'supports' => ['Tests\Fixtures\TestObject'],

0 commit comments

Comments
 (0)