|
| 1 | +<?php |
| 2 | + |
| 3 | +use Graphp\GraphML\Loader; |
| 4 | + |
| 5 | +class LoaderTest extends TestCase |
| 6 | +{ |
| 7 | + private $loader; |
| 8 | + |
| 9 | + public function setUp() |
| 10 | + { |
| 11 | + $this->loader = new Loader(); |
| 12 | + } |
| 13 | + |
| 14 | + public function testEmpty() |
| 15 | + { |
| 16 | + $data = <<<EOL |
| 17 | +<?xml version="1.0" encoding="UTF-8"?> |
| 18 | +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" |
| 19 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 20 | + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns |
| 21 | + http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> |
| 22 | + <graph id="G" edgedefault="undirected"> |
| 23 | + </graph> |
| 24 | +</graphml> |
| 25 | +EOL; |
| 26 | + |
| 27 | + $graph = $this->loader->loadContents($data); |
| 28 | + |
| 29 | + $this->assertCount(0, $graph->getVertices()); |
| 30 | + } |
| 31 | + |
| 32 | + public function testSimpleGraph() |
| 33 | + { |
| 34 | + $data = <<<EOL |
| 35 | +<?xml version="1.0" encoding="UTF-8"?> |
| 36 | +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" |
| 37 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 38 | + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns |
| 39 | + http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> |
| 40 | + <graph id="G" edgedefault="undirected"> |
| 41 | + <node id="n0"/> |
| 42 | + <node id="n1"/> |
| 43 | + <node id="n2"/> |
| 44 | + <node id="n3"/> |
| 45 | + <node id="n4"/> |
| 46 | + <node id="n5"/> |
| 47 | + <node id="n6"/> |
| 48 | + <node id="n7"/> |
| 49 | + <node id="n8"/> |
| 50 | + <node id="n9"/> |
| 51 | + <node id="n10"/> |
| 52 | + <edge source="n0" target="n2"/> |
| 53 | + <edge source="n1" target="n2"/> |
| 54 | + <edge source="n2" target="n3"/> |
| 55 | + <edge source="n3" target="n5"/> |
| 56 | + <edge source="n3" target="n4"/> |
| 57 | + <edge source="n4" target="n6"/> |
| 58 | + <edge source="n6" target="n5"/> |
| 59 | + <edge source="n5" target="n7"/> |
| 60 | + <edge source="n6" target="n8"/> |
| 61 | + <edge source="n8" target="n7"/> |
| 62 | + <edge source="n8" target="n9"/> |
| 63 | + <edge source="n8" target="n10"/> |
| 64 | + </graph> |
| 65 | +</graphml> |
| 66 | +EOL; |
| 67 | + |
| 68 | + $graph = $this->loader->loadContents($data); |
| 69 | + |
| 70 | + $this->assertCount(11, $graph->getVertices()); |
| 71 | + $this->assertCount(12, $graph->getEdges()); |
| 72 | + } |
| 73 | + |
| 74 | + public function testEdgeUndirected() |
| 75 | + { |
| 76 | + $data = <<<EOL |
| 77 | +<?xml version="1.0" encoding="UTF-8"?> |
| 78 | +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" |
| 79 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 80 | + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns |
| 81 | + http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> |
| 82 | + <graph id="G" edgedefault="undirected"> |
| 83 | + <node id="n0"/> |
| 84 | + <edge source="n0" target="n0" directed="false"/> |
| 85 | + <edge source="n0" target="n0"/> |
| 86 | + </graph> |
| 87 | +</graphml> |
| 88 | +EOL; |
| 89 | + |
| 90 | + $graph = $this->loader->loadContents($data); |
| 91 | + |
| 92 | + $this->assertCount(2, $graph->getEdges()); |
| 93 | + $this->assertInstanceOf('Fhaculty\Graph\Edge\Undirected', $graph->getEdges()->getEdgeFirst()); |
| 94 | + $this->assertInstanceOf('Fhaculty\Graph\Edge\Undirected', $graph->getEdges()->getEdgeLast()); |
| 95 | + } |
| 96 | + |
| 97 | + public function testEdgeDirected() |
| 98 | + { |
| 99 | + $data = <<<EOL |
| 100 | +<?xml version="1.0" encoding="UTF-8"?> |
| 101 | +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" |
| 102 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 103 | + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns |
| 104 | + http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> |
| 105 | + <graph id="G" edgedefault="directed"> |
| 106 | + <node id="n0"/> |
| 107 | + <edge source="n0" target="n0" directed="true"/> |
| 108 | + <edge source="n0" target="n0"/> |
| 109 | + </graph> |
| 110 | +</graphml> |
| 111 | +EOL; |
| 112 | + |
| 113 | + $graph = $this->loader->loadContents($data); |
| 114 | + |
| 115 | + $this->assertCount(2, $graph->getEdges()); |
| 116 | + $this->assertInstanceOf('Fhaculty\Graph\Edge\Directed', $graph->getEdges()->getEdgeFirst()); |
| 117 | + $this->assertInstanceOf('Fhaculty\Graph\Edge\Directed', $graph->getEdges()->getEdgeLast()); |
| 118 | + } |
| 119 | + |
| 120 | + public function testAttributeTypes() |
| 121 | + { |
| 122 | + $data = <<<EOL |
| 123 | +<?xml version="1.0" encoding="UTF-8"?> |
| 124 | +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" |
| 125 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 126 | + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns |
| 127 | + http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> |
| 128 | + <key id="d0" for="node" attr.name="string" attr.type="string"></key> |
| 129 | + <key id="d1" for="node" attr.name="boolean" attr.type="boolean"></key> |
| 130 | + <key id="d2" for="node" attr.name="float" attr.type="float"></key> |
| 131 | + <key id="d3" for="node" attr.name="int" attr.type="int"></key> |
| 132 | + <graph id="G" edgedefault="undirected"> |
| 133 | + <node id="n0"> |
| 134 | + <data key="d0">text</data> |
| 135 | + <data key="d1">true</data> |
| 136 | + <data key="d2">4.5</data> |
| 137 | + <data key="d3">3</data> |
| 138 | + </node> |
| 139 | + </graph> |
| 140 | +</graphml> |
| 141 | +EOL; |
| 142 | + |
| 143 | + $graph = $this->loader->loadContents($data); |
| 144 | + |
| 145 | + $vertex = $graph->getVertices()->getVertexFirst(); |
| 146 | + |
| 147 | + $this->assertEquals('n0', $vertex->getId()); |
| 148 | + $this->assertSame('text', $vertex->getAttribute('string')); |
| 149 | + $this->assertSame(true, $vertex->getAttribute('boolean')); |
| 150 | + $this->assertSame(4.5, $vertex->getAttribute('float')); |
| 151 | + $this->assertSame(3, $vertex->getAttribute('int')); |
| 152 | + } |
| 153 | + |
| 154 | + public function testAttributeDefault() |
| 155 | + { |
| 156 | + $data = <<<EOL |
| 157 | +<?xml version="1.0" encoding="UTF-8"?> |
| 158 | +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" |
| 159 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 160 | + xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns |
| 161 | + http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> |
| 162 | + <key id="d0" for="node" attr.name="color" attr.type="string"> |
| 163 | + <default>yellow</default> |
| 164 | + </key> |
| 165 | + <graph id="G" edgedefault="undirected"> |
| 166 | + <node id="n0"/> |
| 167 | + </graph> |
| 168 | +</graphml> |
| 169 | +EOL; |
| 170 | + |
| 171 | + $graph = $this->loader->loadContents($data); |
| 172 | + |
| 173 | + $vertex = $graph->getVertices()->getVertexFirst(); |
| 174 | + |
| 175 | + $this->assertEquals('n0', $vertex->getId()); |
| 176 | + $this->assertEquals('yellow', $vertex->getAttribute('color')); |
| 177 | + } |
| 178 | +} |
0 commit comments