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 as ArgumentDescriptor; |
18
|
|
|
use phpDocumentor\Reflection\Php\Method as MethodDescriptor; |
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
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Test case for \phpDocumentor\Reflection\Php\Factory\Method |
29
|
|
|
* @coversDefaultClass \phpDocumentor\Reflection\Php\Factory\Method |
30
|
|
|
* @covers ::<!public> |
31
|
|
|
*/ |
32
|
|
|
class MethodTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
protected function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->fixture = new Method(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @covers ::matches |
41
|
|
|
*/ |
42
|
|
|
public function testMatches() |
43
|
|
|
{ |
44
|
|
|
$this->assertFalse($this->fixture->matches(new \stdClass())); |
45
|
|
|
$this->assertTrue($this->fixture->matches(m::mock(ClassMethod::class))); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @covers ::create |
50
|
|
|
*/ |
51
|
|
|
public function testCreateWithoutParameters() |
52
|
|
|
{ |
53
|
|
|
$classMethodMock = $this->buildClassMethodMock(); |
54
|
|
|
$classMethodMock->params = []; |
55
|
|
|
$classMethodMock->shouldReceive('isPrivate')->once()->andReturn(false); |
56
|
|
|
$classMethodMock->shouldReceive('isProtected')->once()->andReturn(false); |
57
|
|
|
$classMethodMock->shouldReceive('getDocComment')->once()->andReturnNull(); |
58
|
|
|
$classMethodMock->shouldReceive('getReturnType')->once()->andReturn(null); |
59
|
|
|
|
60
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
61
|
|
|
$containerMock->shouldReceive('findMatching')->never(); |
62
|
|
|
|
63
|
|
|
/** @var MethodDescriptor $method */ |
64
|
|
|
$method = $this->fixture->create($classMethodMock, $containerMock); |
65
|
|
|
|
66
|
|
|
$this->assertEquals('\SomeSpace\Class::function()', (string) $method->getFqsen()); |
67
|
|
|
$this->assertEquals('public', (string) $method->getVisibility()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testCreateProtectedMethod() |
71
|
|
|
{ |
72
|
|
|
$classMethodMock = $this->buildClassMethodMock(); |
73
|
|
|
$classMethodMock->params = []; |
74
|
|
|
$classMethodMock->shouldReceive('isPrivate')->once()->andReturn(false); |
75
|
|
|
$classMethodMock->shouldReceive('isProtected')->once()->andReturn(true); |
76
|
|
|
$classMethodMock->shouldReceive('getDocComment')->once()->andReturnNull(); |
77
|
|
|
$classMethodMock->shouldReceive('getReturnType')->once()->andReturn(null); |
78
|
|
|
|
79
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
80
|
|
|
$containerMock->shouldReceive('findMatching')->never(); |
81
|
|
|
|
82
|
|
|
/** @var MethodDescriptor $method */ |
83
|
|
|
$method = $this->fixture->create($classMethodMock, $containerMock); |
84
|
|
|
|
85
|
|
|
$this->assertEquals('\SomeSpace\Class::function()', (string) $method->getFqsen()); |
86
|
|
|
$this->assertEquals('protected', (string) $method->getVisibility()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @covers ::create |
91
|
|
|
*/ |
92
|
|
|
public function testCreateWithParameters() |
93
|
|
|
{ |
94
|
|
|
$classMethodMock = $this->buildClassMethodMock(); |
95
|
|
|
$classMethodMock->params = ['param1']; |
96
|
|
|
$classMethodMock->shouldReceive('isPrivate')->once()->andReturn(true); |
97
|
|
|
$classMethodMock->shouldReceive('getDocComment')->once()->andReturnNull(); |
98
|
|
|
$classMethodMock->shouldReceive('getReturnType')->once()->andReturn(null); |
99
|
|
|
|
100
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
101
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
102
|
|
|
|
103
|
|
|
$strategyMock->shouldReceive('create') |
104
|
|
|
->with('param1', $containerMock, null) |
105
|
|
|
->andReturn(new ArgumentDescriptor('param1')); |
106
|
|
|
|
107
|
|
|
$containerMock->shouldReceive('findMatching') |
108
|
|
|
->with('param1') |
109
|
|
|
->andReturn($strategyMock); |
110
|
|
|
|
111
|
|
|
/** @var MethodDescriptor $method */ |
112
|
|
|
$method = $this->fixture->create($classMethodMock, $containerMock); |
113
|
|
|
|
114
|
|
|
$this->assertEquals('\SomeSpace\Class::function()', (string) $method->getFqsen()); |
115
|
|
|
$this->assertTrue($method->isAbstract()); |
116
|
|
|
$this->assertTrue($method->isFinal()); |
117
|
|
|
$this->assertTrue($method->isStatic()); |
118
|
|
|
$this->assertEquals('private', (string) $method->getVisibility()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @covers ::create |
123
|
|
|
*/ |
124
|
|
|
public function testReturnTypeResolving() |
125
|
|
|
{ |
126
|
|
|
$classMethodMock = $this->buildClassMethodMock(); |
127
|
|
|
$classMethodMock->params = []; |
128
|
|
|
$classMethodMock->shouldReceive('isPrivate')->once()->andReturn(true); |
129
|
|
|
$classMethodMock->shouldReceive('getDocComment')->once()->andReturnNull(); |
130
|
|
|
$classMethodMock->shouldReceive('getReturnType')->times(3)->andReturn('int'); |
131
|
|
|
|
132
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
133
|
|
|
$containerMock->shouldReceive('findMatching')->never(); |
134
|
|
|
|
135
|
|
|
/** @var MethodDescriptor $method */ |
136
|
|
|
$method = $this->fixture->create($classMethodMock, $containerMock); |
137
|
|
|
|
138
|
|
|
$this->assertEquals(new Integer(), $method->getReturnType()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @covers ::create |
143
|
|
|
*/ |
144
|
|
|
public function testReturnTypeNullableResolving() |
145
|
|
|
{ |
146
|
|
|
$classMethodMock = $this->buildClassMethodMock(); |
147
|
|
|
$classMethodMock->params = []; |
148
|
|
|
$classMethodMock->shouldReceive('isPrivate')->once()->andReturn(true); |
149
|
|
|
$classMethodMock->shouldReceive('getDocComment')->once()->andReturnNull(); |
150
|
|
|
$classMethodMock->shouldReceive('getReturnType')->times(3)->andReturn(new NullableType('int')); |
151
|
|
|
|
152
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
153
|
|
|
$containerMock->shouldReceive('findMatching')->never(); |
154
|
|
|
|
155
|
|
|
/** @var MethodDescriptor $method */ |
156
|
|
|
$method = $this->fixture->create($classMethodMock, $containerMock); |
157
|
|
|
|
158
|
|
|
$this->assertEquals(new Nullable(new Integer()), $method->getReturnType()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @covers ::create |
163
|
|
|
*/ |
164
|
|
|
public function testCreateWithDocBlock() |
165
|
|
|
{ |
166
|
|
|
$doc = m::mock(Doc::class); |
167
|
|
|
$classMethodMock = $this->buildClassMethodMock(); |
168
|
|
|
$classMethodMock->params = []; |
169
|
|
|
$classMethodMock->shouldReceive('isPrivate')->once()->andReturn(true); |
170
|
|
|
$classMethodMock->shouldReceive('getDocComment')->andReturn($doc); |
171
|
|
|
$classMethodMock->shouldReceive('getReturnType')->once()->andReturn(null); |
172
|
|
|
|
173
|
|
|
$docBlock = new DocBlockDescriptor(''); |
174
|
|
|
$strategyMock = m::mock(ProjectFactoryStrategy::class); |
175
|
|
|
$containerMock = m::mock(StrategyContainer::class); |
176
|
|
|
|
177
|
|
|
$strategyMock->shouldReceive('create') |
178
|
|
|
->with($doc, $containerMock, null) |
179
|
|
|
->andReturn($docBlock); |
180
|
|
|
|
181
|
|
|
$containerMock->shouldReceive('findMatching') |
182
|
|
|
->with($doc) |
183
|
|
|
->andReturn($strategyMock); |
184
|
|
|
|
185
|
|
|
/** @var MethodDescriptor $method */ |
186
|
|
|
$method = $this->fixture->create($classMethodMock, $containerMock); |
187
|
|
|
|
188
|
|
|
$this->assertEquals('\SomeSpace\Class::function()', (string) $method->getFqsen()); |
189
|
|
|
$this->assertSame($docBlock, $method->getDocBlock()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function buildClassMethodMock() |
193
|
|
|
{ |
194
|
|
|
$methodMock = m::mock(ClassMethod::class); |
195
|
|
|
$methodMock->name = 'function'; |
196
|
|
|
$methodMock->fqsen = new Fqsen('\SomeSpace\Class::function()'); |
197
|
|
|
|
198
|
|
|
$methodMock->shouldReceive('isStatic')->once()->andReturn(true); |
199
|
|
|
$methodMock->shouldReceive('isFinal')->once()->andReturn(true); |
200
|
|
|
$methodMock->shouldReceive('isAbstract')->once()->andReturn(true); |
201
|
|
|
$methodMock->shouldReceive('getLine')->once()->andReturn(1); |
202
|
|
|
|
203
|
|
|
return $methodMock; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|