Skip to content

Commit

Permalink
minor #20548 [Workflow] Refactored test suite (HeahDude, lyrixx)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.2-dev branch.

Discussion
----------

[Workflow] Refactored test suite

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20507
| License       | MIT
| Doc PR        | -

Commits
-------

cef6f31 [Workflow] Refactored tests suite
3ee876b [Workflow] Fixed validator tests and used static function to create workflows
  • Loading branch information
lyrixx committed Nov 17, 2016
2 parents 05c3e86 + cef6f31 commit 9868b05
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 115 deletions.
Expand Up @@ -16,6 +16,6 @@
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class InvalidDefinitionException extends \LogicException implements ExceptionInterface
class InvalidDefinitionException extends LogicException
{
}
41 changes: 7 additions & 34 deletions src/Symfony/Component/Workflow/Tests/Dumper/GraphvizDumperTest.php
Expand Up @@ -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()
Expand Down Expand Up @@ -39,50 +40,22 @@ 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(),
);
}

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()
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -2,13 +2,13 @@

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 WorkflowTest
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.
Expand All @@ -22,9 +22,7 @@ public function testSinglePlaceWorkflowValidatorAndComplexWorkflow()

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');
}
Expand Down
46 changes: 46 additions & 0 deletions src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php
@@ -0,0 +1,46 @@
<?php

namespace Symfony\Component\Workflow\Tests;

use Symfony\Component\Workflow\Definition;
use Symfony\Component\Workflow\Transition;

trait WorkflowBuilderTrait
{
private function createComplexWorkflow()
{
$places = range('a', 'g');

$transitions = array();
$transitions[] = new Transition('t1', 'a', array('b', 'c'));
$transitions[] = new Transition('t2', array('b', 'c'), 'd');
$transitions[] = new Transition('t3', 'd', 'e');
$transitions[] = new Transition('t4', 'd', 'f');
$transitions[] = new Transition('t5', 'e', 'g');
$transitions[] = new Transition('t6', 'f', 'g');

return new Definition($places, $transitions);

// The graph looks like:
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
// | a | --> | 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);
}
}
32 changes: 2 additions & 30 deletions src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Expand Up @@ -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;
Expand All @@ -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".
Expand Down Expand Up @@ -47,7 +48,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());

Expand Down Expand Up @@ -209,34 +209,6 @@ public function testGetEnabledTransitions()
$this->assertCount(1, $transitions);
$this->assertSame('t5', $transitions[0]->getName());
}

protected 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
Expand Down
28 changes: 4 additions & 24 deletions src/Symfony/Component/Workflow/Validator/StateMachineValidator.php
Expand Up @@ -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;
}
}
Expand Down
19 changes: 7 additions & 12 deletions src/Symfony/Component/Workflow/Validator/WorkflowValidator.php
Expand Up @@ -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())));
}
}
}
Expand Down

0 comments on commit 9868b05

Please sign in to comment.