Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

Compiler/Pass/ResolveInlineLinkAndSeeTagsTest.php (5 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-2015 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\Compiler\Pass;
14
15
use Mockery as m;
16
use phpDocumentor\Descriptor\DescriptorAbstract;
17
use phpDocumentor\Descriptor\ProjectDescriptor;
18
use phpDocumentor\Transformer\Router\Queue;
19
use phpDocumentor\Transformer\Router\Rule;
20
use phpDocumentor\Transformer\Template\Collection;
21
22
/**
23
 * @coversDefaultClass \phpDocumentor\Compiler\Pass\ResolveInlineLinkAndSeeTags
24
 * @covers ::__construct
25
 * @covers ::<private>
26
 */
27
class ResolveInlineLinkAndSeeTagsTest extends \PHPUnit_Framework_TestCase
28
{
29
    /** @var Queue||m\MockInterface */
30
    private $router;
31
32
    /** @var ResolveInlineLinkAndSeeTags */
33
    private $fixture;
34
35
    /**
36
     * Initializes the fixture and its dependencies.
37
     */
38
    protected function setUp()
39
    {
40
        $this->router = m::mock('phpDocumentor\Transformer\Router\Queue');
41
        $this->fixture = new ResolveInlineLinkAndSeeTags($this->router);
42
    }
43
44
    /**
45
     * @covers ::getDescription
46
     */
47
    public function testDescriptionName()
48
    {
49
        $this->assertSame('Resolve @link and @see tags in descriptions', $this->fixture->getDescription());
50
    }
51
52
    /**
53
     * @covers ::execute
54
     */
55
    public function testReplaceDescriptionIfItContainsNoSeeOrLink()
56
    {
57
        $description = 'This is a description';
58
59
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
60
        $collection = $this->givenACollection($descriptor);
61
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $description);
62
63
        $project = $this->givenAProjectDescriptorWithChildDescriptors($collection);
64
65
        $this->fixture->execute($project);
66
    }
67
68
    /**
69
     * @covers ::execute
70
     */
71
    public function testReplaceDescriptionIfItContainsASeeButFileIsNotAvailable()
72
    {
73
        $description = 'Description with {@see ARandomDescriptor}';
74
        $expected = 'Description with \ARandomDescriptor';
75
76
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
77
        $collection = $this->givenACollection($descriptor);
78
        $elementToLinkTo = $this->givenAnElementToLinkTo();
79
80
        $this->whenDescriptionContainsSeeOrLinkWithElement($descriptor, $elementToLinkTo);
0 ignored issues
show
It seems like $elementToLinkTo defined by $this->givenAnElementToLinkTo() on line 78 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Compiler\P...sSeeOrLinkWithElement() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81
82
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
83
84
        $project = $this->givenAProjectDescriptorWithChildDescriptors($collection);
85
86
        $this->fixture->execute($project);
87
    }
88
89
    /**
90
     * @covers ::execute
91
     */
92
    public function testReplaceDescriptionIfItContainsASeeAndFileIsPresent()
93
    {
94
        $description = 'Description with {@see LinkDescriptor}';
95
        $expected = 'Description with [\phpDocumentor\LinkDescriptor](../classes/phpDocumentor.LinkDescriptor.html)';
96
97
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
98
        $collection = $this->givenACollection($descriptor);
99
        $elementToLinkTo = $this->givenAnElementToLinkTo();
100
101
        $this->whenDescriptionContainsSeeOrLinkWithElement($descriptor, $elementToLinkTo);
0 ignored issues
show
It seems like $elementToLinkTo defined by $this->givenAnElementToLinkTo() on line 99 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Compiler\P...sSeeOrLinkWithElement() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
102
103
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
104
105
        $project = $this->givenAProjectDescriptorWithChildDescriptors($collection);
106
107
        $this->fixture->execute($project);
108
    }
109
110
    /**
111
     * @covers ::execute
112
     */
113
    public function testReplaceDescriptionIfItContainsALinkAndFileIsPresent()
114
    {
115
        $description = 'Description with {@link LinkDescriptor}';
116
        $expected =
117
            'Description with [LinkDescriptor](../classes/phpDocumentor.LinkDescriptor.html)';
118
119
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
120
        $collection = $this->givenACollection($descriptor);
121
        $elementToLinkTo = $this->givenAnElementToLinkTo();
122
123
        $this->whenDescriptionContainsSeeOrLinkWithElement($descriptor, $elementToLinkTo);
0 ignored issues
show
It seems like $elementToLinkTo defined by $this->givenAnElementToLinkTo() on line 121 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Compiler\P...sSeeOrLinkWithElement() does only seem to accept object<phpDocumentor\Des...tor\DescriptorAbstract>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
124
125
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
126
127
        $project = $this->givenAProjectDescriptorWithChildDescriptors($collection);
128
129
        $this->fixture->execute($project);
130
    }
131
132
    /**
133
     * @covers ::execute
134
     */
135
    public function testReplaceDescriptionIfItContainsAUrl()
136
    {
137
        $description = 'Description with {@see http://www.phpdoc.org}';
138
        $expected = 'Description with [http://www.phpdoc.org](http://www.phpdoc.org)';
139
140
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
141
        $collection = $this->givenACollection($descriptor);
142
143
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
144
145
        $project = $this->givenAProjectDescriptorWithChildDescriptors($collection);
146
147
        $this->fixture->execute($project);
148
    }
149
150
    /**
151
     * @covers ::execute
152
     */
153
    public function testReplaceDescriptionIfItContainsAnotherTag()
154
    {
155
        $description = 'Description with {@author John Doe}';
156
        $expected = 'Description with {@author John Doe}';
157
158
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
159
        $collection = $this->givenACollection($descriptor);
160
161
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
162
163
        $project = $this->givenAProjectDescriptorWithChildDescriptors($collection);
164
165
        $this->fixture->execute($project);
166
    }
167
168
    /**
169
     * Returns a mocked Descriptor with its description set to the given value.
170
     *
171
     * @param string $description
172
     *
173
     * @return m\MockInterface
174
     */
175
    private function givenAChildDescriptorWithDescription($description)
176
    {
177
        $descriptor = m::mock('phpDocumentor\Descriptor\DescriptorAbstract');
178
        $descriptor->shouldReceive('getDescription')->andReturn($description);
179
180
        return $descriptor;
181
    }
182
183
    /**
184
     * Returns a mocked Project Descriptor.
185
     *
186
     * @param Collection|m\MockInterface $descriptors
187
     *
188
     * @return m\MockInterface
189
     */
190
    private function givenAProjectDescriptorWithChildDescriptors($descriptors)
191
    {
192
        $projectDescriptor = m::mock('phpDocumentor\Descriptor\ProjectDescriptor');
193
        $projectDescriptor->shouldReceive('getIndexes')->andReturn($descriptors);
194
195
        return $projectDescriptor;
196
    }
197
198
    /**
199
     * Returns the descriptor of the element that the link points to
200
     *
201
     * @return DescriptorAbstract|m\MockInterface
202
     */
203
    private function givenAnElementToLinkTo()
204
    {
205
        $namespaceAliases = array('LinkDescriptor' => '\phpDocumentor\LinkDescriptor');
206
        $namespaceCollection = m::mock('phpDocumentor\Transformer\Template\Collection');
207
        $namespaceCollection->shouldReceive('getAll')->once()->andReturn($namespaceAliases);
208
209
        $elementToLinkTo = m::mock('phpDocumentor\Descriptor\DescriptorAbstract');
210
        $elementToLinkTo->shouldReceive('getNamespaceAliases')->once()->andReturn($namespaceCollection);
211
212
        return $elementToLinkTo;
213
    }
214
215
    /**
216
     * Returns a collection with descriptor. This collection will be scanned to see if a link can be made to a file.
217
     *
218
     * @param DescriptorAbstract|m\MockInterface $descriptor
219
     *
220
     * @return Collection|m\MockInterface
221
     */
222
    private function givenACollection($descriptor)
223
    {
224
        $collection = m::mock('phpDocumentor\Transformer\Template\Collection');
225
226
        $items = array('\phpDocumentor\LinkDescriptor' => $descriptor);
227
228
        $collection->shouldReceive('get')->once()->andReturn($items);
229
230
        return $collection;
231
    }
232
233
    /**
234
     * Verifies if the given descriptor's setDescription method is called with the given value.
235
     *
236
     * @param m\MockInterface $descriptor
237
     * @param string          $expected
238
     *
239
     * @return void
240
     */
241
    public function thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected)
242
    {
243
        $descriptor->shouldReceive('setDescription')->with($expected);
244
    }
245
246
    /**
247
     * It resolves the element that is linked to
248
     *
249
     * @param DescriptorAbstract $descriptor
250
     * @param DescriptorAbstract $elementToLinkTo
251
     *
252
     * @return DescriptorAbstract
253
     */
254
    private function whenDescriptionContainsSeeOrLinkWithElement($descriptor, $elementToLinkTo)
255
    {
256
        $rule = m::mock('phpDocumentor\Transformer\Router\Rule');
257
        $rule->shouldReceive('generate')->andReturn('/classes/phpDocumentor.LinkDescriptor.html');
258
        $this->router->shouldReceive('match')->andReturn($rule);
259
        $descriptor->shouldReceive('getFile')->andReturn($elementToLinkTo);
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<phpDocumentor\Des...tor\DescriptorAbstract>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
260
        $descriptor->shouldReceive('getNamespace');
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<phpDocumentor\Des...tor\DescriptorAbstract>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
261
262
        return $descriptor;
263
    }
264
}
265