|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* Grafizzi\Graph\Tests\RendererTest: a component of the Grafizzi library. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2012 Frédéric G. MARAND <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* Grafizzi is free software: you can redistribute it and/or modify it under the |
|
10
|
|
|
* terms of the GNU Lesser General Public License as published by the Free |
|
11
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
|
12
|
|
|
* later version. |
|
13
|
|
|
* |
|
14
|
|
|
* Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY |
|
15
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|
16
|
|
|
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
17
|
|
|
* details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
20
|
|
|
* along with Grafizzi, in the COPYING.LESSER.txt file. If not, see |
|
21
|
|
|
* <http://www.gnu.org/licenses/> |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace Grafizzi\Graph\Tests; |
|
25
|
|
|
|
|
26
|
|
|
require 'vendor/autoload.php'; |
|
27
|
|
|
|
|
28
|
|
|
use Grafizzi\Graph\Renderer; |
|
29
|
|
|
use \ErrorException; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Renderer test case. |
|
33
|
|
|
* |
|
34
|
|
|
* Note: at least in PHP3.7, when passing, expectOutputString is not |
|
35
|
|
|
* included in the assertions count, but it is included when failing. |
|
36
|
|
|
*/ |
|
37
|
|
|
class RendererTest extends BaseGraphTest { |
|
38
|
|
|
|
|
39
|
|
|
const ERASABLE = 'input should be overwritten'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* @var Renderer |
|
44
|
|
|
*/ |
|
45
|
|
|
private $renderer; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Prepares the environment before running a test. |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function setUp() : void { |
|
51
|
|
|
parent::setUpExtended(); |
|
|
|
|
|
|
52
|
|
|
$this->renderer = new Renderer($this->dic); |
|
53
|
|
|
$this->renderer->pipe = $this->Graph->build(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Cleans up the environment after running a test. |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function tearDown() : void { |
|
60
|
|
|
$this->renderer = null; |
|
61
|
|
|
parent::tearDown(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Tests Renderer::getFormats() |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testGetFormats() { |
|
68
|
|
|
$dic = $this->dic; |
|
69
|
|
|
$dic['use_exceptions'] = false; |
|
70
|
|
|
$formats = Renderer::getFormats($dic); |
|
71
|
|
|
$this->assertTrue(is_array($formats), 'Renderer::getFormats() returns an array when exceptions are not used.'); |
|
72
|
|
|
|
|
73
|
|
|
$dic['use_exceptions'] = true; |
|
74
|
|
|
try { |
|
75
|
|
|
$formats = Renderer::getFormats($dic); |
|
76
|
|
|
$this->assertTrue(is_array($formats) && !empty($formats), 'Renderer::getFormats() returns a non-empty array when exceptions are used.'); |
|
77
|
|
|
} |
|
78
|
|
|
catch (ErrorException $e) { |
|
|
|
|
|
|
79
|
|
|
$this->fail('Renderer::getFormats() could not find dot and threw an ErrorException.'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Test use of magic __call() with non existent filter. |
|
85
|
|
|
* |
|
86
|
|
|
* - must throw DomainException |
|
87
|
|
|
* - must not overwrite source data |
|
88
|
|
|
*/ |
|
89
|
|
|
public function test__call() { |
|
90
|
|
|
$expected = $out = 'Some data'; |
|
91
|
|
|
try { |
|
92
|
|
|
$out = call_user_func(array($this->renderer, 'nonexistent')); |
|
93
|
|
|
} |
|
94
|
|
|
catch (\DomainException $e) { |
|
95
|
|
|
$this->assertInstanceOf('\\DomainException', $e, 'Rendering non existent filter throws DomainException'); |
|
96
|
|
|
} |
|
97
|
|
|
$this->assertEquals($expected, $out, 'Output is not overwritten by non existent filter.'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Tests rendering with a single filter. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testRenderOneFilter() { |
|
104
|
|
|
// Callback can be a closure. |
|
105
|
|
|
$callback = function ($x) { |
|
106
|
|
|
return strrev($x); |
|
107
|
|
|
}; |
|
108
|
|
|
|
|
109
|
|
|
// The value of this string should be overwritten by the filter. |
|
110
|
|
|
$output = self::ERASABLE; |
|
111
|
|
|
|
|
112
|
|
|
$expected = $callback($this->renderer->pipe); |
|
113
|
|
|
|
|
114
|
|
|
// Invoke filter via renderer, overwriting output and pipe. |
|
115
|
|
|
call_user_func(array($this->renderer, 'string'), array( |
|
116
|
|
|
'out' => &$output, |
|
117
|
|
|
'callback' => $callback, |
|
118
|
|
|
)); |
|
119
|
|
|
|
|
120
|
|
|
$pipe = $this->renderer->pipe; |
|
121
|
|
|
$this->assertIsString($pipe, "String filter returns string output"); |
|
122
|
|
|
$this->assertEquals($expected, $this->renderer->pipe, "Filter with closure works on renderer pipe."); |
|
123
|
|
|
|
|
124
|
|
|
// No error output. |
|
125
|
|
|
$this->expectOutputString(""); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Test the fluent filter interface. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function testRenderChainedFilters() { |
|
132
|
|
|
$callback = 'strrev'; |
|
133
|
|
|
|
|
134
|
|
|
// The value of this string should be overwritten by the filter. |
|
135
|
|
|
$output1 = $output2 = $output3 = self::ERASABLE; |
|
136
|
|
|
|
|
137
|
|
|
$input = $this->renderer->pipe; |
|
138
|
|
|
$step1 = call_user_func(array($this->renderer, 'string'), array( |
|
139
|
|
|
'out' => &$output1, |
|
140
|
|
|
'callback' => $callback, |
|
141
|
|
|
)); |
|
142
|
|
|
|
|
143
|
|
|
$r = call_user_func(array($step1, 'string'), array( |
|
144
|
|
|
'out' => &$output2, |
|
145
|
|
|
'callback' => $callback, |
|
146
|
|
|
)); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertEquals($input, $this->renderer->pipe, "Chained filters with strrev work."); |
|
149
|
|
|
|
|
150
|
|
|
// Test a filter chain: string, then sink. |
|
151
|
|
|
$pipe_input = $this->renderer->pipe; |
|
152
|
|
|
|
|
153
|
|
|
$r->string(array( |
|
154
|
|
|
'out' => &$output3, |
|
155
|
|
|
'callback' => $callback, |
|
156
|
|
|
))->sink(); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertEquals($callback($pipe_input), $output3, "String filter updates argument."); |
|
159
|
|
|
$pipe = $this->renderer->pipe; |
|
160
|
|
|
$this->assertEmpty($pipe, 'Sink filter drops content from stdout.'); |
|
161
|
|
|
|
|
162
|
|
|
// No error output from either filter. |
|
163
|
|
|
$this->expectOutputString(""); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.