Completed
Push — develop ( 8b2e58...a5af85 )
by Jaap
09:43
created

testCreatePropertyDescriptorFromReflector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 1
b 0
f 0
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-2017 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 phpDocumentor\Descriptor\ProjectDescriptorBuilder;
16
use phpDocumentor\Reflection\DocBlock;
17
use phpDocumentor\Reflection\ClassReflector\PropertyReflector;
18
use Mockery as m;
19
20
class PropertyAssemblerTest extends \PHPUnit_Framework_TestCase
21
{
22
    /** @var PropertyAssembler $fixture */
23
    protected $fixture;
24
25
    /** @var ProjectDescriptorBuilder|m\MockInterface */
26
    protected $builderMock;
27
28
    /**
29
     * Creates a new fixture to test with.
30
     */
31
    protected function setUp()
32
    {
33
        $this->builderMock = m::mock('phpDocumentor\Descriptor\ProjectDescriptorBuilder');
34
        $this->builderMock->shouldReceive('buildDescriptor')->andReturn(null);
35
36
        $this->fixture = new PropertyAssembler();
37
        $this->fixture->setBuilder($this->builderMock);
38
    }
39
40
    /**
41
     * @covers phpDocumentor\Descriptor\Builder\Reflector\PropertyAssembler::create
42
     */
43
    public function testCreatePropertyDescriptorFromReflector()
44
    {
45
        // Arrange
46
        $namespace    = 'Namespace';
47
        $propertyName   = 'property';
48
49
        $propertyReflectorMock = $this->givenAPropertyReflector(
50
            $namespace,
51
            $propertyName,
52
            $this->givenADocBlockObject(true)
53
        );
54
55
        // Act
56
        $descriptor = $this->fixture->create($propertyReflectorMock);
0 ignored issues
show
Bug introduced by
It seems like $propertyReflectorMock defined by $this->givenAPropertyRef...nADocBlockObject(true)) on line 49 can also be of type object<Mockery\MockInterface>; however, phpDocumentor\Descriptor...ertyAssembler::create() does only seem to accept object<phpDocumentor\Ref...ctor\PropertyReflector>, 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...
57
58
        // Assert
59
        $expectedFqsen = $namespace . '\\$' . $propertyName;
60
        $this->assertSame($expectedFqsen, $descriptor->getFullyQualifiedStructuralElementName());
61
        $this->assertSame($propertyName, $descriptor->getName());
62
        $this->assertSame('\\' . $namespace, $descriptor->getNamespace());
63
        $this->assertSame('protected', $descriptor->getVisibility());
64
        $this->assertSame(false, $descriptor->isStatic());
65
    }
66
67
    /**
68
     * Creates a sample property reflector for the tests with the given data.
69
     *
70
     * @param string                             $namespace
71
     * @param string                             $propertyName
72
     * @param DocBlock|m\MockInterface           $docBlockMock
73
     *
74
     * @return PropertyReflector|m\MockInterface
75
     */
76
    protected function givenAPropertyReflector($namespace, $propertyName, $docBlockMock = null)
77
    {
78
        $propertyReflectorMock = m::mock('phpDocumentor\Reflection\PropertyReflector');
79
        $propertyReflectorMock->shouldReceive('getName')->andReturn($namespace . '\\$' . $propertyName);
80
        $propertyReflectorMock->shouldReceive('getShortName')->andReturn($propertyName);
81
        $propertyReflectorMock->shouldReceive('getNamespace')->andReturn($namespace);
82
        $propertyReflectorMock->shouldReceive('getDocBlock')->andReturn($docBlockMock);
83
        $propertyReflectorMock->shouldReceive('getLinenumber')->andReturn(128);
84
        $propertyReflectorMock->shouldReceive('getVisibility')->andReturn('protected');
85
        $propertyReflectorMock->shouldReceive('getDefault')->andReturn(null);
86
        $propertyReflectorMock->shouldReceive('isStatic')->andReturn(false);
87
88
        return $propertyReflectorMock;
89
    }
90
91
    /**
92
     * Generates a DocBlock object with applicable defaults for these tests.
93
     *
94
     * @return DocBlock|m\MockInterface
95
     */
96
    protected function givenADocBlockObject($withTags)
97
    {
98
        $docBlockDescription = new DocBlock\Description('This is an example description');
99
100
        $docBlockMock = m::mock('phpDocumentor\Reflection\DocBlock');
101
        $docBlockMock->shouldReceive('getTags')->andReturn(array());
102
        $docBlockMock->shouldReceive('getShortDescription')->andReturn('This is a example description');
103
        $docBlockMock->shouldReceive('getLongDescription')->andReturn($docBlockDescription);
104
105
        if ($withTags) {
106
            $docBlockMock->shouldReceive('getTagsByName')->andReturnUsing(function ($param) {
0 ignored issues
show
Unused Code introduced by
The parameter $param is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
                $tag = m::mock('phpDocumentor\Reflection\DocBlock\Tag');
108
109
                $tag->shouldReceive('isVariadic')->once()->andReturn(true);
110
                $tag->shouldReceive('getVariableName')->andReturn('variableName');
111
                $tag->shouldReceive('getTypes')->andReturn(array());
112
                $tag->shouldReceive('getDescription');
113
114
                return array($tag);
115
            });
116
        } else {
117
            $docBlockMock->shouldReceive('getTagsByName')->andReturn(array());
118
        }
119
120
        return $docBlockMock;
121
    }
122
}
123