Completed
Push — develop ( 28aa0b...3fae3c )
by Jaap
13s
created

Trait_Test   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
dl 0
loc 151
rs 9.1666
c 0
b 0
f 0
wmc 8
lcom 1
cbo 15

8 Methods

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