Completed
Push — develop ( 0cc20e...dccdc9 )
by Mike
07:04
created

phpDocumentor/Transformer/Router/RendererTest.php (9 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 declare(strict_types=1);
2
3
/**
4
 * This file is part of phpDocumentor.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @copyright 2010-2018 Mike van Riel<[email protected]>
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Transformer\Router;
15
16
use Mockery as m;
17
use phpDocumentor\Descriptor\Collection;
18
use phpDocumentor\Descriptor\Type\CollectionDescriptor;
19
20
/**
21
 * Test class for phpDocumentor\Transformer\Router\Renderer
22
 *
23
 * @coversDefaultClass \phpDocumentor\Transformer\Router\Renderer
24
 * @covers ::<private>
25
 */
26
final class RendererTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
27
{
28
    /** @var Queue */
29
    private $queue;
30
31
    /** @var Renderer */
32
    private $renderer;
33
34
    protected function setUp()
35
    {
36
        $this->queue = m::mock('phpDocumentor\Transformer\Router\Queue');
37
38
        $this->renderer = new Renderer($this->queue);
39
    }
40
41
    /**
42
     * @covers \phpDocumentor\Transformer\Router\Renderer::__construct
43
     * @covers \phpDocumentor\Transformer\Router\Renderer::getDestination
44
     * @covers \phpDocumentor\Transformer\Router\Renderer::setDestination
45
     */
46
    public function testGetAndSetDestination(): void
47
    {
48
        $this->renderer->setDestination('destination');
49
50
        $this->assertSame('destination', $this->renderer->getDestination());
51
    }
52
53
    /**
54
     * @covers ::render
55
     * @covers ::convertToRootPath
56
     */
57
    public function testRenderWithFqsenAndRepresentationUrl(): void
58
    {
59
        $rule = $this->givenARule('/classes/My.Namespace.Class.html');
60
        $this->queue->shouldReceive('match')->andReturn($rule);
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<phpDocumentor\Transformer\Router\Queue>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
62
        $result = $this->renderer->render('\My\Namespace\Class', 'url');
63
64
        $this->assertSame('classes/My.Namespace.Class.html', $result);
65
    }
66
67
    /**
68
     * @covers ::render
69
     * @covers ::convertToRootPath
70
     */
71
    public function testRenderWithCollectionOfFqsensAndRepresentationUrl(): void
72
    {
73
        $rule = $this->givenARule('/classes/My.Namespace.Class.html');
74
        $this->queue->shouldReceive('match')->andReturn($rule);
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<phpDocumentor\Transformer\Router\Queue>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
76
        $this->renderer->setDestination(str_replace('/', DIRECTORY_SEPARATOR, '/root/of/project'));
77
        $collection = new Collection(['\My\Namespace\Class']);
78
        $result = $this->renderer->render($collection, 'url');
79
80
        $this->assertSame(['../../../classes/My.Namespace.Class.html'], $result);
81
    }
82
83
    /**
84
     * @covers ::render
85
     * @covers ::convertToRootPath
86
     */
87
    public function testRenderWithUrlAndNoRuleMatch(): void
88
    {
89
        $rule = $this->givenARule('@');
90
        $this->queue->shouldReceive('match')->with('file://phpdoc')->andReturn($rule);
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<phpDocumentor\Transformer\Router\Queue>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
        $this->queue->shouldReceive('match')->with('@')->andReturn(null);
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<phpDocumentor\Transformer\Router\Queue>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
93
        $result = $this->renderer->render('file://phpdoc', 'url');
94
95
        $this->assertNull($result);
96
    }
97
98
    /**
99
     * @covers ::convertToRootPath
100
     */
101
    public function testConvertToRootPathWithUrlAndAtSignInRelativePath(): void
102
    {
103
        $rule = $this->givenARule('@Class::$property');
104
        $this->queue->shouldReceive('match')->with('@Class::$property')->andReturn($rule);
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<phpDocumentor\Transformer\Router\Queue>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
106
        $result = $this->renderer->convertToRootPath('@Class::$property');
107
108
        $this->assertSame('@Class::$property', $result);
109
    }
110
111
    /**
112
     * @covers ::render
113
     * @covers ::convertToRootPath
114
     */
115
    public function testRenderWithCollectionDescriptorWithNameIsNotArrayAndRepresentationUrl(): void
116
    {
117
        $rule = $this->givenARule('ClassDescriptor');
118
        $this->queue->shouldReceive('match')->andReturn($rule);
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<phpDocumentor\Transformer\Router\Queue>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
120
        $collectionDescriptor = $this->givenACollectionDescriptor('class');
121
        $collectionDescriptor->setKeyTypes(['ClassDescriptor']);
122
        $result = $this->renderer->render($collectionDescriptor, 'url');
123
124
        $this->assertSame('ClassDescriptor&lt;ClassDescriptor,ClassDescriptor&gt;', $result);
125
    }
126
127
    /**
128
     * @covers ::render
129
     * @covers ::convertToRootPath
130
     */
131
    public function testRenderWithCollectionDescriptorWithNameIsArrayAndRepresentationUrl(): void
132
    {
133
        $rule = $this->givenARule('ClassDescriptor');
134
        $this->queue->shouldReceive('match')->andReturn($rule);
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<phpDocumentor\Transformer\Router\Queue>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
136
        $collectionDescriptor = $this->givenACollectionDescriptor('array');
137
        $result = $this->renderer->render($collectionDescriptor, 'url');
138
139
        $this->assertSame('ClassDescriptor[]', $result);
140
    }
141
142
    /**
143
     * @covers ::render
144
     */
145
    public function testRenderWithFqsenAndRepresentationClassShort(): void
146
    {
147
        $rule = $this->givenARule('/classes/My.Namespace.Class.html');
148
        $this->queue->shouldReceive('match')->andReturn($rule);
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<phpDocumentor\Transformer\Router\Queue>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
150
        $result = $this->renderer->render('\My\Namespace\Class', 'class:short');
151
152
        $this->assertSame('<a href="classes/My.Namespace.Class.html">Class</a>', $result);
153
    }
154
155
    /**
156
     * @covers ::render
157
     * @dataProvider provideUrls
158
     */
159
    public function testRenderWithUrl(string $url): void
160
    {
161
        $rule = $this->givenARule($url);
162
        $this->queue->shouldReceive('match')->andReturn($rule);
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<phpDocumentor\Transformer\Router\Queue>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163
164
        $result = $this->renderer->render($url, 'url');
165
166
        $this->assertSame($url, $result);
167
    }
168
169
    private function givenARule(string $returnValue): Rule
170
    {
171
        $rule = m::mock('phpDocumentor\Transformer\Router\Rule');
172
        $rule->shouldReceive('generate')->andReturn($returnValue);
173
174
        return $rule;
175
    }
176
177
    private function givenACollectionDescriptor(string $name): CollectionDescriptor
178
    {
179
        $classDescriptor = m::mock('phpDocumentor\Descriptor\ClassDescriptor');
180
        $classDescriptor->shouldReceive('getName')->andReturn($name);
181
        $collectionDescriptor = new CollectionDescriptor($classDescriptor);
182
        $collectionDescriptor->setTypes(['ClassDescriptor']);
183
        return $collectionDescriptor;
184
    }
185
186
    public function provideUrls(): array
187
    {
188
        return [
189
            ['http://phpdoc.org'],
190
            ['https://phpdoc.org'],
191
            ['ftp://phpdoc.org'],
192
        ];
193
    }
194
}
195