graphViz = new GraphViz(); } public function testGraphEmpty() { $graph = new Graph(); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphWithName() { $graph = new Graph(); $graph->setAttribute('graphviz.name', 'G'); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphWithNameWithSpaces() { $graph = new Graph(); $graph->setAttribute('graphviz.name', 'My Graph Name'); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphIsolatedVertices() { $graph = new Graph(); $graph->createVertex('a'); $graph->createVertex('b'); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphIsolatedVerticesWithGroupsWillBeAddedToClusters() { $graph = new Graph(); $graph->createVertex('a')->setGroup(0); $graph->createVertex('b')->setGroup(1)->setAttribute('graphviz.label', 'second'); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphDefaultAttributes() { $graph = new Graph(); $graph->setAttribute('graphviz.graph.bgcolor', 'transparent'); $graph->setAttribute('graphviz.node.color', 'blue'); $graph->setAttribute('graphviz.edge.color', 'grey'); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testUnknownGraphAttributesWillBeDiscarded() { $graph = new Graph(); $graph->setAttribute('graphviz.vertex.color', 'blue'); $graph->setAttribute('graphviz.unknown.color', 'red'); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testEscaping() { $graph = new Graph(); $graph->createVertex('a'); $graph->createVertex('b¹²³ is; ok\\ay, "right"?'); $graph->createVertex(3); $graph->createVertex(4)->setAttribute('graphviz.label', 'normal'); $graph->createVertex(5)->setAttribute('graphviz.label', GraphViz::raw('')); $expected = <<] } VIZ; $this->assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphWithSimpleEdgeUsesGraphWithSimpleEdgeDefinition() { // a -- b $graph = new Graph(); $graph->createVertex('a')->createEdge($graph->createVertex('b')); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphWithLoopUsesGraphWithSimpleLoopDefinition() { // a -- b -\ // | | // \--/ $graph = new Graph(); $graph->createVertex('a')->createEdge($graph->createVertex('b')); $graph->getVertex('b')->createEdge($graph->getVertex('b')); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphDirectedUsesDigraph() { $graph = new Graph(); $graph->createVertex('a')->createEdgeTo($graph->createVertex('b')); $expected = << "b" } VIZ; $this->assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphDirectedWithLoopUsesDigraphWithSimpleLoopDefinition() { // a -> b -\ // ^ | // \--/ $graph = new Graph(); $graph->createVertex('a')->createEdgeTo($graph->createVertex('b')); $graph->getVertex('b')->createEdgeTo($graph->getVertex('b')); $expected = << "b" "b" -> "b" } VIZ; $this->assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphMixedUsesDigraphWithExplicitDirectionNoneForUndirectedEdges() { // a -> b -- c $graph = new Graph(); $graph->createVertex('a')->createEdgeTo($graph->createVertex('b')); $graph->createVertex('c')->createEdge($graph->getVertex('b')); $expected = << "b" "c" -> "b" [dir="none"] } VIZ; $this->assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphMixedWithDirectedLoopUsesDigraphWithoutDirectionForDirectedLoop() { // a -- b -\ // ^ | // \--/ $graph = new Graph(); $graph->createVertex('a')->createEdge($graph->createVertex('b')); $graph->getVertex('b')->createEdgeTo($graph->getVertex('b')); $expected = << "b" [dir="none"] "b" -> "b" } VIZ; $this->assertEquals($expected, $this->graphViz->createScript($graph)); } public function testGraphUndirectedWithIsolatedVerticesFirst() { // a -- b -- c d $graph = new Graph(); $graph->createVertices(array('a', 'b', 'c', 'd')); $graph->getVertex('a')->createEdge($graph->getVertex('b')); $graph->getVertex('b')->createEdge($graph->getVertex('c')); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testVertexLabels() { $graph = new Graph(); $graph->createVertex('a')->setBalance(1); $graph->createVertex('b')->setBalance(0); $graph->createVertex('c')->setBalance(-1); $graph->createVertex('d')->setAttribute('graphviz.label', 'test'); $graph->createVertex('e')->setBalance(2)->setAttribute('graphviz.label', 'unnamed'); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testEdgeLayoutAtributes() { $graph = new Graph(); $graph->createVertex('1a')->createEdge($graph->createVertex('1b')); $graph->createVertex('2a')->createEdge($graph->createVertex('2b'))->setAttribute('graphviz.numeric', 20); $graph->createVertex('3a')->createEdge($graph->createVertex('3b'))->setAttribute('graphviz.textual', "forty"); $graph->createVertex('4a')->createEdge($graph->createVertex('4b'))->getAttributeBag()->setAttributes(array('graphviz.1' => 1, 'graphviz.2' => 2)); $graph->createVertex('5a')->createEdge($graph->createVertex('5b'))->getAttributeBag()->setAttributes(array('graphviz.a' => 'b', 'graphviz.c' => 'd')); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testEdgeLabels() { $graph = new Graph(); $graph->createVertex('1a')->createEdge($graph->createVertex('1b')); $graph->createVertex('2a')->createEdge($graph->createVertex('2b'))->setWeight(20); $graph->createVertex('3a')->createEdge($graph->createVertex('3b'))->setCapacity(30); $graph->createVertex('4a')->createEdge($graph->createVertex('4b'))->setFlow(40); $graph->createVertex('5a')->createEdge($graph->createVertex('5b'))->setFlow(50)->setCapacity(60); $graph->createVertex('6a')->createEdge($graph->createVertex('6b'))->setFlow(60)->setCapacity(70)->setWeight(80); $graph->createVertex('7a')->createEdge($graph->createVertex('7b'))->setFlow(70)->setAttribute('graphviz.label', 'prefixed'); $expected = <<assertEquals($expected, $this->graphViz->createScript($graph)); } public function testCreateImageSrcWillExportPngDefaultFormat() { $graph = new Graph(); $src = $this->graphViz->createImageSrc($graph); $this->assertStringStartsWith('data:image/png;base64,', $src); } public function testCreateImageSrcAsSvgWithUtf8DefaultCharset() { $graph = new Graph(); $this->graphViz->setFormat('svg'); $src = $this->graphViz->createImageSrc($graph); $this->assertStringStartsWith('data:image/svg+xml;charset=UTF-8;base64,', $src); } public function testCreateImageSrcAsSvgzWithExplicitIsoCharsetLatin1() { $graph = new Graph(); $graph->setAttribute('graphviz.graph.charset', 'iso-8859-1'); $this->graphViz->setFormat('svgz'); $src = $this->graphViz->createImageSrc($graph); $this->assertStringStartsWith('data:image/svg+xml;charset=iso-8859-1;base64,', $src); } }