Completed
Push — develop ( 80740b...61b5c3 )
by Mike
10:20
created

phpDocumentor/Plugin/Core/Xslt/ExtensionTest.php (1 issue)

Labels
Severity

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
/**
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\Plugin\Core\Xslt;
15
16
use Mockery as m;
17
18
/**
19
 * Test class for \phpDocumentor\Plugin\Core\Xslt\Extension.
20
 *
21
 * @covers \phpDocumentor\Plugin\Core\Xslt\Extension
22
 */
23
class ExtensionTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
24
{
25
    /**
26
     * @covers \phpDocumentor\Plugin\Core\Xslt\Extension::markdown
27
     */
28
    public function testMarkdownWithString()
29
    {
30
        $text = '`this is markdown`';
31
32
        $result = Extension::markdown($text);
33
34
        $this->assertSame('<p><code>this is markdown</code></p>', $result);
35
    }
36
37
    /**
38
     * @covers \phpDocumentor\Plugin\Core\Xslt\Extension::markdown
39
     */
40
    public function testMarkdownReturnsInputUnchangedWhenInputIsNotString()
41
    {
42
        $text = [];
43
44
        $result = Extension::markdown($text);
45
46
        $this->assertSame([], $result);
47
    }
48
49
    /**
50
     * @covers \phpDocumentor\Plugin\Core\Xslt\Extension::path
51
     */
52
    public function testPathWithExternalLink()
53
    {
54
        $elementList = m::mock('phpDocumentor\Descriptor\Collection');
55
        $elementList->shouldReceive('get')->andReturn(null);
56
57
        $projectDescriptorBuilder = $this->givenAProjectDescriptorBuilder($elementList);
58
59
        $rule = m::mock('phpDocumentor\Transformer\Router');
60
        $rule->shouldReceive('generate')->andReturn('http://phpdoc.org');
61
        $router = $this->givenARouter($rule);
62
63
        Extension::$descriptorBuilder = $projectDescriptorBuilder;
64
        Extension::$routers = $router;
65
        $result = Extension::path('http://phpdoc.org');
66
67
        $this->assertSame('http://phpdoc.org', $result);
68
    }
69
70
    /**
71
     * @covers \phpDocumentor\Plugin\Core\Xslt\Extension::path
72
     */
73
    public function testPathWithUndocumentedElement()
74
    {
75
        $elementList = m::mock('phpDocumentor\Descriptor\Collection');
76
        $elementList->shouldReceive('get')->andReturn(null);
77
78
        $projectDescriptorBuilder = $this->givenAProjectDescriptorBuilder($elementList);
79
80
        $router = $this->givenARouter(null);
81
82
        Extension::$descriptorBuilder = $projectDescriptorBuilder;
83
        Extension::$routers = $router;
84
        $result = Extension::path('undocumented');
85
86
        $this->assertSame('', $result);
87
    }
88
89
    /**
90
     * @covers \phpDocumentor\Plugin\Core\Xslt\Extension::path
91
     */
92
    public function testPathWithDocumentedElement()
93
    {
94
        $elementList = m::mock('phpDocumentor\Descriptor\Collection');
95
        $element = m::mock('phpDocumentor\Descriptor\Collection');
96
        $element->shouldReceive('offsetExists')->andReturn(true);
97
        $element->shouldReceive('offsetGet');
98
        $elementList->shouldReceive('get')->andReturn($element);
99
100
        $projectDescriptorBuilder = $this->givenAProjectDescriptorBuilder($elementList);
101
102
        $rule = m::mock('phpDocumentor\Transformer\Router');
103
        $rule->shouldReceive('generate')->andReturn('/classes/my.namespace.class.html');
104
105
        $router = $this->givenARouter($rule);
106
107
        Extension::$descriptorBuilder = $projectDescriptorBuilder;
108
        Extension::$routers = $router;
109
        $result = Extension::path('\\my\\namespace\\class');
110
111
        $this->assertSame('classes/my.namespace.class.html', $result);
112
    }
113
114
    /**
115
     * @covers \phpDocumentor\Plugin\Core\Xslt\Extension::typeOfElement
116
     */
117
    public function testTypeOfElementWithUrl()
118
    {
119
        $elementList = m::mock('phpDocumentor\Descriptor\Collection');
120
        $elementList->shouldReceive('get')->andReturn(null);
121
122
        $projectDescriptorBuilder = $this->givenAProjectDescriptorBuilder($elementList);
123
124
        $router = $this->givenARouter(null);
125
126
        Extension::$descriptorBuilder = $projectDescriptorBuilder;
127
        Extension::$routers = $router;
128
        $result = Extension::typeOfElement('http://phpdoc.org');
129
130
        $this->assertSame('url', $result);
131
    }
132
133
    /**
134
     * @covers \phpDocumentor\Plugin\Core\Xslt\Extension::typeOfElement
135
     */
136
    public function testTypeOfElementWithUndocumentedElement()
137
    {
138
        $elementList = m::mock('phpDocumentor\Descriptor\Collection');
139
        $elementList->shouldReceive('get')->andReturn(null);
140
141
        $projectDescriptorBuilder = $this->givenAProjectDescriptorBuilder($elementList);
142
143
        $router = $this->givenARouter(null);
144
145
        Extension::$descriptorBuilder = $projectDescriptorBuilder;
146
        Extension::$routers = $router;
147
        $result = Extension::typeOfElement('undocumented element');
148
149
        $this->assertSame('undocumented', $result);
150
    }
151
152
    /**
153
     * @covers \phpDocumentor\Plugin\Core\Xslt\Extension::typeOfElement
154
     */
155
    public function testTypeOfElementWithDocumentedElement()
156
    {
157
        $elementList = m::mock('phpDocumentor\Descriptor\Collection');
158
        $element = m::mock('phpDocumentor\Descriptor\Collection');
159
        $element->shouldReceive('offsetExists')->with('my\\namespace')->andReturn(false);
160
        $element->shouldReceive('offsetExists')->with('~\\my\\namespace')->andReturn(true);
161
        $element->shouldReceive('offsetGet')->with('~\\my\\namespace')->andReturn('the namespace descriptor');
162
        $elementList->shouldReceive('get')->andReturn($element);
163
164
        $projectDescriptorBuilder = $this->givenAProjectDescriptorBuilder($elementList);
165
166
        $rule = m::mock('phpDocumentor\Transformer\Router');
167
        $rule->shouldReceive('generate')->andReturn('/classes/my.namespace.class.html');
168
169
        $router = $this->givenARouter(null);
170
171
        Extension::$descriptorBuilder = $projectDescriptorBuilder;
172
        Extension::$routers = $router;
173
        $result = Extension::typeOfElement('my\\namespace');
174
175
        $this->assertSame('documented', $result);
176
    }
177
178
    private function givenAProjectDescriptorBuilder($elementList)
179
    {
180
        $projectDescriptor = m::mock('\phpDocumentor\Descriptor\ProjectDescriptor');
181
        $projectDescriptor->shouldReceive('getIndexes')->andReturn($elementList);
0 ignored issues
show
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...
182
        $projectDescriptorBuilder = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder');
183
        $projectDescriptorBuilder->shouldReceive('getProjectDescriptor')->andReturn($projectDescriptor);
184
        return $projectDescriptorBuilder;
185
    }
186
187
    private function givenARouter($rule)
188
    {
189
        $queue = m::mock('phpDocumentor\Transformer\Router\Queue');
190
        $router = m::mock('phpDocumentor\Transformer\Router\StandardRouter');
191
        $queue->shouldReceive('insert');
192
        $router->shouldReceive('match')->andReturn($rule);
193
        return $router;
194
    }
195
}
196