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

Builder/Reflector/PropertyAssemblerTest.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
 * 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-2018 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\Descriptor\Builder\Reflector;
14
15
use Mockery as m;
16
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
17
use phpDocumentor\Reflection\DocBlock;
18
use phpDocumentor\Reflection\DocBlock\Description;
19
use phpDocumentor\Reflection\Fqsen;
20
use phpDocumentor\Reflection\Php\Property;
21
use phpDocumentor\Reflection\Php\Visibility;
22
use phpDocumentor\Reflection\Types\String_;
23
24
class PropertyAssemblerTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
25
{
26
    /** @var PropertyAssembler $fixture */
27
    protected $fixture;
28
29
    /** @var ProjectDescriptorBuilder|m\MockInterface */
30
    protected $builderMock;
31
32
    /**
33
     * Creates a new fixture to test with.
34
     */
35
    protected function setUp()
36
    {
37
        $this->builderMock = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder');
38
        $this->builderMock->shouldReceive('buildDescriptor')->andReturn(null);
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...
39
40
        $this->fixture = new PropertyAssembler();
41
        $this->fixture->setBuilder($this->builderMock);
42
    }
43
44
    /**
45
     * @covers phpDocumentor\Descriptor\Builder\Reflector\PropertyAssembler::create
46
     */
47
    public function testCreatePropertyDescriptorFromReflector()
48
    {
49
        // Arrange
50
        $namespace = 'Namespace';
51
        $propertyName = 'property';
52
53
        $propertyReflectorMock = $this->givenAPropertyReflector(
54
            $namespace,
55
            $propertyName,
56
            $this->givenADocBlockObject(true)
57
        );
58
59
        // Act
60
        $descriptor = $this->fixture->create($propertyReflectorMock);
61
62
        // Assert
63
        $expectedFqsen = '\\' . $namespace . '::$' . $propertyName;
64
        $this->assertSame($expectedFqsen, (string) $descriptor->getFullyQualifiedStructuralElementName());
65
        $this->assertSame($propertyName, $descriptor->getName());
66
        $this->assertSame('\\' . $namespace, $descriptor->getNamespace());
67
        $this->assertSame('protected', (string) $descriptor->getVisibility());
68
        $this->assertFalse($descriptor->isStatic());
69
    }
70
71
    /**
72
     * Creates a sample property reflector for the tests with the given data.
73
     *
74
     * @param string $namespace
75
     * @param string $propertyName
76
     * @param DocBlock $docBlockMock
77
     */
78
    protected function givenAPropertyReflector($namespace, $propertyName, $docBlockMock = null): Property
79
    {
80
        return new Property(
81
            new Fqsen('\\' . $namespace . '::$' . $propertyName),
82
            new Visibility(Visibility::PROTECTED_),
83
            $docBlockMock
84
        );
85
    }
86
87
    /**
88
     * Generates a DocBlock object with applicable defaults for these tests.
89
     */
90
    protected function givenADocBlockObject($withTags): DocBlock
91
    {
92
        $docBlockDescription = new Description('This is an example description');
93
94
        $tags = [];
95
96
        if ($withTags) {
97
            $tags[] = new DocBlock\Tags\Var_('variableName', new String_(), new Description('Var description'));
98
        }
99
100
        return new DocBlock('This is a example description', $docBlockDescription, $tags);
101
    }
102
}
103