diff --git a/src/Symfony/Component/Workflow/Tests/Dumper/GraphvizDumperTest.php b/src/Symfony/Component/Workflow/Tests/Dumper/GraphvizDumperTest.php index f24bb003a8..2b90ea8d5f 100644 --- a/src/Symfony/Component/Workflow/Tests/Dumper/GraphvizDumperTest.php +++ b/src/Symfony/Component/Workflow/Tests/Dumper/GraphvizDumperTest.php @@ -2,13 +2,14 @@ namespace Symfony\Component\Workflow\Tests\Dumper; -use Symfony\Component\Workflow\DefinitionBuilder; use Symfony\Component\Workflow\Dumper\GraphvizDumper; use Symfony\Component\Workflow\Marking; -use Symfony\Component\Workflow\Transition; +use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait; class GraphvizDumperTest extends \PHPUnit_Framework_TestCase { + use WorkflowBuilderTrait; + private $dumper; public function setUp() @@ -39,13 +40,13 @@ class GraphvizDumperTest extends \PHPUnit_Framework_TestCase public function provideWorkflowDefinitionWithMarking() { yield array( - $this->provideComplexWorkflowDefinition(), + $this->createComplexWorkflow(), new Marking(array('b' => 1)), $this->createComplexWorkflowDumpWithMarking(), ); yield array( - $this->provideSimpleWorkflowDefinition(), + $this->createSimpleWorkflowDefinition(), new Marking(array('c' => 1, 'd' => 1)), $this->createSimpleWorkflowDumpWithMarking(), ); @@ -53,36 +54,8 @@ class GraphvizDumperTest extends \PHPUnit_Framework_TestCase public function provideWorkflowDefinitionWithoutMarking() { - yield array($this->provideComplexWorkflowDefinition(), $this->provideComplexWorkflowDumpWithoutMarking()); - yield array($this->provideSimpleWorkflowDefinition(), $this->provideSimpleWorkflowDumpWithoutMarking()); - } - - public function provideComplexWorkflowDefinition() - { - $builder = new DefinitionBuilder(); - - $builder->addPlaces(range('a', 'g')); - - $builder->addTransition(new Transition('t1', 'a', array('b', 'c'))); - $builder->addTransition(new Transition('t2', array('b', 'c'), 'd')); - $builder->addTransition(new Transition('t3', 'd', 'e')); - $builder->addTransition(new Transition('t4', 'd', 'f')); - $builder->addTransition(new Transition('t5', 'e', 'g')); - $builder->addTransition(new Transition('t6', 'f', 'g')); - - return $builder->build(); - } - - public function provideSimpleWorkflowDefinition() - { - $builder = new DefinitionBuilder(); - - $builder->addPlaces(range('a', 'c')); - - $builder->addTransition(new Transition('t1', 'a', 'b')); - $builder->addTransition(new Transition('t2', 'b', 'c')); - - return $builder->build(); + yield array($this->createComplexWorkflow(), $this->provideComplexWorkflowDumpWithoutMarking()); + yield array($this->createSimpleWorkflowDefinition(), $this->provideSimpleWorkflowDumpWithoutMarking()); } public function createComplexWorkflowDumpWithMarking() diff --git a/src/Symfony/Component/Workflow/Tests/EventListener/AuditTrailListenerTest.php b/src/Symfony/Component/Workflow/Tests/EventListener/AuditTrailListenerTest.php index c3982f1787..95da647614 100644 --- a/src/Symfony/Component/Workflow/Tests/EventListener/AuditTrailListenerTest.php +++ b/src/Symfony/Component/Workflow/Tests/EventListener/AuditTrailListenerTest.php @@ -4,22 +4,20 @@ namespace Symfony\Component\Workflow\Tests\EventListener; use Psr\Log\AbstractLogger; use Symfony\Component\EventDispatcher\EventDispatcher; -use Symfony\Component\Workflow\Definition; use Symfony\Component\Workflow\EventListener\AuditTrailListener; use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore; +use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait; +use Symfony\Component\Workflow\Tests\createSimpleWorkflowDefinition; use Symfony\Component\Workflow\Transition; use Symfony\Component\Workflow\Workflow; class AuditTrailListenerTest extends \PHPUnit_Framework_TestCase { + use WorkflowBuilderTrait; + public function testItWorks() { - $transitions = array( - new Transition('t1', 'a', 'b'), - new Transition('t2', 'a', 'b'), - ); - - $definition = new Definition(array('a', 'b'), $transitions); + $definition = $this->createSimpleWorkflowDefinition(); $object = new \stdClass(); $object->marking = null; diff --git a/src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php b/src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php index b2f82d5d5b..1b5fa67fdb 100644 --- a/src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php +++ b/src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php @@ -2,29 +2,27 @@ namespace Symfony\Component\Workflow\Tests\Validator; -use Symfony\Component\Workflow\Definition; -use Symfony\Component\Workflow\Tests\WorkflowTest; -use Symfony\Component\Workflow\Transition; +use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait; use Symfony\Component\Workflow\Validator\WorkflowValidator; class WorkflowValidatorTest extends \PHPUnit_Framework_TestCase { + use WorkflowBuilderTrait; + /** * @expectedException \Symfony\Component\Workflow\Exception\InvalidDefinitionException * @expectedExceptionMessage The marking store of workflow "foo" can not store many places. */ public function testSinglePlaceWorkflowValidatorAndComplexWorkflow() { - $definition = WorkflowTest::createComplexWorkflow(); + $definition = $this->createComplexWorkflow(); (new WorkflowValidator(true))->validate($definition, 'foo'); } public function testSinglePlaceWorkflowValidatorAndSimpleWorkflow() { - $places = array('a', 'b'); - $transition = new Transition('t1', 'a', 'b'); - $definition = new Definition($places, array($transition)); + $definition = $this->createSimpleWorkflowDefinition(); (new WorkflowValidator(true))->validate($definition, 'foo'); } diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php b/src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php new file mode 100644 index 0000000000..d7b8de5304 --- /dev/null +++ b/src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php @@ -0,0 +1,46 @@ + | t1 | --> | c | --> | t2 | --> | d | --> | t4 | --> | f | --> | t6 | --> | g | + // +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+ + // | ^ | ^ + // | | | | + // v | v | + // +----+ | +----+ +----+ +----+ | + // | b | ----------------+ | t3 | --> | e | --> | t5 | -----------------+ + // +----+ +----+ +----+ +----+ + } + + public function createSimpleWorkflowDefinition() + { + $places = range('a', 'c'); + + $transitions = array(); + $transitions[] = new Transition('t1', 'a', 'b'); + $transitions[] = new Transition('t2', 'b', 'c'); + + return new Definition($places, $transitions); + } +} diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php index 4b4e625ea8..0a886752d5 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php @@ -4,7 +4,6 @@ namespace Symfony\Component\Workflow\Tests; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Workflow\Definition; -use Symfony\Component\Workflow\DefinitionBuilder; use Symfony\Component\Workflow\Event\GuardEvent; use Symfony\Component\Workflow\Marking; use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface; @@ -14,6 +13,8 @@ use Symfony\Component\Workflow\Workflow; class WorkflowTest extends \PHPUnit_Framework_TestCase { + use WorkflowBuilderTrait; + /** * @expectedException \Symfony\Component\Workflow\Exception\LogicException * @expectedExceptionMessage The value returned by the MarkingStore is not an instance of "Symfony\Component\Workflow\Marking" for workflow "unnamed". @@ -208,34 +209,6 @@ class WorkflowTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $transitions); $this->assertSame('t5', $transitions[0]->getName()); } - - public static function createComplexWorkflow() - { - $builder = new DefinitionBuilder(); - - $builder->addPlaces(range('a', 'g')); - - $builder->addTransition(new Transition('t1', 'a', array('b', 'c'))); - $builder->addTransition(new Transition('t2', array('b', 'c'), 'd')); - $builder->addTransition(new Transition('t3', 'd', 'e')); - $builder->addTransition(new Transition('t4', 'd', 'f')); - $builder->addTransition(new Transition('t5', 'e', 'g')); - $builder->addTransition(new Transition('t6', 'f', 'g')); - - return $builder->build(); - - // The graph looks like: - // - // +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+ - // | a | --> | t1 | --> | c | --> | t2 | --> | d | --> | t4 | --> | f | --> | t6 | --> | g | - // +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+ - // | ^ | ^ - // | | | | - // v | v | - // +----+ | +----+ +----+ +----+ | - // | b | ----------------+ | t3 | --> | e | --> | t5 | -----------------+ - // +----+ +----+ +----+ +----+ - } } class EventDispatcherMock implements \Symfony\Component\EventDispatcher\EventDispatcherInterface diff --git a/src/Symfony/Component/Workflow/Validator/StateMachineValidator.php b/src/Symfony/Component/Workflow/Validator/StateMachineValidator.php index 60ffd9570f..16948f6769 100644 --- a/src/Symfony/Component/Workflow/Validator/StateMachineValidator.php +++ b/src/Symfony/Component/Workflow/Validator/StateMachineValidator.php @@ -25,41 +25,21 @@ class StateMachineValidator implements DefinitionValidatorInterface foreach ($definition->getTransitions() as $transition) { // Make sure that each transition has exactly one TO if (1 !== count($transition->getTos())) { - throw new InvalidDefinitionException( - sprintf( - 'A transition in StateMachine can only have one output. But the transition "%s" in StateMachine "%s" has %d outputs.', - $transition->getName(), - $name, - count($transition->getTos()) - ) - ); + throw new InvalidDefinitionException(sprintf('A transition in StateMachine can only have one output. But the transition "%s" in StateMachine "%s" has %d outputs.', $transition->getName(), $name, count($transition->getTos()))); } // Make sure that each transition has exactly one FROM $froms = $transition->getFroms(); if (1 !== count($froms)) { - throw new InvalidDefinitionException( - sprintf( - 'A transition in StateMachine can only have one input. But the transition "%s" in StateMachine "%s" has %d inputs.', - $transition->getName(), - $name, - count($transition->getTos()) - ) - ); + throw new InvalidDefinitionException(sprintf('A transition in StateMachine can only have one input. But the transition "%s" in StateMachine "%s" has %d inputs.', $transition->getName(), $name, count($transition->getTos()))); } // Enforcing uniqueness of the names of transitions starting at each node $from = reset($froms); if (isset($transitionFromNames[$from][$transition->getName()])) { - throw new InvalidDefinitionException( - sprintf( - 'A transition from a place/state must have an unique name. Multiple transitions named "%s" from place/state "%s" where found on StateMachine "%s". ', - $transition->getName(), - $from, - $name - ) - ); + throw new InvalidDefinitionException(sprintf('A transition from a place/state must have an unique name. Multiple transitions named "%s" from place/state "%s" where found on StateMachine "%s". ', $transition->getName(), $from, $name)); } + $transitionFromNames[$from][$transition->getName()] = true; } } diff --git a/src/Symfony/Component/Workflow/Validator/WorkflowValidator.php b/src/Symfony/Component/Workflow/Validator/WorkflowValidator.php index d3794cd5fe..cd31e1fb3e 100644 --- a/src/Symfony/Component/Workflow/Validator/WorkflowValidator.php +++ b/src/Symfony/Component/Workflow/Validator/WorkflowValidator.php @@ -31,18 +31,13 @@ class WorkflowValidator implements DefinitionValidatorInterface public function validate(Definition $definition, $name) { - if ($this->singlePlace) { - foreach ($definition->getTransitions() as $transition) { - if (1 < count($transition->getTos())) { - throw new InvalidDefinitionException( - sprintf( - 'The marking store of workflow "%s" can not store many places. But the transition "%s" has too many output (%d). Only one is accepted.', - $name, - $transition->getName(), - count($transition->getTos()) - ) - ); - } + if (!$this->singlePlace) { + return; + } + + foreach ($definition->getTransitions() as $transition) { + if (1 < count($transition->getTos())) { + throw new InvalidDefinitionException(sprintf('The marking store of workflow "%s" can not store many places. But the transition "%s" has too many output (%d). Only one is accepted.', $name, $transition->getName(), count($transition->getTos()))); } } }