Skip to content

Commit eece8ad

Browse files
Nyholmfabpot
authored andcommitted
[Workflow] Added new validator to make sure each place has unique translation names
1 parent 9d09059 commit eece8ad

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Symfony\Component\Workflow\Tests\Validator;
44

5+
use Symfony\Component\Workflow\Definition;
56
use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait;
7+
use Symfony\Component\Workflow\Transition;
68
use Symfony\Component\Workflow\Validator\WorkflowValidator;
79

810
class WorkflowValidatorTest extends \PHPUnit_Framework_TestCase
@@ -26,4 +28,36 @@ public function testSinglePlaceWorkflowValidatorAndSimpleWorkflow()
2628

2729
(new WorkflowValidator(true))->validate($definition, 'foo');
2830
}
31+
32+
/**
33+
* @expectedException \Symfony\Component\Workflow\Exception\InvalidDefinitionException
34+
* @expectedExceptionMessage All transitions for a place must have an unique name. Multiple transitions named "t1" where found for place "a" in workflow "foo".
35+
*/
36+
public function testWorkflowWithInvalidNames()
37+
{
38+
$places = range('a', 'c');
39+
40+
$transitions = array();
41+
$transitions[] = new Transition('t0', 'c', 'b');
42+
$transitions[] = new Transition('t1', 'a', 'b');
43+
$transitions[] = new Transition('t1', 'a', 'c');
44+
45+
$definition = new Definition($places, $transitions);
46+
47+
(new WorkflowValidator())->validate($definition, 'foo');
48+
}
49+
50+
public function testSameTransitionNameButNotSamePlace()
51+
{
52+
$places = range('a', 'd');
53+
54+
$transitions = array();
55+
$transitions[] = new Transition('t1', 'a', 'b');
56+
$transitions[] = new Transition('t1', 'b', 'c');
57+
$transitions[] = new Transition('t1', 'd', 'c');
58+
59+
$definition = new Definition($places, $transitions);
60+
61+
(new WorkflowValidator())->validate($definition, 'foo');
62+
}
2963
}

src/Symfony/Component/Workflow/Validator/WorkflowValidator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ public function __construct($singlePlace = false)
3131

3232
public function validate(Definition $definition, $name)
3333
{
34+
// Make sure all transitions for one place has unique name.
35+
$places = array_fill_keys($definition->getPlaces(), array());
36+
foreach ($definition->getTransitions() as $transition) {
37+
foreach ($transition->getFroms() as $from) {
38+
if (in_array($transition->getName(), $places[$from])) {
39+
throw new InvalidDefinitionException(sprintf('All transitions for a place must have an unique name. Multiple transitions named "%s" where found for place "%s" in workflow "%s".', $transition->getName(), $from, $name));
40+
}
41+
$places[$from][] = $transition->getName();
42+
}
43+
}
44+
3445
if (!$this->singlePlace) {
3546
return;
3647
}

0 commit comments

Comments
 (0)