Completed
Push — develop ( 67be7e...e4ed82 )
by Jaap
03:36
created

Compiler/Pass/ExampleTagsEnricherTest.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
namespace phpDocumentor\Compiler\Pass;
4
5
use Mockery as m;
6
use phpDocumentor\Descriptor\Example\Finder;
7
use phpDocumentor\Reflection\DocBlock\ExampleFinder;
8
9
/**
10
 * Tests the \phpDocumentor\Compiler\Pass\ExampleTagsEnricher class.
11
 */
12
class ExampleTagsEnricherTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
13
{
14
    /** @var Finder|m\MockInterface */
15
    private $finderMock;
16
17
    /** @var ExampleTagsEnricher */
18
    private $fixture;
19
20
    /**
21
     * Initializes the fixture and its dependencies.
22
     */
23
    protected function setUp()
24
    {
25
        $this->finderMock = m::mock(ExampleFinder::class);
26
        $this->fixture    = new ExampleTagsEnricher($this->finderMock);
27
    }
28
    /**
29
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::getDescription
30
     */
31
    public function testDescriptionName()
32
    {
33
        $this->assertSame('Enriches inline example tags with their sources', $this->fixture->getDescription());
34
    }
35
36
    /**
37
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::__construct
38
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::execute
39
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::replaceInlineExamples
40
     */
41
    public function testReplaceExampleTagReturnsDescriptionIfItContainsNoExampleTags()
42
    {
43
        $description = 'This is a description';
44
45
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
46
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $description);
47
48
        $project = $this->givenAProjectDescriptorWithChildDescriptors(array($descriptor));
49
50
        $this->fixture->execute($project);
51
52
        $this->assertTrue(true);
53
    }
54
55
    /**
56
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::__construct
57
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::execute
58
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::replaceInlineExamples
59
     */
60
    public function testReplaceExampleTagWithExampleContents()
61
    {
62
        $exampleText = 'Example Text';
63
        $description = 'This is a description with {@example example2.txt} without description.';
64
        $expected    = "This is a description with `$exampleText` without description.";
65
66
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
67
        $this->whenExampleTxtFileContains($exampleText);
68
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
69
70
        $project = $this->givenAProjectDescriptorWithChildDescriptors(array($descriptor));
71
72
        $this->fixture->execute($project);
73
74
        $this->assertTrue(true);
75
    }
76
77
    /**
78
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::__construct
79
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::execute
80
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::replaceInlineExamples
81
     */
82
    public function testReplaceExampleTagWithExampleContentsAndDescription()
83
    {
84
        $exampleText = 'Example Text';
85
        $description = 'This is a description with {@example example.txt including description}.';
86
        $expected    = "This is a description with *including description*`$exampleText`.";
87
88
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
89
        $this->whenExampleTxtFileContains($exampleText);
90
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
91
92
        $project = $this->givenAProjectDescriptorWithChildDescriptors(array($descriptor));
93
94
        $this->fixture->execute($project);
95
96
        $this->assertTrue(true);
97
    }
98
99
    /**
100
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::__construct
101
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::execute
102
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::replaceInlineExamples
103
     */
104
    public function testReplacingOfDescriptionHappensOncePerExample()
105
    {
106
        $exampleText = 'Example Text';
107
        $description = 'This is a description with {@example example.txt} and {@example example.txt}.';
108
        $expected    = "This is a description with `$exampleText` and `$exampleText`.";
109
110
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
111
        $this->whenExampleTxtFileContainsAndMustBeCalledOnlyOnce($exampleText);
112
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
113
114
        $project = $this->givenAProjectDescriptorWithChildDescriptors(array($descriptor));
115
116
        $this->fixture->execute($project);
117
118
        $this->assertTrue(true);
119
    }
120
121
    /**
122
     * Returns a mocked Descriptor with its description set to the given value.
123
     *
124
     * @param string $description
125
     *
126
     * @return m\MockInterface
127
     */
128
    private function givenAChildDescriptorWithDescription($description)
129
    {
130
        $descriptor = m::mock('phpDocumentor\Descriptor\DescriptorAbstract');
131
        $descriptor->shouldReceive('getDescription')->andReturn($description);
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...
132
133
        return $descriptor;
134
    }
135
136
    /**
137
     * Returns a mocked Project Descriptor.
138
     *
139
     * @param m\MockInterface[] $descriptors
140
     *
141
     * @return m\MockInterface
142
     */
143
    private function givenAProjectDescriptorWithChildDescriptors($descriptors)
144
    {
145
        $projectDescriptor = m::mock('phpDocumentor\Descriptor\ProjectDescriptor');
146
        $projectDescriptor->shouldReceive('getIndexes->get')->with('elements')->andReturn($descriptors);
147
148
        return $projectDescriptor;
149
    }
150
151
    /**
152
     * Verifies if the given descriptor's setDescription method is called with the given value.
153
     *
154
     * @param m\MockInterface $descriptor
155
     * @param string          $expected
156
     *
157
     * @return void
158
     */
159
    public function thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected)
160
    {
161
        $descriptor->shouldReceive('setDescription')->with($expected);
162
    }
163
164
    /**
165
     * Instructs the finder mock to return the given text when an example is requested.
166
     *
167
     * @param string $exampleText
168
     *
169
     * @return void
170
     */
171
    private function whenExampleTxtFileContains($exampleText)
172
    {
173
        $this->finderMock->shouldReceive('find')->andReturn($exampleText);
174
    }
175
176
    /**
177
     * Instructs the finder mock to return the given text when an example is requested and verifies that that is only
178
     * done once.
179
     *
180
     * @param string $exampleText
181
     *
182
     * @return void
183
     */
184
    private function whenExampleTxtFileContainsAndMustBeCalledOnlyOnce($exampleText)
185
    {
186
        $this->finderMock->shouldReceive('find')->once()->andReturn($exampleText);
187
    }
188
}
189