Skip to content

Commit f5fc321

Browse files
committed
[Workflow] Refactored tests suite
1 parent 3ee876b commit f5fc321

File tree

7 files changed

+96
-113
lines changed

7 files changed

+96
-113
lines changed

src/Symfony/Component/Workflow/Tests/Dumper/GraphvizDumperTest.php

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Symfony\Component\Workflow\Tests\Dumper;
44

5-
use Symfony\Component\Workflow\DefinitionBuilder;
65
use Symfony\Component\Workflow\Dumper\GraphvizDumper;
76
use Symfony\Component\Workflow\Marking;
8-
use Symfony\Component\Workflow\Transition;
7+
use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait;
98

109
class GraphvizDumperTest extends \PHPUnit_Framework_TestCase
1110
{
11+
use WorkflowBuilderTrait;
12+
1213
private $dumper;
1314

1415
public function setUp()
@@ -39,50 +40,22 @@ public function testWorkflowWithMarking($definition, $marking, $expected)
3940
public function provideWorkflowDefinitionWithMarking()
4041
{
4142
yield array(
42-
$this->provideComplexWorkflowDefinition(),
43+
$this->createComplexWorkflow(),
4344
new Marking(array('b' => 1)),
4445
$this->createComplexWorkflowDumpWithMarking(),
4546
);
4647

4748
yield array(
48-
$this->provideSimpleWorkflowDefinition(),
49+
$this->createSimpleWorkflowDefinition(),
4950
new Marking(array('c' => 1, 'd' => 1)),
5051
$this->createSimpleWorkflowDumpWithMarking(),
5152
);
5253
}
5354

5455
public function provideWorkflowDefinitionWithoutMarking()
5556
{
56-
yield array($this->provideComplexWorkflowDefinition(), $this->provideComplexWorkflowDumpWithoutMarking());
57-
yield array($this->provideSimpleWorkflowDefinition(), $this->provideSimpleWorkflowDumpWithoutMarking());
58-
}
59-
60-
public function provideComplexWorkflowDefinition()
61-
{
62-
$builder = new DefinitionBuilder();
63-
64-
$builder->addPlaces(range('a', 'g'));
65-
66-
$builder->addTransition(new Transition('t1', 'a', array('b', 'c')));
67-
$builder->addTransition(new Transition('t2', array('b', 'c'), 'd'));
68-
$builder->addTransition(new Transition('t3', 'd', 'e'));
69-
$builder->addTransition(new Transition('t4', 'd', 'f'));
70-
$builder->addTransition(new Transition('t5', 'e', 'g'));
71-
$builder->addTransition(new Transition('t6', 'f', 'g'));
72-
73-
return $builder->build();
74-
}
75-
76-
public function provideSimpleWorkflowDefinition()
77-
{
78-
$builder = new DefinitionBuilder();
79-
80-
$builder->addPlaces(range('a', 'c'));
81-
82-
$builder->addTransition(new Transition('t1', 'a', 'b'));
83-
$builder->addTransition(new Transition('t2', 'b', 'c'));
84-
85-
return $builder->build();
57+
yield array($this->createComplexWorkflow(), $this->provideComplexWorkflowDumpWithoutMarking());
58+
yield array($this->createSimpleWorkflowDefinition(), $this->provideSimpleWorkflowDumpWithoutMarking());
8659
}
8760

8861
public function createComplexWorkflowDumpWithMarking()

src/Symfony/Component/Workflow/Tests/EventListener/AuditTrailListenerTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44

55
use Psr\Log\AbstractLogger;
66
use Symfony\Component\EventDispatcher\EventDispatcher;
7-
use Symfony\Component\Workflow\Definition;
87
use Symfony\Component\Workflow\EventListener\AuditTrailListener;
98
use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore;
9+
use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait;
10+
use Symfony\Component\Workflow\Tests\createSimpleWorkflowDefinition;
1011
use Symfony\Component\Workflow\Transition;
1112
use Symfony\Component\Workflow\Workflow;
1213

1314
class AuditTrailListenerTest extends \PHPUnit_Framework_TestCase
1415
{
16+
use WorkflowBuilderTrait;
17+
1518
public function testItWorks()
1619
{
17-
$transitions = array(
18-
new Transition('t1', 'a', 'b'),
19-
new Transition('t2', 'a', 'b'),
20-
);
21-
22-
$definition = new Definition(array('a', 'b'), $transitions);
20+
$definition = $this->createSimpleWorkflowDefinition();
2321

2422
$object = new \stdClass();
2523
$object->marking = null;

src/Symfony/Component/Workflow/Tests/Validator/WorkflowValidatorTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,27 @@
22

33
namespace Symfony\Component\Workflow\Tests\Validator;
44

5-
use Symfony\Component\Workflow\Definition;
6-
use Symfony\Component\Workflow\Tests\WorkflowTest;
7-
use Symfony\Component\Workflow\Transition;
5+
use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait;
86
use Symfony\Component\Workflow\Validator\WorkflowValidator;
97

108
class WorkflowValidatorTest extends \PHPUnit_Framework_TestCase
119
{
10+
use WorkflowBuilderTrait;
11+
1212
/**
1313
* @expectedException \Symfony\Component\Workflow\Exception\InvalidDefinitionException
1414
* @expectedExceptionMessage The marking store of workflow "foo" can not store many places.
1515
*/
1616
public function testSinglePlaceWorkflowValidatorAndComplexWorkflow()
1717
{
18-
$definition = WorkflowTest::createComplexWorkflow();
18+
$definition = $this->createComplexWorkflow();
1919

2020
(new WorkflowValidator(true))->validate($definition, 'foo');
2121
}
2222

2323
public function testSinglePlaceWorkflowValidatorAndSimpleWorkflow()
2424
{
25-
$places = array('a', 'b');
26-
$transition = new Transition('t1', 'a', 'b');
27-
$definition = new Definition($places, array($transition));
25+
$definition = $this->createSimpleWorkflowDefinition();
2826

2927
(new WorkflowValidator(true))->validate($definition, 'foo');
3028
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Symfony\Component\Workflow\Tests;
4+
5+
use Symfony\Component\Workflow\Definition;
6+
use Symfony\Component\Workflow\Transition;
7+
8+
trait WorkflowBuilderTrait
9+
{
10+
private function createComplexWorkflow()
11+
{
12+
$places = range('a', 'g');
13+
14+
$transitions = array();
15+
$transitions[] = new Transition('t1', 'a', array('b', 'c'));
16+
$transitions[] = new Transition('t2', array('b', 'c'), 'd');
17+
$transitions[] = new Transition('t3', 'd', 'e');
18+
$transitions[] = new Transition('t4', 'd', 'f');
19+
$transitions[] = new Transition('t5', 'e', 'g');
20+
$transitions[] = new Transition('t6', 'f', 'g');
21+
22+
return new Definition($places, $transitions);
23+
24+
// The graph looks like:
25+
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
26+
// | a | --> | t1 | --> | c | --> | t2 | --> | d | --> | t4 | --> | f | --> | t6 | --> | g |
27+
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
28+
// | ^ | ^
29+
// | | | |
30+
// v | v |
31+
// +----+ | +----+ +----+ +----+ |
32+
// | b | ----------------+ | t3 | --> | e | --> | t5 | -----------------+
33+
// +----+ +----+ +----+ +----+
34+
}
35+
36+
public function createSimpleWorkflowDefinition()
37+
{
38+
$places = range('a', 'c');
39+
40+
$transitions = array();
41+
$transitions[] = new Transition('t1', 'a', 'b');
42+
$transitions[] = new Transition('t2', 'b', 'c');
43+
44+
return new Definition($places, $transitions);
45+
}
46+
}

src/Symfony/Component/Workflow/Tests/WorkflowTest.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Symfony\Component\EventDispatcher\EventDispatcher;
66
use Symfony\Component\Workflow\Definition;
7-
use Symfony\Component\Workflow\DefinitionBuilder;
87
use Symfony\Component\Workflow\Event\GuardEvent;
98
use Symfony\Component\Workflow\Marking;
109
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
@@ -14,6 +13,8 @@
1413

1514
class WorkflowTest extends \PHPUnit_Framework_TestCase
1615
{
16+
use WorkflowBuilderTrait;
17+
1718
/**
1819
* @expectedException \Symfony\Component\Workflow\Exception\LogicException
1920
* @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()
208209
$this->assertCount(1, $transitions);
209210
$this->assertSame('t5', $transitions[0]->getName());
210211
}
211-
212-
public static function createComplexWorkflow()
213-
{
214-
$builder = new DefinitionBuilder();
215-
216-
$builder->addPlaces(range('a', 'g'));
217-
218-
$builder->addTransition(new Transition('t1', 'a', array('b', 'c')));
219-
$builder->addTransition(new Transition('t2', array('b', 'c'), 'd'));
220-
$builder->addTransition(new Transition('t3', 'd', 'e'));
221-
$builder->addTransition(new Transition('t4', 'd', 'f'));
222-
$builder->addTransition(new Transition('t5', 'e', 'g'));
223-
$builder->addTransition(new Transition('t6', 'f', 'g'));
224-
225-
return $builder->build();
226-
227-
// The graph looks like:
228-
//
229-
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
230-
// | a | --> | t1 | --> | c | --> | t2 | --> | d | --> | t4 | --> | f | --> | t6 | --> | g |
231-
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
232-
// | ^ | ^
233-
// | | | |
234-
// v | v |
235-
// +----+ | +----+ +----+ +----+ |
236-
// | b | ----------------+ | t3 | --> | e | --> | t5 | -----------------+
237-
// +----+ +----+ +----+ +----+
238-
}
239212
}
240213

241214
class EventDispatcherMock implements \Symfony\Component\EventDispatcher\EventDispatcherInterface

src/Symfony/Component/Workflow/Validator/StateMachineValidator.php

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,36 @@ public function validate(Definition $definition, $name)
2525
foreach ($definition->getTransitions() as $transition) {
2626
// Make sure that each transition has exactly one TO
2727
if (1 !== count($transition->getTos())) {
28-
throw new InvalidDefinitionException(
29-
sprintf(
30-
'A transition in StateMachine can only have one output. But the transition "%s" in StateMachine "%s" has %d outputs.',
31-
$transition->getName(),
32-
$name,
33-
count($transition->getTos())
34-
)
35-
);
28+
throw new InvalidDefinitionException(sprintf(
29+
'A transition in StateMachine can only have one output. But the transition "%s" in StateMachine "%s" has %d outputs.',
30+
$transition->getName(),
31+
$name,
32+
count($transition->getTos())
33+
));
3634
}
3735

3836
// Make sure that each transition has exactly one FROM
3937
$froms = $transition->getFroms();
4038
if (1 !== count($froms)) {
41-
throw new InvalidDefinitionException(
42-
sprintf(
43-
'A transition in StateMachine can only have one input. But the transition "%s" in StateMachine "%s" has %d inputs.',
44-
$transition->getName(),
45-
$name,
46-
count($transition->getTos())
47-
)
48-
);
39+
throw new InvalidDefinitionException(sprintf(
40+
'A transition in StateMachine can only have one input. But the transition "%s" in StateMachine "%s" has %d inputs.',
41+
$transition->getName(),
42+
$name,
43+
count($transition->getTos())
44+
));
4945
}
5046

5147
// Enforcing uniqueness of the names of transitions starting at each node
5248
$from = reset($froms);
5349
if (isset($transitionFromNames[$from][$transition->getName()])) {
54-
throw new InvalidDefinitionException(
55-
sprintf(
56-
'A transition from a place/state must have an unique name. Multiple transitions named "%s" from place/state "%s" where found on StateMachine "%s". ',
57-
$transition->getName(),
58-
$from,
59-
$name
60-
)
61-
);
50+
throw new InvalidDefinitionException(sprintf(
51+
'A transition from a place/state must have an unique name. Multiple transitions named "%s" from place/state "%s" where found on StateMachine "%s". ',
52+
$transition->getName(),
53+
$from,
54+
$name
55+
));
6256
}
57+
6358
$transitionFromNames[$from][$transition->getName()] = true;
6459
}
6560
}

src/Symfony/Component/Workflow/Validator/WorkflowValidator.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ public function __construct($singlePlace = false)
3131

3232
public function validate(Definition $definition, $name)
3333
{
34-
if ($this->singlePlace) {
35-
foreach ($definition->getTransitions() as $transition) {
36-
if (1 < count($transition->getTos())) {
37-
throw new InvalidDefinitionException(
38-
sprintf(
39-
'The marking store of workflow "%s" can not store many places. But the transition "%s" has too many output (%d). Only one is accepted.',
40-
$name,
41-
$transition->getName(),
42-
count($transition->getTos())
43-
)
44-
);
45-
}
34+
if (!$this->singlePlace) {
35+
return;
36+
}
37+
38+
foreach ($definition->getTransitions() as $transition) {
39+
if (1 < count($transition->getTos())) {
40+
throw new InvalidDefinitionException(sprintf(
41+
'The marking store of workflow "%s" can not store many places. But the transition "%s" has too many output (%d). Only one is accepted.',
42+
$name,
43+
$transition->getName(),
44+
count($transition->getTos())
45+
));
4646
}
4747
}
4848
}

0 commit comments

Comments
 (0)