Skip to content

Commit

Permalink
Merge pull request #5 from laurenkt/master
Browse files Browse the repository at this point in the history
Add support for multiple transitions with the same name
  • Loading branch information
brexis authored Nov 17, 2017
2 parents 876075f + 05a41c4 commit 18f73ed
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/WorkflowRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
}

Expand Down
46 changes: 46 additions & 0 deletions tests/WorkflowRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
4 changes: 3 additions & 1 deletion tests/WorkflowSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

class WorkflowSubscriberTest extends TestCase
{
public function testIfWorkflowIsRegisrter()
public function testIfWorkflowEmitsEvents()
{
global $events;

$events = [];

$config = [
'straight' => [
'supports' => ['Tests\Fixtures\TestObject'],
Expand Down

0 comments on commit 18f73ed

Please sign in to comment.