Skip to content

Commit 3ad4b8b

Browse files
committed
Improve the way graphs are compared using images.
1 parent 7be956b commit 3ad4b8b

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

spec/drupol/phptree/Exporter/GraphSpec.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use drupol\phptree\Exporter\Graph;
88
use drupol\phptree\Node\ValueNode;
9-
use PhpSpec\ObjectBehavior;
9+
use spec\drupol\phptree\Node\NodeObjectBehavior;
1010

11-
class GraphSpec extends ObjectBehavior
11+
class GraphSpec extends NodeObjectBehavior
1212
{
1313
public function it_can_generate_a_graph()
1414
{
@@ -29,6 +29,10 @@ public function it_can_generate_a_graph()
2929
$this
3030
->export($child1)
3131
->shouldReturnAnInstanceOf(\Fhaculty\Graph\Graph::class);
32+
33+
$this
34+
->export($tree)
35+
->shouldHaveSameGraphImageFile($_SERVER['PWD'] . '/tests/fixtures/graphvizMvJSKP.png');
3236
}
3337

3438
public function it_is_initializable()

spec/drupol/phptree/Node/NodeObjectBehavior.php

+20
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace spec\drupol\phptree\Node;
66

7+
use drupol\phptree\Exporter\Graph;
78
use drupol\phptree\Exporter\Text;
89
use drupol\phptree\Node\NodeInterface;
10+
use Graphp\GraphViz\GraphViz;
911
use PhpSpec\ObjectBehavior;
1012

1113
/**
@@ -14,6 +16,8 @@
1416
* @method shouldHaveSameGraph(NodeInterface $node)
1517
* @method shouldHaveSameTextExport(string $export)
1618
* @method shouldNotHaveSameTextExport(string $export)
19+
* @method shouldHaveSameGraphImage(NodeInterface $node)
20+
* @method shouldHaveSameGraphImageFile(string $filepath)
1721
*/
1822
abstract class NodeObjectBehavior extends ObjectBehavior
1923
{
@@ -45,6 +49,22 @@ public function getMatchers(): array
4549

4650
return $left !== $key;
4751
},
52+
'haveSameGraphImage' => function ($subject, $key) {
53+
$exporter = new Graph();
54+
55+
$left = $exporter->export($subject);
56+
$right = $exporter->export($key);
57+
58+
$left = (new GraphViz())->setFormat('png')->createImageFile($left);
59+
$right = (new GraphViz())->setFormat('png')->createImageFile($right);
60+
61+
return \file_get_contents($left) === \file_get_contents($right);
62+
},
63+
'haveSameGraphImageFile' => function ($subject, $key) {
64+
$left = (new GraphViz())->setFormat('png')->createImageFile($subject);
65+
66+
return \file_get_contents($left) === \file_get_contents($key);
67+
},
4868
];
4969
}
5070
}

spec/drupol/phptree/Node/ValueNodeSpec.php

+13
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ public function it_can_be_set_with_a_value()
7878
->shouldReturn('root');
7979
}
8080

81+
public function it_can_draw_the_tree()
82+
{
83+
$this
84+
->beConstructedWith('root', 3);
85+
86+
foreach (\range(0, 30) as $number) {
87+
$this->add(new ValueNode($number, 2));
88+
}
89+
90+
$this
91+
->shouldHaveSameGraphImage($this->getWrappedObject());
92+
}
93+
8194
public function it_can_have_children()
8295
{
8396
$this

tests/fixtures/graphvizMvJSKP.png

17.5 KB
Loading

0 commit comments

Comments
 (0)