[Workflow] Refactored tests suite

This commit is contained in:
Grégoire Pineau 2016-11-17 11:10:08 +01:00
parent 3ee876bf4d
commit cef6f31f4a
7 changed files with 76 additions and 113 deletions

View File

@ -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()

View File

@ -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;

View File

@ -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');
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -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;
}
}

View File

@ -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())));
}
}
}