Trait_Test::testCreateWithDocBlock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 24
rs 9.536
c 0
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-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\Reflection\Php\Factory;
14
15
use Mockery as m;
16
use phpDocumentor\Reflection\DocBlock as DocBlockElement;
17
use phpDocumentor\Reflection\Fqsen;
18
use phpDocumentor\Reflection\Php\Method as MethodElement;
19
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
20
use phpDocumentor\Reflection\Php\Property as PropertyElement;
21
use phpDocumentor\Reflection\Php\StrategyContainer;
22
use phpDocumentor\Reflection\Php\Trait_ as TraitElement;
23
use PhpParser\Comment\Doc;
24
use PhpParser\Node\Name;
25
use PhpParser\Node\Stmt\ClassMethod;
26
use PhpParser\Node\Stmt\Property as PropertyNode;
27
use PhpParser\Node\Stmt\PropertyProperty;
28
use PhpParser\Node\Stmt\Trait_ as TraitNode;
29
use PhpParser\Node\Stmt\TraitUse;
30
31
/**
32
 * Test case for Trait_
33
 * @coversDefaultClass phpDocumentor\Reflection\Php\Factory\Trait_
34
 * @covers ::<!public>
35
 */
36
// @codingStandardsIgnoreStart
37
class Trait_Test extends TestCase
38
// @codingStandardsIgnoreEnd
39
{
40
    protected function setUp()
41
    {
42
        $this->fixture = new Trait_();
43
    }
44
45
    /**
46
     * @covers ::matches
47
     */
48
    public function testMatches()
49
    {
50
        $this->assertFalse($this->fixture->matches(new \stdClass()));
51
        $this->assertTrue($this->fixture->matches(m::mock(TraitNode::class)));
52
    }
53
54
    /**
55
     * @covers ::create
56
     */
57
    public function testSimpleCreate()
58
    {
59
        $containerMock = m::mock(StrategyContainer::class);
60
        $interfaceMock = $this->buildTraitMock();
61
        $interfaceMock->shouldReceive('getDocComment')->andReturnNull();
62
63
        /** @var TraitElement $trait */
64
        $trait = $this->fixture->create($interfaceMock, $containerMock);
65
66
        $this->assertInstanceOf(TraitElement::class, $trait);
67
        $this->assertEquals('\Space\MyTrait', (string) $trait->getFqsen());
68
    }
69
70
    /**
71
     * @covers ::create
72
     */
73
    public function testCreateWithDocBlock()
74
    {
75
        $doc = m::mock(Doc::class);
76
        $interfaceMock = $this->buildTraitMock();
77
        $interfaceMock->shouldReceive('getDocComment')->andReturn($doc);
78
79
        $docBlock = new DocBlockElement('');
80
81
        $strategyMock = m::mock(ProjectFactoryStrategy::class);
82
        $containerMock = m::mock(StrategyContainer::class);
83
84
        $strategyMock->shouldReceive('create')
85
            ->with($doc, $containerMock, null)
86
            ->andReturn($docBlock);
87
88
        $containerMock->shouldReceive('findMatching')
89
            ->with($doc)
90
            ->andReturn($strategyMock);
91
92
        /** @var TraitElement $trait */
93
        $trait = $this->fixture->create($interfaceMock, $containerMock);
94
95
        $this->assertSame($docBlock, $trait->getDocBlock());
96
    }
97
98
    /**
99
     * @covers ::create
100
     */
101
    public function testWithPropertyMembers()
102
    {
103
        $propertyProperty = new PropertyProperty('\Space\MyTrait::$property');
104
        $property = new PropertyNode(1, [$propertyProperty]);
105
        $propertyDescriptor = new PropertyElement(new Fqsen('\Space\MyTrait::$property'));
106
        $strategyMock = m::mock(ProjectFactoryStrategy::class);
107
        $containerMock = m::mock(StrategyContainer::class);
108
        $traitMock = $this->buildTraitMock();
109
        $traitMock->shouldReceive('getDocComment')->andReturnNull();
110
        $traitMock->stmts = [
111
            $property,
112
        ];
113
114
        $strategyMock->shouldReceive('create')
115
            ->with(m::type(PropertyIterator::class), $containerMock, null)
116
            ->andReturn($propertyDescriptor);
117
118
        $containerMock->shouldReceive('findMatching')
119
            ->with(m::type(PropertyIterator::class))
120
            ->andReturn($strategyMock);
121
122
        /** @var TraitElement $trait */
123
        $trait = $this->fixture->create($traitMock, $containerMock);
124
125
        $this->assertInstanceOf(TraitElement::class, $trait);
126
        $this->assertEquals('\Space\MyTrait', (string) $trait->getFqsen());
127
        $this->assertEquals(
128
            ['\Space\MyTrait::$property' => $propertyDescriptor],
129
            $trait->getProperties()
130
        );
131
    }
132
133
    /**
134
     * @covers ::create
135
     */
136
    public function testWithMethodMembers()
137
    {
138
        $method1 = new ClassMethod('MyTrait::method1');
139
        $method1Descriptor = new MethodElement(new Fqsen('\MyTrait::method1'));
140
        $strategyMock = m::mock(ProjectFactoryStrategy::class);
141
        $containerMock = m::mock(StrategyContainer::class);
142
        $traitMock = $this->buildTraitMock();
143
        $traitMock->shouldReceive('getDocComment')->andReturnNull();
144
        $traitMock->stmts = [
145
            $method1,
146
        ];
147
148
        $strategyMock->shouldReceive('create')
149
            ->with($method1, $containerMock, null)
150
            ->andReturn($method1Descriptor);
151
152
        $containerMock->shouldReceive('findMatching')
153
            ->with($method1)
154
            ->andReturn($strategyMock);
155
156
        /** @var TraitElement $class */
157
        $class = $this->fixture->create($traitMock, $containerMock);
158
159
        $this->assertInstanceOf(TraitElement::class, $class);
160
        $this->assertEquals('\Space\MyTrait', (string) $class->getFqsen());
161
        $this->assertEquals(
162
            ['\MyTrait::method1' => $method1Descriptor],
163
            $class->getMethods()
164
        );
165
    }
166
167
    /**
168
     * @covers ::create
169
     */
170
    public function testWithUsedTraits()
171
    {
172
        $trait = new TraitUse([new Name('MyTrait')]);
173
        $containerMock = m::mock(StrategyContainer::class);
174
        $containerMock->shouldReceive('findMatching')->never();
175
        $traitMock = $this->buildTraitMock();
176
        $traitMock->shouldReceive('getDocComment')->andReturnNull();
177
        $traitMock->stmts = [
178
            $trait,
179
        ];
180
181
        /** @var TraitElement $trait */
182
        $trait = $this->fixture->create($traitMock, $containerMock);
183
184
        $this->assertEquals(
185
            [
186
                '\MyTrait' => new Fqsen('\MyTrait'),
187
            ],
188
            $trait->getUsedTraits()
189
        );
190
    }
191
192
    /**
193
     * @return m\MockInterface|TraitNode
194
     */
195
    private function buildTraitMock()
196
    {
197
        $mock = m::mock(TraitNode::class);
198
        $mock->fqsen = new Fqsen('\Space\MyTrait');
199
        $mock->shouldReceive('getLine')->andReturn(1);
200
        return $mock;
201
    }
202
}
203