1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Grafizzi\Graph\Tests\EdgeTest: 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
|
|
|
/** @noinspection PhpIncludeInspection */ |
27
|
|
|
require 'vendor/autoload.php'; |
28
|
|
|
|
29
|
|
|
use Grafizzi\Graph\Attribute; |
30
|
|
|
use Grafizzi\Graph\Edge; |
31
|
|
|
use Grafizzi\Graph\Node; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Edge test case. |
35
|
|
|
*/ |
36
|
|
|
class EdgeTest extends BaseGraphTest { |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Edge |
40
|
|
|
*/ |
41
|
|
|
private $Edge; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Attribute |
45
|
|
|
*/ |
46
|
|
|
private $Attribute; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Prepares the environment before running a test. |
50
|
|
|
*/ |
51
|
|
|
protected function setUp() : void { |
52
|
|
|
parent::setUpExtended(); |
|
|
|
|
53
|
|
|
$src = new Node($this->dic, 'source'); |
54
|
|
|
$dst = new Node($this->dic, 'destination'); |
55
|
|
|
$this->Attribute = new Attribute($this->dic, 'label', 'Source to Destination'); |
56
|
|
|
$this->Edge = new Edge($this->dic, $src, $dst); |
57
|
|
|
$this->Edge->setAttribute($this->Attribute); |
58
|
|
|
foreach (array($src, $dst, $this->Edge) as $child) { |
59
|
|
|
$this->Graph->addChild($child); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Cleans up the environment after running a test. |
65
|
|
|
*/ |
66
|
|
|
protected function tearDown() : void { |
67
|
|
|
$this->Edge = null; |
68
|
|
|
parent::tearDown(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Tests Edge->__construct() |
73
|
|
|
*/ |
74
|
|
|
public function test__construct() { |
75
|
|
|
$this->assertEquals($this->Attribute, $this->Edge->getAttributeByName('label'), |
76
|
|
|
'Edge is correctly labeled'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Tests Edge->build() |
81
|
|
|
* |
82
|
|
|
* @TODO test graph build |
83
|
|
|
*/ |
84
|
|
|
public function testBuild() { |
85
|
|
|
$dot = $this->Edge->build($this->Graph->getDirected()); |
86
|
|
|
$this->assertEquals(<<<EOT |
87
|
|
|
source -> destination [ label="Source to Destination" ]; |
88
|
|
|
|
89
|
|
|
EOT |
90
|
|
|
, $dot, "Edge builds correctly"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Common logic for the testBuild* test methods. |
95
|
|
|
* |
96
|
|
|
* @param string $expected |
97
|
|
|
* The expected GraphViz output. |
98
|
|
|
* @param array $edges |
99
|
|
|
* |
100
|
|
|
* @internal param array $toSet An array of edges to add.* An array of edges to add. |
101
|
|
|
*/ |
102
|
|
|
public function buildTestHelper($expected, array $edges) { |
103
|
|
|
$this->Edge->removeAttribute($this->Attribute); |
104
|
|
|
$toSet = array(); |
105
|
|
|
foreach ($edges as $edge) { |
106
|
|
|
list($name, $value) = $edge; |
107
|
|
|
$toSet[] = new Attribute($this->dic, $name, $value); |
108
|
|
|
} |
109
|
|
|
$this->Edge->setAttributes($toSet); |
110
|
|
|
$actual = $this->Edge->build(); |
111
|
|
|
$this->assertEquals($expected, $actual); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testBuildAttributesNormal() { |
115
|
|
|
$expected = <<<EOT |
116
|
|
|
source -> destination [ foo=bar, baz=quux ]; |
117
|
|
|
|
118
|
|
|
EOT; |
119
|
|
|
$this->buildTestHelper($expected, array( |
120
|
|
|
array('foo', 'bar'), |
121
|
|
|
array('baz', 'quux'), |
122
|
|
|
)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Attribute list with empty title in middle on edge bound to root graph. |
126
|
|
View Code Duplication |
public function testBuildAttributesEmptyMiddle() { |
|
|
|
|
127
|
|
|
$expected = <<<EOT |
128
|
|
|
source -> destination [ foo=bar, baz=quux ]; |
129
|
|
|
|
130
|
|
|
EOT; |
131
|
|
|
|
132
|
|
|
$this->buildTestHelper($expected, array( |
133
|
|
|
array('foo', 'bar'), |
134
|
|
|
array('title', ''), |
135
|
|
|
array('baz', 'quux'), |
136
|
|
|
)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Attribute list with empty title as single attribute on edge bound to root graph. |
140
|
|
|
public function testBuildAttributesOnlyEmpty() { |
141
|
|
|
$expected = <<<EOT |
142
|
|
|
source -> destination; |
143
|
|
|
|
144
|
|
|
EOT; |
145
|
|
|
|
146
|
|
|
$this->buildTestHelper($expected, array( |
147
|
|
|
array('title', ''), |
148
|
|
|
)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Attribute list with empty title as last attribute on edge bound to root graph. |
152
|
|
View Code Duplication |
public function testBuildAttributesEmptyLast() { |
|
|
|
|
153
|
|
|
$expected = <<<EOT |
154
|
|
|
source -> destination [ foo=bar, baz=quux ]; |
155
|
|
|
|
156
|
|
|
EOT; |
157
|
|
|
|
158
|
|
|
$this->buildTestHelper($expected, array( |
159
|
|
|
array('foo', 'bar'), |
160
|
|
|
array('baz', 'quux'), |
161
|
|
|
array('title', ''), |
162
|
|
|
)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Tests Edge::getAllowedChildTypes() |
167
|
|
|
*/ |
168
|
|
|
public function testGetAllowedChildTypes() { |
169
|
|
|
$this->assertEmpty(Edge::getAllowedChildTypes()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Tests Edge::getType() |
174
|
|
|
*/ |
175
|
|
|
public function testGetType() { |
176
|
|
|
$this->assertEquals('edge', $this->Edge->getType()); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.