Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

Compiler/Pass/ExampleTagsEnricherTest.php (3 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
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
    /**
30
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::getDescription
31
     */
32
    public function testDescriptionName()
33
    {
34
        $this->assertSame('Enriches inline example tags with their sources', $this->fixture->getDescription());
35
    }
36
37
    /**
38
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::__construct
39
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::execute
40
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::replaceInlineExamples
41
     */
42
    public function testReplaceExampleTagReturnsDescriptionIfItContainsNoExampleTags()
43
    {
44
        $description = 'This is a description';
45
46
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
47
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $description);
48
49
        $project = $this->givenAProjectDescriptorWithChildDescriptors([$descriptor]);
50
51
        $this->fixture->execute($project);
52
53
        $this->assertTrue(true);
54
    }
55
56
    /**
57
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::__construct
58
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::execute
59
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::replaceInlineExamples
60
     */
61
    public function testReplaceExampleTagWithExampleContents()
62
    {
63
        $exampleText = 'Example Text';
64
        $description = 'This is a description with {@example example2.txt} without description.';
65
        $expected = "This is a description with `${exampleText}` without description.";
66
67
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
68
        $this->whenExampleTxtFileContains($exampleText);
69
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
70
71
        $project = $this->givenAProjectDescriptorWithChildDescriptors([$descriptor]);
72
73
        $this->fixture->execute($project);
74
75
        $this->assertTrue(true);
76
    }
77
78
    /**
79
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::__construct
80
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::execute
81
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::replaceInlineExamples
82
     */
83
    public function testReplaceExampleTagWithExampleContentsAndDescription()
84
    {
85
        $exampleText = 'Example Text';
86
        $description = 'This is a description with {@example example.txt including description}.';
87
        $expected = "This is a description with *including description*`${exampleText}`.";
88
89
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
90
        $this->whenExampleTxtFileContains($exampleText);
91
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
92
93
        $project = $this->givenAProjectDescriptorWithChildDescriptors([$descriptor]);
94
95
        $this->fixture->execute($project);
96
97
        $this->assertTrue(true);
98
    }
99
100
    /**
101
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::__construct
102
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::execute
103
     * @covers \phpDocumentor\Compiler\Pass\ExampleTagsEnricher::replaceInlineExamples
104
     */
105
    public function testReplacingOfDescriptionHappensOncePerExample()
106
    {
107
        $exampleText = 'Example Text';
108
        $description = 'This is a description with {@example example.txt} and {@example example.txt}.';
109
        $expected = "This is a description with `${exampleText}` and `${exampleText}`.";
110
111
        $descriptor = $this->givenAChildDescriptorWithDescription($description);
112
        $this->whenExampleTxtFileContainsAndMustBeCalledOnlyOnce($exampleText);
113
        $this->thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected);
114
115
        $project = $this->givenAProjectDescriptorWithChildDescriptors([$descriptor]);
116
117
        $this->fixture->execute($project);
118
119
        $this->assertTrue(true);
120
    }
121
122
    /**
123
     * Returns a mocked Descriptor with its description set to the given value.
124
     *
125
     * @param string $description
126
     *
127
     * @return m\MockInterface
128
     */
129
    private function givenAChildDescriptorWithDescription($description)
130
    {
131
        $descriptor = m::mock('phpDocumentor\Descriptor\DescriptorAbstract');
132
        $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...
133
134
        return $descriptor;
135
    }
136
137
    /**
138
     * Returns a mocked Project Descriptor.
139
     *
140
     * @param m\MockInterface[] $descriptors
141
     *
142
     * @return m\MockInterface
143
     */
144
    private function givenAProjectDescriptorWithChildDescriptors($descriptors)
145
    {
146
        $projectDescriptor = m::mock('phpDocumentor\Descriptor\ProjectDescriptor');
147
        $projectDescriptor->shouldReceive('getIndexes->get')->with('elements')->andReturn($descriptors);
148
149
        return $projectDescriptor;
150
    }
151
152
    /**
153
     * Verifies if the given descriptor's setDescription method is called with the given value.
154
     *
155
     * @param m\MockInterface $descriptor
156
     * @param string          $expected
157
     */
158
    public function thenDescriptionOfDescriptorIsChangedInto($descriptor, $expected)
159
    {
160
        $descriptor->shouldReceive('setDescription')->with($expected);
161
    }
162
163
    /**
164
     * Instructs the finder mock to return the given text when an example is requested.
165
     *
166
     * @param string $exampleText
167
     */
168
    private function whenExampleTxtFileContains($exampleText)
169
    {
170
        $this->finderMock->shouldReceive('find')->andReturn($exampleText);
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\Example\Finder.

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...
171
    }
172
173
    /**
174
     * Instructs the finder mock to return the given text when an example is requested and verifies that that is only
175
     * done once.
176
     *
177
     * @param string $exampleText
178
     */
179
    private function whenExampleTxtFileContainsAndMustBeCalledOnlyOnce($exampleText)
180
    {
181
        $this->finderMock->shouldReceive('find')->once()->andReturn($exampleText);
0 ignored issues
show
The method shouldReceive does only exist in Mockery\MockInterface, but not in phpDocumentor\Descriptor\Example\Finder.

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
    }
183
}
184