1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Grafizzi\Graph\Tests\IG20Test: a component of the Grafizzi library. |
6
|
|
|
* |
7
|
|
|
* (c) 2012 Frédéric G. MARAND <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Grafizzi is free software: you can redistribute it and/or modify it under the |
10
|
|
|
* terms of the GNU Lesser General Public License as published by the Free |
11
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
12
|
|
|
* later version. |
13
|
|
|
* |
14
|
|
|
* Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY |
15
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
16
|
|
|
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
17
|
|
|
* details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
20
|
|
|
* along with Grafizzi, in the COPYING.LESSER.txt file. If not, see |
21
|
|
|
* <http://www.gnu.org/licenses/> |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Grafizzi\Graph\Tests; |
25
|
|
|
|
26
|
|
|
use Grafizzi\Graph\Attribute; |
27
|
|
|
use Grafizzi\Graph\Cluster; |
28
|
|
|
use Grafizzi\Graph\Edge; |
29
|
|
|
use Grafizzi\Graph\Node; |
30
|
|
|
|
31
|
|
|
require 'vendor/autoload.php'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A recreation of Image_GraphViz test20.phpt |
35
|
|
|
* |
36
|
|
|
* Image_GraphViz version author: Philippe Jausions <[email protected]> |
37
|
|
|
* |
38
|
|
|
* Test 20: "Graph with edges on clusters" |
39
|
|
|
* |
40
|
|
|
* "Graph definition taken from GraphViz documentation" |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
class IG20Test extends BaseGraphTest { |
|
|
|
|
43
|
|
|
|
44
|
|
|
public function setUp() : void { |
45
|
|
|
// not strict by default. |
46
|
|
|
parent::setUpExtended(); |
|
|
|
|
47
|
|
|
$graph = $this->Graph; |
48
|
|
|
$dic = $this->dic; |
49
|
|
|
$graph->setDirected(true); |
50
|
|
|
$graph->setAttributes(array( |
51
|
|
|
new Attribute($dic, 'compound', true), |
52
|
|
|
)); |
53
|
|
|
|
54
|
|
|
$nullTitle = array(new Attribute($dic, 'title', NULL)); |
55
|
|
|
$nodes = array(); |
56
|
|
|
|
57
|
|
|
$graph->addChild($cluster0 = new Cluster($dic, 'cluster0', $nullTitle)); |
58
|
|
|
foreach (array('a', 'b', 'c', 'd') as $name) { |
59
|
|
|
$cluster0->addChild($nodes[$name] = new Node($dic, $name)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$graph->addChild($cluster1 = new Cluster($dic, 'cluster1', $nullTitle)); |
63
|
|
|
foreach (array('e', 'f', 'g') as $name) { |
64
|
|
|
$cluster1->addChild($nodes[$name] = new Node($dic, $name)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$graph->addChild(new Edge($dic, $nodes['a'], $nodes['b'])); |
68
|
|
|
$graph->addChild(new Edge($dic, $nodes['a'], $nodes['c'])); |
69
|
|
|
$graph->addChild(new Edge($dic, $nodes['b'], $nodes['d'])); |
70
|
|
|
|
71
|
|
|
// Note how we use getBuildName() instead of getName() for lhead/ltail, |
72
|
|
|
// because this are the strings GraphViz expects. |
73
|
|
|
$graph->addChild(new Edge($dic, $nodes['b'], $nodes['f'], array( |
74
|
|
|
new Attribute($dic, 'lhead', $cluster1->getBuildName()), |
75
|
|
|
))); |
76
|
|
|
$graph->addChild(new Edge($dic, $nodes['c'], $nodes['d'])); |
77
|
|
|
$graph->addChild(new Edge($dic, $nodes['c'], $nodes['g'], array( |
78
|
|
|
new Attribute($dic, 'ltail', $cluster0->getBuildName()), |
79
|
|
|
new Attribute($dic, 'lhead', $cluster1->getBuildName()), |
80
|
|
|
))); |
81
|
|
|
$graph->addChild(new Edge($dic, $nodes['c'], $nodes['e'], array( |
82
|
|
|
new Attribute($dic, 'ltail', $cluster0->getBuildName()), |
83
|
|
|
))); |
84
|
|
|
$graph->addChild(new Edge($dic, $nodes['e'], $nodes['g'])); |
85
|
|
|
$graph->addChild(new Edge($dic, $nodes['e'], $nodes['f'])); |
86
|
|
|
$graph->addChild(new Edge($dic, $nodes['d'], $nodes['e'])); |
87
|
|
|
$graph->addChild(new Edge($dic, $nodes['d'], $nodes['h'] = new Node($dic, 'h', array('implicit' => true)))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Tests g->build() |
92
|
|
|
*/ |
93
|
|
|
public function testBuild() { |
94
|
|
|
$expected = <<<'EOT' |
95
|
|
|
digraph G { |
96
|
|
|
compound=true; |
97
|
|
|
|
98
|
|
|
subgraph cluster0 { |
99
|
|
|
a; |
100
|
|
|
b; |
101
|
|
|
c; |
102
|
|
|
d; |
103
|
|
|
} /* /subgraph cluster0 */ |
104
|
|
|
subgraph cluster1 { |
105
|
|
|
e; |
106
|
|
|
f; |
107
|
|
|
g; |
108
|
|
|
} /* /subgraph cluster1 */ |
109
|
|
|
a -> b; |
110
|
|
|
a -> c; |
111
|
|
|
b -> d; |
112
|
|
|
b -> f [ lhead=cluster1 ]; |
113
|
|
|
c -> d; |
114
|
|
|
c -> g [ ltail=cluster0, lhead=cluster1 ]; |
115
|
|
|
c -> e [ ltail=cluster0 ]; |
116
|
|
|
e -> g; |
117
|
|
|
e -> f; |
118
|
|
|
d -> e; |
119
|
|
|
d -> h; |
120
|
|
|
} /* /digraph G */ |
121
|
|
|
|
122
|
|
|
EOT; |
123
|
|
|
$this->check($expected, "Image_graphViz test 20 passed."); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.