bootstrap.php 3.5 KB

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