Completed
Push — develop ( 4557a4...04dbcf )
by Jaap
09:52
created

testMarkdownReturnsInputUnchangedWhenInputIsNotString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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-2017 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 \PHPUnit_Framework_TestCase
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);
0 ignored issues
show
Documentation introduced by
$text is of type array, but the function expects a string.

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...
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);
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