Skip to content

Commit

Permalink
Work around broken mocking with PHP 7.4 on legacy PHPUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Mar 25, 2020
1 parent 1b9d8d1 commit e3d6321
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions tests/GraphComposerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
<?php

use Clue\GraphComposer\Graph\GraphComposer;
use Graphp\GraphViz\GraphViz;
use Fhaculty\Graph\Graph;

class GraphVizMockDisplay extends GraphViz
{
public $called = 0;
public function display(Graph $graph)
{
++$this->called;
}
}

class GraphVizMockCreateImageFile extends GraphViz
{
public $called = 0;
public function createImageFile(Graph $graph)
{
return 'test' . ++$this->called . '.png';
}
}

class GraphVizMockSetFormat extends GraphViz
{
public $called = null;
public function setFormat($format)
{
$this->called = $format;
}
}

class GraphTest extends PHPUnit_Framework_TestCase
{
Expand All @@ -15,40 +44,43 @@ public function testCreateGraph()
$this->assertTrue(count($graph->getVertices()) > 0);
}

public function testWillDisplayGraph()
public function testDisplayGraphCallsDisplayGraphViz()
{
$dir = __DIR__ . '/../';

$graphviz = $this->getMock('Graphp\GraphViz\GraphViz');
$graphviz->expects($this->once())->method('display');
// mocking with PHP 7.4 reports error with legacy PHPUnit, create manual mock classes instead
$graphviz = new GraphVizMockDisplay();

$graphComposer = new GraphComposer($dir, $graphviz);
$graphComposer->displayGraph();

$this->assertEquals(1, $graphviz->called);
}

public function testWillWriteTemporaryGraph()
public function testGetImagePathWillCreateTemporaryImageFileViaGraphViz()
{
$dir = __DIR__ . '/../';

$graphviz = $this->getMock('Graphp\GraphViz\GraphViz');
$graphviz->expects($this->once())->method('createImageFile')->will($this->returnValue('test.png'));
// mocking with PHP 7.4 reports error with legacy PHPUnit, create manual mock classes instead
$graphviz = new GraphVizMockCreateImageFile();

$graphComposer = new GraphComposer($dir, $graphviz);
$ret = $graphComposer->getImagePath();

$this->assertEquals('test.png', $ret);
$this->assertEquals('test1.png', $ret);
}

public function testWillSetFormat()
public function testSetFormatWillSetFormatOnGraphViz()
{
$dir = __DIR__ . '/../';

$graphviz = $this->getMock('Graphp\GraphViz\GraphViz');
$graphviz->expects($this->once())->method('setFormat')->with($this->equalTo('gif'));
// mocking with PHP 7.4 reports error with legacy PHPUnit, create manual mock classes instead
$graphviz = new GraphVizMockSetFormat();

$graphComposer = new GraphComposer($dir, $graphviz);
$ret = $graphComposer->setFormat('gif');

$this->assertEquals($graphComposer, $ret);
$this->assertEquals('gif', $graphviz->called);
}
}

0 comments on commit e3d6321

Please sign in to comment.