Skip to content

Commit 30a0143

Browse files
committed
bug symfony#21271 [Workflow] Added new validator to make sure each place has unique translation names (Nyholm)
This PR was submitted for the master branch but it was merged into the 3.2 branch instead (closes symfony#21271). Discussion ---------- [Workflow] Added new validator to make sure each place has unique translation names | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | n/a A definition where a place that has transitions with the same name is an invalid definition. ```yaml invalid1: places: ['a', 'b', 'c'] transitions: t1: from: a to: b t1: from: a to: c valid1: places: ['a', 'b', 'c'] transitions: t1: from: a to: b t2: from: a to: c valid2: places: ['a', 'b', 'c'] transitions: t1: from: a to: b t1: from: b to: c valid3: places: ['a', 'b', 'c', 'd'] transitions: t1: from: ['a', 'b'] to: d t2: from: ['a', 'b'] to: c ``` FYI @lyrixx Commits ------- eece8ad [Workflow] Added new validator to make sure each place has unique translation names
2 parents 9d09059 + eece8ad commit 30a0143

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)