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

RendererTest::testGetAndSetRouters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
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
Bug introduced by
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
Bug introduced by
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');
0 ignored issues
show
Documentation introduced by
$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...
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
Bug introduced by
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
Bug introduced by
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
Bug introduced by
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
Bug introduced by
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']);
0 ignored issues
show
Documentation introduced by
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...
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
Bug introduced by
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
Bug introduced by
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
Bug introduced by
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);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
181
        $collectionDescriptor = new CollectionDescriptor($classDescriptor);
182
        $collectionDescriptor->setTypes(['ClassDescriptor']);
0 ignored issues
show
Documentation introduced by
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...
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