From 3ee876bf4d7ea8202081637e7894c659c2dae5c9 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Sun, 13 Nov 2016 14:39:51 +0100 Subject: [PATCH 1/2] [Workflow] Fixed validator tests and used static function to create workflows --- .../Workflow/Exception/InvalidDefinitionException.php | 2 +- .../Workflow/Tests/Validator/WorkflowValidatorTest.php | 4 ++-- src/Symfony/Component/Workflow/Tests/WorkflowTest.php | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Workflow/Exception/InvalidDefinitionException.php b/src/Symfony/Component/Workflow/Exception/InvalidDefinitionException.php index 1f55a49ab38ab..d41170953564c 100644 --- a/src/Symfony/Component/Workflow/Exception/InvalidDefinitionException.php +++ b/src/Symfony/Component/Workflow/Exception/InvalidDefinitionException.php @@ -16,6 +16,6 @@ * * @author Tobias Nyholm */ -class InvalidDefinitionException extends \LogicException implements ExceptionInterface +class InvalidDefinitionException extends LogicException { } diff --git a/src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php b/src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php index 6e76c2833797e..b2f82d5d5b6aa 100644 --- a/src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php +++ b/src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php @@ -7,7 +7,7 @@ use Symfony\Component\Workflow\Transition; use Symfony\Component\Workflow\Validator\WorkflowValidator; -class WorkflowValidatorTest extends WorkflowTest +class WorkflowValidatorTest extends \PHPUnit_Framework_TestCase { /** * @expectedException \Symfony\Component\Workflow\Exception\InvalidDefinitionException @@ -15,7 +15,7 @@ class WorkflowValidatorTest extends WorkflowTest */ public function testSinglePlaceWorkflowValidatorAndComplexWorkflow() { - $definition = $this->createComplexWorkflow(); + $definition = WorkflowTest::createComplexWorkflow(); (new WorkflowValidator(true))->validate($definition, 'foo'); } diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php index f5141c0fffba1..4b4e625ea86e3 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php @@ -47,7 +47,6 @@ public function testGetMarkingWithEmptyDefinition() public function testGetMarkingWithImpossiblePlace() { $subject = new \stdClass(); - $subject->marking = null; $subject->marking = array('nope' => true); $workflow = new Workflow(new Definition(array(), array()), new MultipleStateMarkingStore()); @@ -210,7 +209,7 @@ public function testGetEnabledTransitions() $this->assertSame('t5', $transitions[0]->getName()); } - protected function createComplexWorkflow() + public static function createComplexWorkflow() { $builder = new DefinitionBuilder(); From cef6f31f4a02edf98374aca79023774a90ecd5ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Thu, 17 Nov 2016 11:10:08 +0100 Subject: [PATCH 2/2] [Workflow] Refactored tests suite --- .../Tests/Dumper/GraphvizDumperTest.php | 41 +++-------------- .../EventListener/AuditTrailListenerTest.php | 12 ++--- .../Tests/Validator/WorkflowValidatorTest.php | 12 ++--- .../Workflow/Tests/WorkflowBuilderTrait.php | 46 +++++++++++++++++++ .../Component/Workflow/Tests/WorkflowTest.php | 31 +------------ .../Validator/StateMachineValidator.php | 28 ++--------- .../Workflow/Validator/WorkflowValidator.php | 19 +++----- 7 files changed, 76 insertions(+), 113 deletions(-) create mode 100644 src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php diff --git a/src/Symfony/Component/Workflow/Tests/Dumper/GraphvizDumperTest.php b/src/Symfony/Component/Workflow/Tests/Dumper/GraphvizDumperTest.php index f24bb003a892a..2b90ea8d5f197 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 @@ public function testWorkflowWithMarking($definition, $marking, $expected) 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 @@ public function provideWorkflowDefinitionWithMarking() 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 c3982f1787608..95da6476140de 100644 --- a/src/Symfony/Component/Workflow/Tests/EventListener/AuditTrailListenerTest.php +++ b/src/Symfony/Component/Workflow/Tests/EventListener/AuditTrailListenerTest.php @@ -4,22 +4,20 @@ 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 b2f82d5d5b6aa..1b5fa67fdb083 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 0000000000000..d7b8de530445c --- /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 4b4e625ea86e3..0a886752d5992 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php @@ -4,7 +4,6 @@ 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 @@ 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 @@ public function testGetEnabledTransitions() $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 60ffd9570f153..16948f6769b6e 100644 --- a/src/Symfony/Component/Workflow/Validator/StateMachineValidator.php +++ b/src/Symfony/Component/Workflow/Validator/StateMachineValidator.php @@ -25,41 +25,21 @@ public function validate(Definition $definition, $name) 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 d3794cd5feeb8..cd31e1fb3e58b 100644 --- a/src/Symfony/Component/Workflow/Validator/WorkflowValidator.php +++ b/src/Symfony/Component/Workflow/Validator/WorkflowValidator.php @@ -31,18 +31,13 @@ public function __construct($singlePlace = false) 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()))); } } }