PropertyTest::buildPropertyMock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.5
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\Reflection\Php\Factory;
13
14
use Mockery as m;
15
use phpDocumentor\Reflection\DocBlock as DocBlockDescriptor;
16
use phpDocumentor\Reflection\Fqsen;
17
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
18
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
19
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor;
20
use phpDocumentor\Reflection\Php\StrategyContainer;
21
use phpDocumentor\Reflection\PrettyPrinter;
22
use PhpParser\Comment\Doc;
23
use PhpParser\Node\Scalar\String_;
24
use PhpParser\Node\Stmt\Class_ as ClassNode;
25
use PhpParser\Node\Stmt\Property as PropertyNode;
26
use PhpParser\Node\Stmt\PropertyProperty;
27
28
/**
29
 * Class ArgumentTest
30
 * @coversDefaultClass \phpDocumentor\Reflection\Php\Factory\Property
31
 * @covers ::<!public>
32
 * @covers ::__construct
33
 */
34
class PropertyTest extends TestCase
35
{
36
    protected function setUp()
37
    {
38
        $this->fixture = new Property(new PrettyPrinter());
39
    }
40
41
    /**
42
     * @covers ::matches
43
     */
44
    public function testMatches()
45
    {
46
        $this->assertFalse($this->fixture->matches(new \stdClass()));
47
        $this->assertTrue($this->fixture->matches(new PropertyIterator(new PropertyNode(1, []))));
48
    }
49
50
    /**
51
     * @covers ::create
52
     */
53
    public function testPrivateCreate()
54
    {
55
        $factory = new ProjectFactoryStrategies([]);
56
57
        $propertyMock = $this->buildPropertyMock(ClassNode::MODIFIER_PRIVATE);
58
59
        /** @var PropertyDescriptor $property */
60
        $property = $this->fixture->create($propertyMock, $factory);
61
62
        $this->assertProperty($property, 'private');
63
    }
64
65
    /**
66
     * @covers ::create
67
     */
68
    public function testProtectedCreate()
69
    {
70
        $factory = new ProjectFactoryStrategies([]);
71
72
        $propertyMock = $this->buildPropertyMock(ClassNode::MODIFIER_PROTECTED);
73
74
        /** @var PropertyDescriptor $property */
75
        $property = $this->fixture->create($propertyMock, $factory);
76
77
        $this->assertProperty($property, 'protected');
78
    }
79
80
    /**
81
     * @covers ::create
82
     */
83
    public function testCreatePublic()
84
    {
85
        $factory = new ProjectFactoryStrategies([]);
86
87
        $propertyMock = $this->buildPropertyMock(ClassNode::MODIFIER_PUBLIC);
88
89
        /** @var PropertyDescriptor $property */
90
        $property = $this->fixture->create($propertyMock, $factory);
91
92
        $this->assertProperty($property, 'public');
93
    }
94
95
    /**
96
     * @covers ::create
97
     */
98
    public function testCreateWithDocBlock()
99
    {
100
        $doc = m::mock(Doc::class);
101
        $docBlock = new DocBlockDescriptor('');
102
103
        $property = new PropertyProperty('property', new String_('MyDefault'), ['comments' => [$doc]]);
104
        $property->fqsen = new Fqsen('\myClass::$property');
105
        $node = new PropertyNode(ClassNode::MODIFIER_PRIVATE | ClassNode::MODIFIER_STATIC, [$property]);
106
107
        $propertyMock = new PropertyIterator($node);
108
        $strategyMock = m::mock(ProjectFactoryStrategy::class);
109
        $containerMock = m::mock(StrategyContainer::class);
110
111
        $strategyMock->shouldReceive('create')
112
            ->with($doc, $containerMock, null)
113
            ->andReturn($docBlock);
114
115
        $containerMock->shouldReceive('findMatching')
116
            ->with($doc)
117
            ->andReturn($strategyMock);
118
119
        /** @var PropertyDescriptor $property */
120
        $property = $this->fixture->create($propertyMock, $containerMock);
121
122
        $this->assertProperty($property, 'private');
123
        $this->assertSame($docBlock, $property->getDocBlock());
124
    }
125
126
    /**
127
     * @return PropertyIterator
128
     */
129
    private function buildPropertyMock($modifier)
130
    {
131
        $property = new PropertyProperty('property', new String_('MyDefault'));
132
        $property->fqsen = new Fqsen('\myClass::$property');
133
        $propertyMock = new PropertyIterator(new PropertyNode($modifier | ClassNode::MODIFIER_STATIC, [$property]));
134
135
        return $propertyMock;
136
    }
137
138
    /**
139
     * @param PropertyDescriptor $property
140
     * @param string $visibility
141
     */
142
    private function assertProperty($property, $visibility)
143
    {
144
        $this->assertInstanceOf(PropertyDescriptor::class, $property);
145
        $this->assertEquals('\myClass::$property', (string) $property->getFqsen());
146
        $this->assertTrue($property->isStatic());
147
        $this->assertEquals('MyDefault', $property->getDefault());
148
        $this->assertEquals($visibility, (string) $property->getVisibility());
149
    }
150
}
151