Function_Test   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testMatches() 0 5 1
A testCreateWithoutParameters() 0 17 1
A testCreateWithParameters() 0 25 1
A testReturnTypeResolving() 0 17 1
A testReturnTypeNullableResolving() 0 17 1
A testCreateWithDocBlock() 0 28 1
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\Argument;
18
use phpDocumentor\Reflection\Php\Function_ as FunctionDescriptor;
19
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
20
use phpDocumentor\Reflection\Php\StrategyContainer;
21
use phpDocumentor\Reflection\Types\Integer;
22
use phpDocumentor\Reflection\Types\Nullable;
23
use PhpParser\Comment\Doc;
24
use PhpParser\Node\NullableType;
25
26
/**
27
 * Test case for \phpDocumentor\Reflection\Php\Factory\Function_
28
 * @coversDefaultClass \phpDocumentor\Reflection\Php\Factory\Function_
29
 * @covers ::<!public>
30
 */
31
// @codingStandardsIgnoreStart
32
class Function_Test extends TestCase
33
// @codingStandardsIgnoreEnd
34
{
35
    protected function setUp()
36
    {
37
        $this->fixture = new Function_();
38
    }
39
40
    /**
41
     * @covers ::matches
42
     */
43
    public function testMatches()
44
    {
45
        $this->assertFalse($this->fixture->matches(new \stdClass()));
46
        $this->assertTrue($this->fixture->matches(m::mock(\PhpParser\Node\Stmt\Function_::class)));
47
    }
48
49
    /**
50
     * @covers ::create
51
     */
52
    public function testCreateWithoutParameters()
53
    {
54
        $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class);
55
        $functionMock->fqsen = new Fqsen('\SomeSpace::function()');
56
        $functionMock->params = [];
57
        $functionMock->shouldReceive('getDocComment')->andReturnNull();
58
        $functionMock->shouldReceive('getLine')->andReturn(1);
59
        $functionMock->shouldReceive('getReturnType')->andReturnNull();
60
61
        $containerMock = m::mock(StrategyContainer::class);
62
        $containerMock->shouldReceive('findMatching')->never();
63
64
        /** @var FunctionDescriptor $function */
65
        $function = $this->fixture->create($functionMock, $containerMock);
66
67
        $this->assertEquals('\SomeSpace::function()', (string) $function->getFqsen());
68
    }
69
70
    /**
71
     * @covers ::create
72
     */
73
    public function testCreateWithParameters()
74
    {
75
        $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class);
76
        $functionMock->fqsen = new Fqsen('\SomeSpace::function()');
77
        $functionMock->params = ['param1'];
78
        $functionMock->shouldReceive('getDocComment')->andReturnNull();
79
        $functionMock->shouldReceive('getLine')->andReturn(1);
80
        $functionMock->shouldReceive('getReturnType')->andReturnNull();
81
82
        $strategyMock = m::mock(ProjectFactoryStrategy::class);
83
        $containerMock = m::mock(StrategyContainer::class);
84
85
        $strategyMock->shouldReceive('create')
86
            ->with('param1', $containerMock, null)
87
            ->andReturn(new Argument('param1'));
88
89
        $containerMock->shouldReceive('findMatching')
90
            ->with('param1')
91
            ->andReturn($strategyMock);
92
93
        /** @var FunctionDescriptor $function */
94
        $function = $this->fixture->create($functionMock, $containerMock);
95
96
        $this->assertEquals('\SomeSpace::function()', (string) $function->getFqsen());
97
    }
98
99
    /**
100
     * @covers ::create
101
     */
102
    public function testReturnTypeResolving()
103
    {
104
        $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class);
105
        $functionMock->fqsen = new Fqsen('\SomeSpace::function()');
106
        $functionMock->params = [];
107
        $functionMock->shouldReceive('getDocComment')->andReturnNull();
108
        $functionMock->shouldReceive('getLine')->andReturn(1);
109
        $functionMock->shouldReceive('getReturnType')->times(3)->andReturn('int');
110
111
        $containerMock = m::mock(StrategyContainer::class);
112
        $containerMock->shouldReceive('findMatching')->never();
113
114
        /** @var FunctionDescriptor $function */
115
        $function = $this->fixture->create($functionMock, $containerMock);
116
117
        $this->assertEquals(new Integer(), $function->getReturnType());
118
    }
119
120
    /**
121
     * @covers ::create
122
     */
123
    public function testReturnTypeNullableResolving()
124
    {
125
        $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class);
126
        $functionMock->fqsen = new Fqsen('\SomeSpace::function()');
127
        $functionMock->params = [];
128
        $functionMock->shouldReceive('getDocComment')->andReturnNull();
129
        $functionMock->shouldReceive('getLine')->andReturn(1);
130
        $functionMock->shouldReceive('getReturnType')->times(3)->andReturn(new NullableType('int'));
131
132
        $containerMock = m::mock(StrategyContainer::class);
133
        $containerMock->shouldReceive('findMatching')->never();
134
135
        /** @var FunctionDescriptor $method */
136
        $function = $this->fixture->create($functionMock, $containerMock);
137
138
        $this->assertEquals(new Nullable(new Integer()), $function->getReturnType());
139
    }
140
141
    /**
142
     * @covers ::create
143
     */
144
    public function testCreateWithDocBlock()
145
    {
146
        $doc = m::mock(Doc::class);
147
        $functionMock = m::mock(\PhpParser\Node\Stmt\Function_::class);
148
        $functionMock->fqsen = new Fqsen('\SomeSpace::function()');
149
        $functionMock->params = [];
150
        $functionMock->shouldReceive('getDocComment')->andReturn($doc);
151
        $functionMock->shouldReceive('getLine')->andReturn(1);
152
        $functionMock->shouldReceive('getReturnType')->andReturnNull();
153
154
        $docBlock = new DocBlockDescriptor('');
155
        $strategyMock = m::mock(ProjectFactoryStrategy::class);
156
        $containerMock = m::mock(StrategyContainer::class);
157
158
        $strategyMock->shouldReceive('create')
159
            ->with($doc, $containerMock, null)
160
            ->andReturn($docBlock);
161
162
        $containerMock->shouldReceive('findMatching')
163
            ->with($doc)
164
            ->andReturn($strategyMock);
165
166
        /** @var FunctionDescriptor $function */
167
        $function = $this->fixture->create($functionMock, $containerMock);
168
169
        $this->assertEquals('\SomeSpace::function()', (string) $function->getFqsen());
170
        $this->assertSame($docBlock, $function->getDocBlock());
171
    }
172
}
173