Skip to content

Commit d40e38d

Browse files
committed
[Workflow] Fixed dumping StateMachine with GraphvizDumper
1 parent 62533f3 commit d40e38d

File tree

2 files changed

+87
-13
lines changed

2 files changed

+87
-13
lines changed

src/Symfony/Component/Workflow/Dumper/GraphvizDumper.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function findTransitions(Definition $definition)
8484
$transitions = array();
8585

8686
foreach ($definition->getTransitions() as $transition) {
87-
$transitions[] = array(
87+
$transitions[$transition->getName()] = array(
8888
'attributes' => array('shape' => 'box', 'regular' => true),
8989
'name' => $transition->getName(),
9090
);
@@ -126,21 +126,20 @@ private function addTransitions(array $transitions)
126126
private function findEdges(Definition $definition)
127127
{
128128
$dotEdges = array();
129+
$getEdge = function ($from, $to, $direction) use (&$dotEdges) {
130+
$dotEdges[$from.$to.$direction] = array(
131+
'from' => $from,
132+
'to' => $to,
133+
'direction' => $direction,
134+
);
135+
};
129136

130137
foreach ($definition->getTransitions() as $transition) {
131138
foreach ($transition->getFroms() as $from) {
132-
$dotEdges[] = array(
133-
'from' => $from,
134-
'to' => $transition->getName(),
135-
'direction' => 'from',
136-
);
139+
$getEdge($from, $transition->getName(), 'from');
137140
}
138141
foreach ($transition->getTos() as $to) {
139-
$dotEdges[] = array(
140-
'from' => $transition->getName(),
141-
'to' => $to,
142-
'direction' => 'to',
143-
);
142+
$getEdge($transition->getName(), $to, 'to');
144143
}
145144
}
146145

@@ -152,10 +151,11 @@ private function addEdges($edges)
152151
$code = '';
153152

154153
foreach ($edges as $edge) {
154+
$from = 'from' === $edge['direction'];
155155
$code .= sprintf(" %s_%s -> %s_%s [style=\"solid\"];\n",
156-
'from' === $edge['direction'] ? 'place' : 'transition',
156+
$from ? 'place' : 'transition',
157157
$this->dotize($edge['from']),
158-
'from' === $edge['direction'] ? 'transition' : 'place',
158+
$from ? 'transition' : 'place',
159159
$this->dotize($edge['to'])
160160
);
161161
}

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
class GraphvizDumperTest extends \PHPUnit_Framework_TestCase
1111
{
12+
/**
13+
* @var GraphvizDumper
14+
*/
1215
private $dumper;
1316

1417
public function setUp()
@@ -36,6 +39,16 @@ public function testWorkflowWithMarking($definition, $marking, $expected)
3639
$this->assertEquals($expected, $dump);
3740
}
3841

42+
/**
43+
* @dataProvider provideStateMachineDefinition
44+
*/
45+
public function testStateMachine($definition, $expected)
46+
{
47+
$dump = $this->dumper->dump($definition);
48+
49+
$this->assertEquals($expected, $dump);
50+
}
51+
3952
public function provideWorkflowDefinitionWithMarking()
4053
{
4154
yield array(
@@ -57,6 +70,11 @@ public function provideWorkflowDefinitionWithoutMarking()
5770
yield array($this->provideSimpleWorkflowDefinition(), $this->provideSimpleWorkflowDumpWithoutMarking());
5871
}
5972

73+
public function provideStateMachineDefinition()
74+
{
75+
yield array($this->provideComplexStateMachineDefinition(), $this->provideComplexStateMachineDump());
76+
}
77+
6078
public function provideComplexWorkflowDefinition()
6179
{
6280
$builder = new DefinitionBuilder();
@@ -85,6 +103,25 @@ public function provideSimpleWorkflowDefinition()
85103
return $builder->build();
86104
}
87105

106+
public function provideComplexStateMachineDefinition()
107+
{
108+
$builder = new DefinitionBuilder();
109+
110+
$builder->addPlaces(range('a', 'g'));
111+
112+
$builder->addTransition(new Transition('t1', 'a', 'c'));
113+
$builder->addTransition(new Transition('t1', 'b', 'c'));
114+
$builder->addTransition(new Transition('t2', 'c', 'd'));
115+
$builder->addTransition(new Transition('t2', 'e', 'd'));
116+
$builder->addTransition(new Transition('t2', 'f', 'd'));
117+
$builder->addTransition(new Transition('t3', 'd', 'g'));
118+
$builder->addTransition(new Transition('t4', 'f', 'e'));
119+
$builder->addTransition(new Transition('t4', 'g', 'e'));
120+
$builder->addTransition(new Transition('t5', 'f', 'b'));
121+
122+
return $builder->build();
123+
}
124+
88125
public function createComplexWorkflowDumpWithMarking()
89126
{
90127
return 'digraph workflow {
@@ -198,6 +235,43 @@ public function provideSimpleWorkflowDumpWithoutMarking()
198235
place_b -> transition_t2 [style="solid"];
199236
transition_t2 -> place_c [style="solid"];
200237
}
238+
';
239+
}
240+
241+
public function provideComplexStateMachineDump()
242+
{
243+
return 'digraph workflow {
244+
ratio="compress" rankdir="LR"
245+
node [fontsize="9" fontname="Arial" color="#333333" fillcolor="lightblue" fixedsize="1" width="1"];
246+
edge [fontsize="9" fontname="Arial" color="#333333" arrowhead="normal" arrowsize="0.5"];
247+
248+
place_a [label="a", shape=circle, style="filled"];
249+
place_b [label="b", shape=circle];
250+
place_c [label="c", shape=circle];
251+
place_d [label="d", shape=circle];
252+
place_e [label="e", shape=circle];
253+
place_f [label="f", shape=circle];
254+
place_g [label="g", shape=circle];
255+
transition_t1 [label="t1", shape=box, shape="box", regular="1"];
256+
transition_t2 [label="t2", shape=box, shape="box", regular="1"];
257+
transition_t3 [label="t3", shape=box, shape="box", regular="1"];
258+
transition_t4 [label="t4", shape=box, shape="box", regular="1"];
259+
transition_t5 [label="t5", shape=box, shape="box", regular="1"];
260+
place_a -> transition_t1 [style="solid"];
261+
transition_t1 -> place_c [style="solid"];
262+
place_b -> transition_t1 [style="solid"];
263+
place_c -> transition_t2 [style="solid"];
264+
transition_t2 -> place_d [style="solid"];
265+
place_e -> transition_t2 [style="solid"];
266+
place_f -> transition_t2 [style="solid"];
267+
place_d -> transition_t3 [style="solid"];
268+
transition_t3 -> place_g [style="solid"];
269+
place_f -> transition_t4 [style="solid"];
270+
transition_t4 -> place_e [style="solid"];
271+
place_g -> transition_t4 [style="solid"];
272+
place_f -> transition_t5 [style="solid"];
273+
transition_t5 -> place_b [style="solid"];
274+
}
201275
';
202276
}
203277
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy