Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Workflow] Refactored test suite #20548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
*
* @author Tobias Nyholm <[email protected]>
*/
class InvalidDefinitionException extends \LogicException implements ExceptionInterface
class InvalidDefinitionException extends LogicException
{
}
41 changes: 7 additions & 34 deletions src/Symfony/Component/Workflow/Tests/Dumper/GraphvizDumperTest.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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