From 14760cf10cc8b93ca4773809fb1189a29d01a2a5 Mon Sep 17 00:00:00 2001 From: Lauren Date: Fri, 17 Nov 2017 11:57:13 +0000 Subject: [PATCH 1/2] Allow multiple transitions with the same name This works in Symfony's Workflow but laravel-workflow doesn't have a way to reference multiple transitions with the same name in the config file. This patch adds support for the alternative notation referenced in symfony/symfony#22558 that uses a `name` field rather than inferring the transition name from the key. E.g. ``` 't1' => [ 'from' => 'a', 'to' => 'b', ] ``` becomes ``` [ 'name' => 't1', 'from' => 'a', 'to' => 'b', ] ``` which allows multiple transitions with the same name. --- src/WorkflowRegistry.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/WorkflowRegistry.php b/src/WorkflowRegistry.php index 2fcf84e..4cb3d42 100644 --- a/src/WorkflowRegistry.php +++ b/src/WorkflowRegistry.php @@ -48,6 +48,10 @@ public function __construct(array $config) $builder = new DefinitionBuilder($workflowData['places']); foreach ($workflowData['transitions'] as $transitionName => $transition) { + if (!is_string($transitionName)) { + $transitionName = $transition['name']; + } + $builder->addTransition(new Transition($transitionName, $transition['from'], $transition['to'])); } From 05a41c4fa3fa0581cccd8af9af941604a7168ebe Mon Sep 17 00:00:00 2001 From: Lauren Date: Fri, 17 Nov 2017 13:43:31 +0000 Subject: [PATCH 2/2] Add test for multiple transitions with the same name Also fixed the existing tests since there were errors that prevent the tests from running. --- tests/WorkflowDumpCommandTest.php | 2 +- tests/WorkflowRegistryTest.php | 46 +++++++++++++++++++++++++++++++ tests/WorkflowSubscriberTest.php | 6 ++-- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/tests/WorkflowDumpCommandTest.php b/tests/WorkflowDumpCommandTest.php index 74f3ae7..59802f3 100644 --- a/tests/WorkflowDumpCommandTest.php +++ b/tests/WorkflowDumpCommandTest.php @@ -5,7 +5,7 @@ use Mockery; use PHPUnit\Framework\TestCase; - class WorkflowRegistryTest extends TestCase + class WorkflowDumpCommandTest extends TestCase { public function testWorkflowCommand() { diff --git a/tests/WorkflowRegistryTest.php b/tests/WorkflowRegistryTest.php index d956ce8..91e12b5 100644 --- a/tests/WorkflowRegistryTest.php +++ b/tests/WorkflowRegistryTest.php @@ -79,4 +79,50 @@ public function testIfStateMachineIsRegistered() $this->assertTrue($workflow instanceof StateMachine); $this->assertTrue($markingStore instanceof MultipleStateMarkingStore); } + + public function testIfTransitionsWithSameNameCanBothBeUsed() + { + $config = [ + 'straight' => [ + 'type' => 'state_machine', + 'supports' => ['Tests\Fixtures\TestObject'], + 'places' => ['a', 'b', 'c'], + 'transitions' => [ + [ + 'name' => 't1', + 'from' => 'a', + 'to' => 'b', + ], + [ + 'name' => 't1', + 'from' => 'c', + 'to' => 'b', + ], + [ + 'name' => 't2', + 'from' => 'b', + 'to' => 'c', + ] + ], + ] + ]; + + $registry = new WorkflowRegistry($config); + $subject = new TestObject; + $workflow = $registry->get($subject); + + $markingStoreProp = new ReflectionProperty(Workflow::class, 'markingStore'); + $markingStoreProp->setAccessible(true); + + $markingStore = $markingStoreProp->getValue($workflow); + + $this->assertTrue($workflow instanceof StateMachine); + $this->assertTrue($markingStore instanceof SingleStateMarkingStore); + $this->assertTrue($workflow->can($subject, 't1')); + + $workflow->apply($subject, 't1'); + $workflow->apply($subject, 't2'); + + $this->assertTrue($workflow->can($subject, 't1')); + } } diff --git a/tests/WorkflowSubscriberTest.php b/tests/WorkflowSubscriberTest.php index 2cccf40..7e03e9c 100644 --- a/tests/WorkflowSubscriberTest.php +++ b/tests/WorkflowSubscriberTest.php @@ -6,12 +6,14 @@ use Tests\Fixtures\TestObject; use Illuminate\Support\Facades\Event; - class WorkflowRegistryTest extends TestCase + class WorkflowSubscriberTest extends TestCase { - public function testIfWorkflowIsRegisrter() + public function testIfWorkflowEmitsEvents() { global $events; + $events = []; + $config = [ 'straight' => [ 'supports' => ['Tests\Fixtures\TestObject'],