Skip to content

Commit 577ce76

Browse files
authored
Merge pull request #149 from TomzxForks/bugs/cannot-destroy-vertex-with-loop-edge
Cannot delete a vertex with a loop
2 parents 5ef1be0 + 435b3e8 commit 577ce76

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/Vertex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function getVerticesEdgeFrom()
354354
*/
355355
public function destroy()
356356
{
357-
foreach ($this->edges as $edge) {
357+
foreach ($this->getEdges()->getEdgesDistinct() as $edge) {
358358
$edge->destroy();
359359
}
360360
$this->graph->removeVertex($this);

tests/Edge/EdgeBaseTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ public function testLoop()
9999
$this->assertSame($this->v1, $edge->getVertexToFrom($this->v1));
100100
}
101101

102+
public function testRemoveWithLoop()
103+
{
104+
$edge = $this->createEdgeLoop();
105+
106+
$this->assertEquals(array($this->edge, $edge), $this->graph->getEdges()->getVector());
107+
108+
$edge->destroy();
109+
110+
$this->assertEquals(array($this->edge), $this->graph->getEdges()->getVector());
111+
$this->assertEquals(array($this->v1, $this->v2), $this->graph->getVertices()->getVector());
112+
}
113+
102114
protected function createAttributeAware()
103115
{
104116
return $this->createEdge();

tests/VertexTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,32 @@ public function testRemoveInvalidEdge()
136136
$this->vertex->removeEdge($edge);
137137
}
138138

139+
public function testRemoveWithEdgeLoopUndirected()
140+
{
141+
// 1 -- 1
142+
$edge = $this->vertex->createEdge($this->vertex);
143+
144+
$this->assertEquals(array(1 => $this->vertex), $this->graph->getVertices()->getMap());
145+
146+
$this->vertex->destroy();
147+
148+
$this->assertEquals(array(), $this->graph->getVertices()->getVector());
149+
$this->assertEquals(array(), $this->graph->getEdges()->getVector());
150+
}
151+
152+
public function testRemoveWithEdgeLoopDirected()
153+
{
154+
// 1 --> 1
155+
$edge = $this->vertex->createEdgeTo($this->vertex);
156+
157+
$this->assertEquals(array(1 => $this->vertex), $this->graph->getVertices()->getMap());
158+
159+
$this->vertex->destroy();
160+
161+
$this->assertEquals(array(), $this->graph->getVertices()->getVector());
162+
$this->assertEquals(array(), $this->graph->getEdges()->getVector());
163+
}
164+
139165
protected function createAttributeAware()
140166
{
141167
return new Vertex(new Graph(), 1);

0 commit comments

Comments
 (0)