1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2017 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace phpDocumentor\Transformer\Router; |
14
|
|
|
|
15
|
|
|
use Mockery as m; |
16
|
|
|
use phpDocumentor\Descriptor\Collection; |
17
|
|
|
use phpDocumentor\Descriptor\Type\CollectionDescriptor; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Test class for phpDocumentor\Transformer\Router\Renderer |
21
|
|
|
* |
22
|
|
|
* @coversDefaultClass \phpDocumentor\Transformer\Router\Renderer |
23
|
|
|
* @covers ::<protected> |
24
|
|
|
*/ |
25
|
|
|
class RendererTest extends \PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Queue |
29
|
|
|
*/ |
30
|
|
|
private $originalQueue; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Renderer |
34
|
|
|
*/ |
35
|
|
|
private $renderer; |
36
|
|
|
|
37
|
|
|
public function setUp() |
38
|
|
|
{ |
39
|
|
|
$this->originalQueue = m::mock('phpDocumentor\Transformer\Router\Queue'); |
40
|
|
|
$this->renderer = new Renderer($this->originalQueue); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @covers ::__construct |
46
|
|
|
* @covers ::getRouters |
47
|
|
|
*/ |
48
|
|
|
public function testConstructRenderer() |
49
|
|
|
{ |
50
|
|
|
$result = $this->renderer->getRouters(); |
51
|
|
|
|
52
|
|
|
$this->assertSame($this->originalQueue, $result); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @covers ::__construct |
57
|
|
|
* @covers ::getRouters |
58
|
|
|
* @covers ::setRouters |
59
|
|
|
*/ |
60
|
|
|
public function testGetAndSetRouters() |
61
|
|
|
{ |
62
|
|
|
$rule = $this->givenARule('http://phpdoc.org'); |
63
|
|
|
$newQueue = [$this->givenAQueue($rule)]; |
64
|
|
|
$this->renderer->setRouters($newQueue); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$result = $this->renderer->getRouters(); |
67
|
|
|
|
68
|
|
|
$this->assertNotSame($this->originalQueue, $result); |
69
|
|
|
$this->assertSame($newQueue, $result); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @covers \phpDocumentor\Transformer\Router\Renderer::__construct |
74
|
|
|
* @covers \phpDocumentor\Transformer\Router\Renderer::getDestination |
75
|
|
|
* @covers \phpDocumentor\Transformer\Router\Renderer::setDestination |
76
|
|
|
*/ |
77
|
|
|
public function testGetAndSetDestination() |
78
|
|
|
{ |
79
|
|
|
$this->renderer->setDestination('destination'); |
80
|
|
|
|
81
|
|
|
$this->assertSame('destination', $this->renderer->getDestination()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @covers ::render |
86
|
|
|
* @covers ::convertToRootPath |
87
|
|
|
*/ |
88
|
|
|
public function testRenderWithFqsenAndRepresentationUrl() |
89
|
|
|
{ |
90
|
|
|
$rule = $this->givenARule('/classes/My.Namespace.Class.html'); |
91
|
|
|
$queue = $this->givenAQueue($rule); |
92
|
|
|
$queue->shouldReceive('match')->andReturn($rule); |
93
|
|
|
$this->renderer->setRouters($queue); |
94
|
|
|
$result = $this->renderer->render('\My\Namespace\Class', 'url'); |
95
|
|
|
|
96
|
|
|
$this->assertSame('classes/My.Namespace.Class.html', $result); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @covers ::render |
101
|
|
|
* @covers ::convertToRootPath |
102
|
|
|
*/ |
103
|
|
|
public function testRenderWithCollectionOfFqsensAndRepresentationUrl() |
104
|
|
|
{ |
105
|
|
|
$rule = $this->givenARule('/classes/My.Namespace.Class.html'); |
106
|
|
|
$queue = $this->givenAQueue($rule); |
107
|
|
|
$queue->shouldReceive('match')->andReturn($rule); |
108
|
|
|
$this->renderer->setRouters($queue); |
109
|
|
|
$this->renderer->setDestination(str_replace('/', DIRECTORY_SEPARATOR,'/root/of/project')); |
110
|
|
|
$collection = new Collection(['\My\Namespace\Class']); |
111
|
|
|
$result = $this->renderer->render($collection, 'url'); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$this->assertSame(['../../../classes/My.Namespace.Class.html'], $result); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @covers ::render |
118
|
|
|
* @covers ::convertToRootPath |
119
|
|
|
*/ |
120
|
|
|
public function testRenderWithUrlAndNoRuleMatch() |
121
|
|
|
{ |
122
|
|
|
$rule = $this->givenARule('@'); |
123
|
|
|
$queue = $this->givenAQueue($rule); |
124
|
|
|
$queue->shouldReceive('match')->with('file://phpdoc')->andReturn($rule); |
125
|
|
|
$queue->shouldReceive('match')->with('@')->andReturn(null); |
126
|
|
|
$this->renderer->setRouters($queue); |
127
|
|
|
$result = $this->renderer->render('file://phpdoc', 'url'); |
128
|
|
|
|
129
|
|
|
$this->assertSame(null, $result); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @covers ::convertToRootPath |
134
|
|
|
*/ |
135
|
|
|
public function testConvertToRootPathWithUrlAndAtSignInRelativePath() |
136
|
|
|
{ |
137
|
|
|
$rule = $this->givenARule('@Class::$property'); |
138
|
|
|
$queue = $this->givenAQueue($rule); |
139
|
|
|
$queue->shouldReceive('match')->with('@Class::$property')->andReturn($rule); |
140
|
|
|
$this->renderer->setRouters($queue); |
141
|
|
|
$result = $this->renderer->convertToRootPath('@Class::$property'); |
142
|
|
|
|
143
|
|
|
$this->assertSame('@Class::$property', $result); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @covers ::render |
148
|
|
|
* @covers ::convertToRootPath |
149
|
|
|
*/ |
150
|
|
|
public function testRenderWithCollectionDescriptorWithNameIsNotArrayAndRepresentationUrl() |
151
|
|
|
{ |
152
|
|
|
$rule = $this->givenARule('ClassDescriptor'); |
153
|
|
|
$queue = $this->givenAQueue($rule); |
154
|
|
|
$queue->shouldReceive('match')->andReturn($rule); |
155
|
|
|
$this->renderer->setRouters($queue); |
156
|
|
|
$collectionDescriptor = $this->givenACollectionDescriptor('class'); |
157
|
|
|
$collectionDescriptor->setKeyTypes(['ClassDescriptor']); |
|
|
|
|
158
|
|
|
$result = $this->renderer->render($collectionDescriptor, 'url'); |
159
|
|
|
|
160
|
|
|
$this->assertSame('ClassDescriptor<ClassDescriptor,ClassDescriptor>', $result); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @covers ::render |
165
|
|
|
* @covers ::convertToRootPath |
166
|
|
|
*/ |
167
|
|
|
public function testRenderWithCollectionDescriptorWithNameIsArrayAndRepresentationUrl() |
168
|
|
|
{ |
169
|
|
|
$rule = $this->givenARule('ClassDescriptor'); |
170
|
|
|
$queue = $this->givenAQueue($rule); |
171
|
|
|
$queue->shouldReceive('match')->andReturn($rule); |
172
|
|
|
$this->renderer->setRouters($queue); |
173
|
|
|
$collectionDescriptor = $this->givenACollectionDescriptor('array'); |
174
|
|
|
$result = $this->renderer->render($collectionDescriptor, 'url'); |
175
|
|
|
|
176
|
|
|
$this->assertSame('ClassDescriptor[]', $result); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @covers ::render |
181
|
|
|
*/ |
182
|
|
|
public function testRenderWithFqsenAndRepresentationClassShort() |
183
|
|
|
{ |
184
|
|
|
$rule = $this->givenARule('/classes/My.Namespace.Class.html'); |
185
|
|
|
$queue = $this->givenAQueue($rule); |
186
|
|
|
$queue->shouldReceive('match')->andReturn($rule); |
187
|
|
|
$this->renderer->setRouters($queue); |
188
|
|
|
$result = $this->renderer->render('\My\Namespace\Class', 'class:short'); |
189
|
|
|
|
190
|
|
|
$this->assertSame('<a href="classes/My.Namespace.Class.html">Class</a>', $result); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @covers ::render |
195
|
|
|
* @dataProvider provideUrls |
196
|
|
|
*/ |
197
|
|
|
public function testRenderWithUrl($url) |
198
|
|
|
{ |
199
|
|
|
$rule = $this->givenARule($url); |
200
|
|
|
$queue = $this->givenAQueue($rule); |
201
|
|
|
$queue->shouldReceive('match')->andReturn($rule); |
202
|
|
|
$this->renderer->setRouters($queue); |
203
|
|
|
$result = $this->renderer->render($url, 'url'); |
204
|
|
|
|
205
|
|
|
$this->assertSame($url, $result); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* $param string $returnValue |
210
|
|
|
* @return m\MockInterface |
211
|
|
|
*/ |
212
|
|
|
protected function givenARule($returnValue) |
213
|
|
|
{ |
214
|
|
|
$rule = m::mock('phpDocumentor\Transformer\Router\Rule'); |
215
|
|
|
$rule->shouldReceive('generate')->andReturn($returnValue); |
216
|
|
|
return $rule; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $name |
221
|
|
|
* @return CollectionDescriptor |
222
|
|
|
*/ |
223
|
|
|
protected function givenACollectionDescriptor($name) |
224
|
|
|
{ |
225
|
|
|
$classDescriptor = m::mock('phpDocumentor\Descriptor\ClassDescriptor'); |
226
|
|
|
$classDescriptor->shouldReceive('getName')->andReturn($name); |
227
|
|
|
$collectionDescriptor = new CollectionDescriptor($classDescriptor); |
228
|
|
|
$collectionDescriptor->setTypes(['ClassDescriptor']); |
|
|
|
|
229
|
|
|
return $collectionDescriptor; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param Rule $rule |
234
|
|
|
* @return m\MockInterface |
235
|
|
|
*/ |
236
|
|
|
private function givenAQueue($rule) |
237
|
|
|
{ |
238
|
|
|
$queue = m::mock('phpDocumentor\Transformer\Router\Queue'); |
239
|
|
|
$router = m::mock('phpDocumentor\Transformer\Router\StandardRouter'); |
240
|
|
|
$queue->shouldReceive('insert'); |
241
|
|
|
$router->shouldReceive('match')->andReturn($rule); |
242
|
|
|
return $queue; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
|
|
public function provideUrls() |
249
|
|
|
{ |
250
|
|
|
return [ |
251
|
|
|
['http://phpdoc.org'], |
252
|
|
|
['https://phpdoc.org'], |
253
|
|
|
['ftp://phpdoc.org'] |
254
|
|
|
]; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: