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

Builder/Reflector/Tags/ExampleAssemblerTest.php (1 issue)

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
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Descriptor\Builder\Reflector\Tags;
13
14
use Mockery as m;
15
use phpDocumentor\Descriptor\Example\Finder;
16
use phpDocumentor\Reflection\DocBlock\ExampleFinder;
17
use phpDocumentor\Reflection\DocBlock\Tags\Example;
18
19
/**
20
 * Tests for the \phpDocumentor\Descriptor\Builder\Reflector\Tags\ExampleAssembler class.
21
 */
22
class ExampleAssemblerTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
23
{
24
    const EXAMPLE_FILE_PATH = 'examples/example.txt';
25
26
    const EXAMPLE_STARTING_LINE = 10;
27
28
    const EXAMPLE_LINE_COUNT = 5;
29
30
    const EXAMPLE_DESCRIPTION = 'This is a description';
31
32
    const EXAMPLE_TEXT = 'This is an example';
33
34
    const TAG_NAME = 'example';
35
36
    /** @var ExampleAssembler */
37
    private $fixture;
38
39
    /** @var Finder|m\MockInterface */
40
    private $finderMock;
41
42
    /**
43
     * Initializes this fixture and its dependencies.
44
     */
45
    protected function setUp()
46
    {
47
        $this->finderMock = m::mock(ExampleFinder::class);
48
        $this->fixture = new ExampleAssembler($this->finderMock);
49
    }
50
51
    /**
52
     * @covers \phpDocumentor\Descriptor\Builder\Reflector\Tags\ExampleAssembler::__construct
53
     * @covers \phpDocumentor\Descriptor\Builder\Reflector\Tags\ExampleAssembler::create
54
     */
55
    public function testCreateDescriptorFromExampleTag()
56
    {
57
        $exampleTagMock = $this->givenExampleTagWithTestData();
58
        $this->whenExampleFileContains(self::EXAMPLE_TEXT);
59
60
        $descriptor = $this->fixture->create($exampleTagMock);
61
62
        $this->assertSame(self::TAG_NAME, $descriptor->getName());
63
        $this->assertSame(self::EXAMPLE_DESCRIPTION, $descriptor->getDescription());
64
        $this->assertSame(self::EXAMPLE_FILE_PATH, $descriptor->getFilePath());
65
        $this->assertSame(self::EXAMPLE_STARTING_LINE, $descriptor->getStartingLine());
66
        $this->assertSame(self::EXAMPLE_LINE_COUNT, $descriptor->getLineCount());
67
        $this->assertSame(self::EXAMPLE_TEXT, $descriptor->getExample());
68
    }
69
70
    /**
71
     * @covers \phpDocumentor\Descriptor\Builder\Reflector\Tags\ExampleAssembler::create
72
     * @expectedException InvalidArgumentException
73
     */
74
    public function testExceptionIsThrownIfTheWrongObjectIsPassed()
75
    {
76
        $this->fixture->create('this is an error');
77
    }
78
79
    /**
80
     * Returns a mock Example tag that will return example data (as provided in the class constants) when asked to.
81
     *
82
     * @return m\MockInterface
83
     */
84
    private function givenExampleTagWithTestData()
85
    {
86
        return new Example(
87
            self::EXAMPLE_FILE_PATH,
88
            false,
89
            self::EXAMPLE_STARTING_LINE,
90
            self::EXAMPLE_LINE_COUNT,
91
            self::EXAMPLE_DESCRIPTION
92
        );
93
    }
94
95
    /**
96
     * Instructs the finder dependency to return the given text when an example file is to be found.
97
     *
98
     * @param string $exampleText
99
     */
100
    private function whenExampleFileContains($exampleText)
101
    {
102
        $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...
103
    }
104
}
105