Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

phpDocumentor/Transformer/Router/RendererTest.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-2018 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 \Mockery\Adapter\Phpunit\MockeryTestCase
26
{
27
    /**
28
     * @var Queue
29
     */
30
    private $originalQueue;
31
32
    /**
33
     * @var Renderer
34
     */
35
    private $renderer;
36
37
    protected function setUp()
38
    {
39
        $this->originalQueue = m::mock('phpDocumentor\Transformer\Router\Queue');
40
        $this->renderer = new Renderer($this->originalQueue);
41
    }
42
43
    /**
44
     * @covers ::__construct
45
     * @covers ::getRouters
46
     */
47
    public function testConstructRenderer()
48
    {
49
        $result = $this->renderer->getRouters();
50
51
        $this->assertSame($this->originalQueue, $result);
52
    }
53
54
    /**
55
     * @covers ::__construct
56
     * @covers ::getRouters
57
     * @covers ::setRouters
58
     */
59
    public function testGetAndSetRouters()
60
    {
61
        $rule = $this->givenARule('http://phpdoc.org');
62
        $newQueue = [$this->givenAQueue($rule)];
63
        $this->renderer->setRouters($newQueue);
0 ignored issues
show
$newQueue is of type array<integer,object<Moc...kery\\MockInterface>"}>, but the function expects a object<phpDocumentor\Transformer\Router\Queue>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
65
        $result = $this->renderer->getRouters();
66
67
        $this->assertNotSame($this->originalQueue, $result);
68
        $this->assertSame($newQueue, $result);
69
    }
70
71
    /**
72
     * @covers \phpDocumentor\Transformer\Router\Renderer::__construct
73
     * @covers \phpDocumentor\Transformer\Router\Renderer::getDestination
74
     * @covers \phpDocumentor\Transformer\Router\Renderer::setDestination
75
     */
76
    public function testGetAndSetDestination()
77
    {
78
        $this->renderer->setDestination('destination');
79
80
        $this->assertSame('destination', $this->renderer->getDestination());
81
    }
82
83
    /**
84
     * @covers ::render
85
     * @covers ::convertToRootPath
86
     */
87
    public function testRenderWithFqsenAndRepresentationUrl()
88
    {
89
        $rule = $this->givenARule('/classes/My.Namespace.Class.html');
90
        $queue = $this->givenAQueue($rule);
91
        $queue->shouldReceive('match')->andReturn($rule);
92
        $this->renderer->setRouters($queue);
93
        $result = $this->renderer->render('\My\Namespace\Class', 'url');
94
95
        $this->assertSame('classes/My.Namespace.Class.html', $result);
96
    }
97
98
    /**
99
     * @covers ::render
100
     * @covers ::convertToRootPath
101
     */
102
    public function testRenderWithCollectionOfFqsensAndRepresentationUrl()
103
    {
104
        $rule = $this->givenARule('/classes/My.Namespace.Class.html');
105
        $queue = $this->givenAQueue($rule);
106
        $queue->shouldReceive('match')->andReturn($rule);
107
        $this->renderer->setRouters($queue);
108
        $this->renderer->setDestination(str_replace('/', DIRECTORY_SEPARATOR, '/root/of/project'));
109
        $collection = new Collection(['\My\Namespace\Class']);
110
        $result = $this->renderer->render($collection, 'url');
0 ignored issues
show
$collection is of type object<phpDocumentor\Descriptor\Collection>, but the function expects a string|object<phpDocumen...tor\DescriptorAbstract>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
112
        $this->assertSame(['../../../classes/My.Namespace.Class.html'], $result);
113
    }
114
115
    /**
116
     * @covers ::render
117
     * @covers ::convertToRootPath
118
     */
119
    public function testRenderWithUrlAndNoRuleMatch()
120
    {
121
        $rule = $this->givenARule('@');
122
        $queue = $this->givenAQueue($rule);
123
        $queue->shouldReceive('match')->with('file://phpdoc')->andReturn($rule);
124
        $queue->shouldReceive('match')->with('@')->andReturn(null);
125
        $this->renderer->setRouters($queue);
126
        $result = $this->renderer->render('file://phpdoc', 'url');
127
128
        $this->assertNull($result);
129
    }
130
131
    /**
132
     * @covers ::convertToRootPath
133
     */
134
    public function testConvertToRootPathWithUrlAndAtSignInRelativePath()
135
    {
136
        $rule = $this->givenARule('@Class::$property');
137
        $queue = $this->givenAQueue($rule);
138
        $queue->shouldReceive('match')->with('@Class::$property')->andReturn($rule);
139
        $this->renderer->setRouters($queue);
140
        $result = $this->renderer->convertToRootPath('@Class::$property');
141
142
        $this->assertSame('@Class::$property', $result);
143
    }
144
145
    /**
146
     * @covers ::render
147
     * @covers ::convertToRootPath
148
     */
149
    public function testRenderWithCollectionDescriptorWithNameIsNotArrayAndRepresentationUrl()
150
    {
151
        $rule = $this->givenARule('ClassDescriptor');
152
        $queue = $this->givenAQueue($rule);
153
        $queue->shouldReceive('match')->andReturn($rule);
154
        $this->renderer->setRouters($queue);
155
        $collectionDescriptor = $this->givenACollectionDescriptor('class');
156
        $collectionDescriptor->setKeyTypes(['ClassDescriptor']);
0 ignored issues
show
array('ClassDescriptor') is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,object<php...erfaces\TypeInterface>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
        $result = $this->renderer->render($collectionDescriptor, 'url');
158
159
        $this->assertSame('ClassDescriptor&lt;ClassDescriptor,ClassDescriptor&gt;', $result);
160
    }
161
162
    /**
163
     * @covers ::render
164
     * @covers ::convertToRootPath
165
     */
166
    public function testRenderWithCollectionDescriptorWithNameIsArrayAndRepresentationUrl()
167
    {
168
        $rule = $this->givenARule('ClassDescriptor');
169
        $queue = $this->givenAQueue($rule);
170
        $queue->shouldReceive('match')->andReturn($rule);
171
        $this->renderer->setRouters($queue);
172
        $collectionDescriptor = $this->givenACollectionDescriptor('array');
173
        $result = $this->renderer->render($collectionDescriptor, 'url');
174
175
        $this->assertSame('ClassDescriptor[]', $result);
176
    }
177
178
    /**
179
     * @covers ::render
180
     */
181
    public function testRenderWithFqsenAndRepresentationClassShort()
182
    {
183
        $rule = $this->givenARule('/classes/My.Namespace.Class.html');
184
        $queue = $this->givenAQueue($rule);
185
        $queue->shouldReceive('match')->andReturn($rule);
186
        $this->renderer->setRouters($queue);
187
        $result = $this->renderer->render('\My\Namespace\Class', 'class:short');
188
189
        $this->assertSame('<a href="classes/My.Namespace.Class.html">Class</a>', $result);
190
    }
191
192
    /**
193
     * @covers ::render
194
     * @dataProvider provideUrls
195
     */
196
    public function testRenderWithUrl($url)
197
    {
198
        $rule = $this->givenARule($url);
199
        $queue = $this->givenAQueue($rule);
200
        $queue->shouldReceive('match')->andReturn($rule);
201
        $this->renderer->setRouters($queue);
202
        $result = $this->renderer->render($url, 'url');
203
204
        $this->assertSame($url, $result);
205
    }
206
207
    /**
208
     * $param string $returnValue
209
     * @return m\MockInterface
210
     */
211
    protected function givenARule($returnValue)
212
    {
213
        $rule = m::mock('phpDocumentor\Transformer\Router\Rule');
214
        $rule->shouldReceive('generate')->andReturn($returnValue);
215
        return $rule;
216
    }
217
218
    /**
219
     * @param string $name
220
     * @return CollectionDescriptor
221
     */
222
    protected function givenACollectionDescriptor($name)
223
    {
224
        $classDescriptor = m::mock('phpDocumentor\Descriptor\ClassDescriptor');
225
        $classDescriptor->shouldReceive('getName')->andReturn($name);
226
        $collectionDescriptor = new CollectionDescriptor($classDescriptor);
227
        $collectionDescriptor->setTypes(['ClassDescriptor']);
0 ignored issues
show
array('ClassDescriptor') is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,object<php...erfaces\TypeInterface>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
228
        return $collectionDescriptor;
229
    }
230
231
    /**
232
     * @param Rule $rule
233
     * @return m\MockInterface
234
     */
235
    private function givenAQueue($rule)
236
    {
237
        $queue = m::mock('phpDocumentor\Transformer\Router\Queue');
238
        $router = m::mock('phpDocumentor\Transformer\Router\StandardRouter');
239
        $queue->shouldReceive('insert');
240
        $router->shouldReceive('match')->andReturn($rule);
241
        return $queue;
242
    }
243
244
    /**
245
     * @return array
246
     */
247
    public function provideUrls()
248
    {
249
        return [
250
            ['http://phpdoc.org'],
251
            ['https://phpdoc.org'],
252
            ['ftp://phpdoc.org'],
253
        ];
254
    }
255
}
256