GraphVizTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. use Fhaculty\Graph\Exception\RuntimeException;
  3. use Fhaculty\Graph\Exporter\Image;
  4. use Fhaculty\Graph\Vertex;
  5. use Fhaculty\Graph\Exception\OverflowException;
  6. use Fhaculty\Graph\Exception\InvalidArgumentException;
  7. use Fhaculty\Graph\Graph;
  8. use Graphp\GraphViz\GraphViz;
  9. class GraphVizTest extends TestCase
  10. {
  11. private $graphViz;
  12. public function setUp()
  13. {
  14. $this->graphViz = new GraphViz();
  15. }
  16. public function testGraphEmpty()
  17. {
  18. $graph = new Graph();
  19. $expected = <<<VIZ
  20. graph G {
  21. }
  22. VIZ;
  23. $this->assertEquals($expected, $this->graphViz->createScript($graph));
  24. }
  25. public function testGraphIsolatedVertices()
  26. {
  27. $graph = new Graph();
  28. $graph->createVertex('a');
  29. $graph->createVertex('b');
  30. $expected = <<<VIZ
  31. graph G {
  32. "a"
  33. "b"
  34. }
  35. VIZ;
  36. $this->assertEquals($expected, $this->graphViz->createScript($graph));
  37. }
  38. public function testEscaping()
  39. {
  40. $graph = new Graph();
  41. $graph->createVertex('a');
  42. $graph->createVertex('b¹²³ is; ok\\ay, "right"?');
  43. $graph->createVertex(3);
  44. $graph->createVertex(4)->setAttribute('graphviz.label', 'normal');
  45. $graph->createVertex(5)->setAttribute('graphviz.label', GraphViz::raw('<raw>'));
  46. $expected = <<<VIZ
  47. graph G {
  48. "a"
  49. "b¹²³ is; ok\\\\ay, &quot;right&quot;?"
  50. 3
  51. 4 [label="normal"]
  52. 5 [label=<raw>]
  53. }
  54. VIZ;
  55. $this->assertEquals($expected, $this->graphViz->createScript($graph));
  56. }
  57. public function testGraphDirected()
  58. {
  59. $graph = new Graph();
  60. $graph->createVertex('a')->createEdgeTo($graph->createVertex('b'));
  61. $expected = <<<VIZ
  62. digraph G {
  63. "a" -> "b"
  64. }
  65. VIZ;
  66. $this->assertEquals($expected, $this->graphViz->createScript($graph));
  67. }
  68. public function testGraphMixed()
  69. {
  70. // a -> b -- c
  71. $graph = new Graph();
  72. $graph->createVertex('a')->createEdgeTo($graph->createVertex('b'));
  73. $graph->createVertex('c')->createEdge($graph->getVertex('b'));
  74. $expected = <<<VIZ
  75. digraph G {
  76. "a" -> "b"
  77. "c" -> "b" [dir="none"]
  78. }
  79. VIZ;
  80. $this->assertEquals($expected, $this->graphViz->createScript($graph));
  81. }
  82. public function testGraphUndirectedWithIsolatedVerticesFirst()
  83. {
  84. // a -- b -- c d
  85. $graph = new Graph();
  86. $graph->createVertices(array('a', 'b', 'c', 'd'));
  87. $graph->getVertex('a')->createEdge($graph->getVertex('b'));
  88. $graph->getVertex('b')->createEdge($graph->getVertex('c'));
  89. $expected = <<<VIZ
  90. graph G {
  91. "d"
  92. "a" -- "b"
  93. "b" -- "c"
  94. }
  95. VIZ;
  96. $this->assertEquals($expected, $this->graphViz->createScript($graph));
  97. }
  98. public function testVertexLabels()
  99. {
  100. $graph = new Graph();
  101. $graph->createVertex('a')->setBalance(1);
  102. $graph->createVertex('b')->setBalance(0);
  103. $graph->createVertex('c')->setBalance(-1);
  104. $graph->createVertex('d')->setAttribute('graphviz.label', 'test');
  105. $graph->createVertex('e')->setBalance(2)->setAttribute('graphviz.label', 'unnamed');
  106. $expected = <<<VIZ
  107. graph G {
  108. "a" [label="a (+1)"]
  109. "b" [label="b (0)"]
  110. "c" [label="c (-1)"]
  111. "d" [label="test"]
  112. "e" [label="unnamed (+2)"]
  113. }
  114. VIZ;
  115. $this->assertEquals($expected, $this->graphViz->createScript($graph));
  116. }
  117. public function testEdgeLayoutAtributes()
  118. {
  119. $graph = new Graph();
  120. $graph->createVertex('1a')->createEdge($graph->createVertex('1b'));
  121. $graph->createVertex('2a')->createEdge($graph->createVertex('2b'))->setAttribute('graphviz.numeric', 20);
  122. $graph->createVertex('3a')->createEdge($graph->createVertex('3b'))->setAttribute('graphviz.textual', "forty");
  123. $graph->createVertex('4a')->createEdge($graph->createVertex('4b'))->getAttributeBag()->setAttributes(array('graphviz.1' => 1, 'graphviz.2' => 2));
  124. $graph->createVertex('5a')->createEdge($graph->createVertex('5b'))->getAttributeBag()->setAttributes(array('graphviz.a' => 'b', 'graphviz.c' => 'd'));
  125. $expected = <<<VIZ
  126. graph G {
  127. "1a" -- "1b"
  128. "2a" -- "2b" [numeric=20]
  129. "3a" -- "3b" [textual="forty"]
  130. "4a" -- "4b" [1=1 2=2]
  131. "5a" -- "5b" [a="b" c="d"]
  132. }
  133. VIZ;
  134. $this->assertEquals($expected, $this->graphViz->createScript($graph));
  135. }
  136. public function testEdgeLabels()
  137. {
  138. $graph = new Graph();
  139. $graph->createVertex('1a')->createEdge($graph->createVertex('1b'));
  140. $graph->createVertex('2a')->createEdge($graph->createVertex('2b'))->setWeight(20);
  141. $graph->createVertex('3a')->createEdge($graph->createVertex('3b'))->setCapacity(30);
  142. $graph->createVertex('4a')->createEdge($graph->createVertex('4b'))->setFlow(40);
  143. $graph->createVertex('5a')->createEdge($graph->createVertex('5b'))->setFlow(50)->setCapacity(60);
  144. $graph->createVertex('6a')->createEdge($graph->createVertex('6b'))->setFlow(60)->setCapacity(70)->setWeight(80);
  145. $graph->createVertex('7a')->createEdge($graph->createVertex('7b'))->setFlow(70)->setAttribute('graphviz.label', 'prefixed');
  146. $expected = <<<VIZ
  147. graph G {
  148. "1a" -- "1b"
  149. "2a" -- "2b" [label=20]
  150. "3a" -- "3b" [label="0/30"]
  151. "4a" -- "4b" [label="40/∞"]
  152. "5a" -- "5b" [label="50/60"]
  153. "6a" -- "6b" [label="60/70/80"]
  154. "7a" -- "7b" [label="prefixed 70/∞"]
  155. }
  156. VIZ;
  157. $this->assertEquals($expected, $this->graphViz->createScript($graph));
  158. }
  159. }