|
| 1 | +<?php |
| 2 | + |
| 3 | +use Fhaculty\Graph\Edge\Directed; |
| 4 | +use Fhaculty\Graph\Edge\Base as Edge; |
| 5 | +use Fhaculty\Graph\Graph; |
| 6 | +use Fhaculty\Graph\Vertex; |
| 7 | +use Fhaculty\Graph\Set\Vertices; |
| 8 | + |
| 9 | +(include_once __DIR__ . '/../vendor/autoload.php') OR die(PHP_EOL . 'ERROR: composer autoloader not found, run "composer install" or see README for instructions' . PHP_EOL); |
| 10 | + |
| 11 | +class TestCase extends PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + protected function assertGraphEquals(Graph $expected, Graph $actual) |
| 14 | + { |
| 15 | + $f = function(Graph $graph){ |
| 16 | + $ret = get_class($graph); |
| 17 | + $ret .= PHP_EOL . 'vertices: ' . count($graph->getVertices()); |
| 18 | + $ret .= PHP_EOL . 'edges: ' . count($graph->getEdges()); |
| 19 | + |
| 20 | + return $ret; |
| 21 | + }; |
| 22 | + |
| 23 | + // assert graph base parameters are equal |
| 24 | + $this->assertEquals($f($expected), $f($actual)); |
| 25 | + |
| 26 | + // next, assert that all vertices in both graphs are the same |
| 27 | + // each vertex has a unique ID, therefor it's easy to search a matching partner |
| 28 | + // do not use assertVertexEquals() in order to not increase assertion counter |
| 29 | + |
| 30 | + foreach ($expected->getVertices()->getMap() as $vid => $vertex) { |
| 31 | + try { |
| 32 | + $other = $actual->getVertex($vid); |
| 33 | + } catch (Exception $e) { |
| 34 | + $this->fail(); |
| 35 | + } |
| 36 | + if ($this->getVertexDump($vertex) !== $this->getVertexDump($vertex)) { |
| 37 | + $this->fail(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + // next, assert that all edges in both graphs are the same |
| 42 | + // assertEdgeEquals() does not work, as the order of the edges is unknown |
| 43 | + // therefor, build an array of edge dump and make sure each entry has a match |
| 44 | + |
| 45 | + $edgesExpected = array(); |
| 46 | + foreach ($expected->getEdges() as $edge) { |
| 47 | + $edgesExpected []= $this->getEdgeDump($edge); |
| 48 | + } |
| 49 | + |
| 50 | + foreach ($actual->getEdges() as $edge) { |
| 51 | + $dump = $this->getEdgeDump($edge); |
| 52 | + |
| 53 | + $pos = array_search($dump, $edgesExpected, true); |
| 54 | + if ($pos === false) { |
| 55 | + $this->fail('given edge ' . $dump . ' not found'); |
| 56 | + } else { |
| 57 | + unset($edgesExpected[$pos]); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + protected function assertVertexEquals(Vertex $expected, Vertex $actual) |
| 63 | + { |
| 64 | + $this->assertEquals($this->getVertexDump($expected), $this->getVertexDump($actual)); |
| 65 | + } |
| 66 | + |
| 67 | + protected function assertEdgeEquals(Edge $expected, Edge $actual) |
| 68 | + { |
| 69 | + $this->assertEquals($this->getEdgeDump($expected), $this->getEdgeDump($actual)); |
| 70 | + } |
| 71 | + |
| 72 | + private function getVertexDump(Vertex $vertex) |
| 73 | + { |
| 74 | + $ret = get_class($vertex); |
| 75 | + |
| 76 | + $ret .= PHP_EOL . 'id: ' . $vertex->getId(); |
| 77 | + $ret .= PHP_EOL . 'attributes: ' . json_encode($vertex->getAttributeBag()->getAttributes()); |
| 78 | + $ret .= PHP_EOL . 'balance: ' . $vertex->getBalance(); |
| 79 | + $ret .= PHP_EOL . 'group: ' . $vertex->getGroup(); |
| 80 | + |
| 81 | + return $ret; |
| 82 | + } |
| 83 | + |
| 84 | + private function getEdgeDump(Edge $edge) |
| 85 | + { |
| 86 | + $ret = get_class($edge) . ' '; |
| 87 | + if ($edge instanceof Directed) { |
| 88 | + $ret .= $edge->getVertexStart()->getId() . ' -> ' . $edge->getVertexEnd()->getId(); |
| 89 | + } else { |
| 90 | + $vertices = $edge->getVertices()->getIds(); |
| 91 | + $ret .= $vertices[0] . ' -- ' . $vertices[1]; |
| 92 | + } |
| 93 | + $ret .= PHP_EOL . 'flow: ' . $edge->getFlow(); |
| 94 | + $ret .= PHP_EOL . 'capacity: ' . $edge->getCapacity(); |
| 95 | + $ret .= PHP_EOL . 'weight: ' . $edge->getWeight(); |
| 96 | + $ret .= PHP_EOL . 'attributes: ' . json_encode($edge->getAttributeBag()->getAttributes()); |
| 97 | + |
| 98 | + return $ret; |
| 99 | + } |
| 100 | +} |
0 commit comments