1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kint\Test\Object; |
4
|
|
|
|
5
|
|
|
use Kint\Object\BasicObject; |
6
|
|
|
use Kint\Object\InstanceObject; |
7
|
|
|
use Kint\Object\MethodObject; |
8
|
|
|
use PHPUnit_Framework_TestCase; |
9
|
|
|
use ReflectionFunction; |
10
|
|
|
use ReflectionMethod; |
11
|
|
|
use stdClass; |
12
|
|
|
|
13
|
|
|
class MethodObjectTest extends PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @covers \Kint\Object\MethodObject::__construct |
17
|
|
|
*/ |
18
|
|
|
public function testConstruct() |
19
|
|
|
{ |
20
|
|
|
$reflection = new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'mix'); |
21
|
|
|
$m = new MethodObject($reflection); |
|
|
|
|
22
|
|
|
$this->assertEquals('mix', $m->name); |
23
|
|
|
$this->assertEquals($reflection->getFileName(), $m->filename); |
24
|
|
|
$this->assertEquals($reflection->getStartLine(), $m->startline); |
25
|
|
|
$this->assertEquals($reflection->getEndLine(), $m->endline); |
26
|
|
|
$this->assertEquals(false, $m->internal); |
27
|
|
|
$this->assertEquals(true, $m->return_reference); |
28
|
|
|
$this->assertEquals($reflection->getDocComment(), $m->docstring); |
29
|
|
|
$this->assertEquals(BasicObject::OPERATOR_STATIC, $m->operator); |
30
|
|
|
$this->assertEquals(BasicObject::ACCESS_PROTECTED, $m->access); |
31
|
|
|
$this->assertEquals('Kint\\Test\\Fixtures\\TestClass', $m->owner_class); |
32
|
|
|
$this->assertTrue($m->static); |
33
|
|
|
$this->assertTrue($m->final); |
34
|
|
|
$this->assertFalse($m->abstract); |
35
|
|
|
$this->assertFalse($m->internal); |
36
|
|
|
|
37
|
|
|
$reflection = new ReflectionMethod('Kint\\Test\\Fixtures\\ChildTestClass', '__construct'); |
38
|
|
|
$parent_reflection = new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__construct'); |
|
|
|
|
39
|
|
|
$m = new MethodObject($reflection); |
40
|
|
|
$this->assertEquals($parent_reflection->getDocComment(), $m->docstring); |
|
|
|
|
41
|
|
|
$this->assertEquals(BasicObject::OPERATOR_OBJECT, $m->operator); |
42
|
|
|
$this->assertEquals(BasicObject::ACCESS_PUBLIC, $m->access); |
43
|
|
|
$this->assertEquals('Kint\\Test\\Fixtures\\TestClass', $m->owner_class); |
44
|
|
|
|
45
|
|
|
$reflection = new ReflectionMethod('Kint\\Test\\Fixtures\\ChildTestClass', 'classHint'); |
46
|
|
|
$m = new MethodObject($reflection); |
47
|
|
|
$this->assertEquals(BasicObject::OPERATOR_OBJECT, $m->operator); |
48
|
|
|
$this->assertEquals(BasicObject::ACCESS_PRIVATE, $m->access); |
49
|
|
|
$this->assertEquals('Kint\\Test\\Fixtures\\TestClass', $m->owner_class); |
50
|
|
|
|
51
|
|
|
$reflection = new ReflectionFunction('explode'); |
52
|
|
|
$m = new MethodObject($reflection); |
53
|
|
|
$this->assertTrue($m->internal); |
54
|
|
|
$this->assertEquals(BasicObject::OPERATOR_NONE, $m->operator); |
55
|
|
|
$this->assertEquals(BasicObject::ACCESS_NONE, $m->access); |
56
|
|
|
$this->assertEquals(null, $m->owner_class); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @covers \Kint\Object\MethodObject::__construct |
61
|
|
|
*/ |
62
|
|
|
public function testConstructWrongType() |
63
|
|
|
{ |
64
|
|
|
if (KINT_PHP70) { |
65
|
|
|
$this->setExpectedException('TypeError'); |
66
|
|
|
} else { |
67
|
|
|
$this->setExpectedException('PHPUnit_Framework_Error'); |
68
|
|
|
} |
69
|
|
|
$m = new MethodObject(new stdClass()); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @covers \Kint\Object\MethodObject::setAccessPathFrom |
74
|
|
|
* @covers \Kint\Object\MethodObject::getAccessPath |
75
|
|
|
*/ |
76
|
|
|
public function testSetAccessPathFrom() |
77
|
|
|
{ |
78
|
|
|
$o = BasicObject::blank('$tc'); |
|
|
|
|
79
|
|
|
$o = $o->transplant(new InstanceObject()); |
80
|
|
|
$o->classname = 'Kint\\Test\\Fixtures\\TestClass'; |
|
|
|
|
81
|
|
|
|
82
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__construct')); |
|
|
|
|
83
|
|
|
$this->assertNull($m->getAccessPath()); |
84
|
|
|
$m->setAccessPathFrom($o); |
|
|
|
|
85
|
|
|
$this->assertEquals('new \\Kint\\Test\\Fixtures\\TestClass()', $m->getAccessPath()); |
86
|
|
|
|
87
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'staticMethod')); |
88
|
|
|
$this->assertNull($m->getAccessPath()); |
89
|
|
|
$m->setAccessPathFrom($o); |
|
|
|
|
90
|
|
|
$this->assertEquals('\\Kint\\Test\\Fixtures\\TestClass::staticMethod()', $m->getAccessPath()); |
91
|
|
|
|
92
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'finalMethod')); |
93
|
|
|
$this->assertNull($m->getAccessPath()); |
94
|
|
|
$m->setAccessPathFrom($o); |
|
|
|
|
95
|
|
|
$this->assertEquals('$tc->finalMethod()', $m->getAccessPath()); |
96
|
|
|
|
97
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'mix')); |
98
|
|
|
$this->assertNull($m->getAccessPath()); |
99
|
|
|
$m->setAccessPathFrom($o); |
|
|
|
|
100
|
|
|
$this->assertEquals( |
101
|
|
|
'\\Kint\\Test\\Fixtures\\TestClass::mix(array &$x, Kint\\Test\\Fixtures\\TestClass $y = null, $z = array(...), $_ = \'string\')', |
102
|
|
|
$m->getAccessPath() |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__clone')); |
106
|
|
|
$this->assertNull($m->getAccessPath()); |
107
|
|
|
$m->setAccessPathFrom($o); |
|
|
|
|
108
|
|
|
$this->assertEquals('clone $tc', $m->getAccessPath()); |
109
|
|
|
|
110
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__invoke')); |
111
|
|
|
$this->assertNull($m->getAccessPath()); |
112
|
|
|
$m->setAccessPathFrom($o); |
|
|
|
|
113
|
|
|
$this->assertEquals('$tc($x)', $m->getAccessPath()); |
114
|
|
|
|
115
|
|
|
// Tests both tostring and case insensitivity |
116
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__tostring')); |
117
|
|
|
$this->assertNull($m->getAccessPath()); |
118
|
|
|
$m->setAccessPathFrom($o); |
|
|
|
|
119
|
|
|
$this->assertEquals('__ToStRiNg', $m->name); |
120
|
|
|
$this->assertEquals('(string) $tc', $m->getAccessPath()); |
121
|
|
|
|
122
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__get')); |
123
|
|
|
$this->assertNull($m->getAccessPath()); |
124
|
|
|
$m->setAccessPathFrom($o); |
|
|
|
|
125
|
|
|
$this->assertNull($m->getAccessPath()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @covers \Kint\Object\MethodObject::getValueShort |
130
|
|
|
*/ |
131
|
|
|
public function testGetValueShort() |
132
|
|
|
{ |
133
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__construct')); |
|
|
|
|
134
|
|
|
$this->assertEquals( |
135
|
|
|
'This is a constructor for a TestClass with the first line of the docstring split into two different lines.', |
136
|
|
|
$m->getValueShort() |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$m = new MethodObject(new ReflectionFunction('explode')); |
140
|
|
|
$this->assertNull($m->getValueShort()); |
141
|
|
|
|
142
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'arrayHint')); |
143
|
|
|
$this->assertNull($m->getValueShort()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @covers \Kint\Object\MethodObject::getModifiers |
148
|
|
|
*/ |
149
|
|
|
public function testGetModifiers() |
150
|
|
|
{ |
151
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'staticMethod')); |
|
|
|
|
152
|
|
|
$this->assertEquals('private static', $m->getModifiers()); |
153
|
|
|
|
154
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'finalMethod')); |
155
|
|
|
$this->assertEquals('final public', $m->getModifiers()); |
156
|
|
|
|
157
|
|
|
$m = new MethodObject(new ReflectionMethod('ReflectionFunctionAbstract', '__toString')); |
158
|
|
|
$this->assertEquals('abstract public', $m->getModifiers()); |
159
|
|
|
|
160
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'mix')); |
161
|
|
|
$this->assertEquals('final protected static', $m->getModifiers()); |
162
|
|
|
|
163
|
|
|
$m = new MethodObject(new ReflectionFunction('explode')); |
164
|
|
|
$this->assertNull($m->getModifiers()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @covers \Kint\Object\MethodObject::getAccessPath |
169
|
|
|
*/ |
170
|
|
|
public function testGetAccessPath() |
171
|
|
|
{ |
172
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'arrayHint')); |
|
|
|
|
173
|
|
|
$this->assertNull($m->getAccessPath()); |
174
|
|
|
$m->access_path = '$m->arrayHint'; |
175
|
|
|
$this->assertEquals('$m->arrayHint(array $x)', $m->getAccessPath()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @covers \Kint\Object\MethodObject::getParams |
180
|
|
|
*/ |
181
|
|
|
public function testGetParams() |
182
|
|
|
{ |
183
|
|
|
$m = new MethodObject(new ReflectionFunction('explode')); |
|
|
|
|
184
|
|
|
if (defined('HHVM_VERSION')) { |
185
|
|
|
$this->assertStringStartsWith('HH\\string $delimiter, HH\\string $str, HH\\int $limit = ', $m->getParams()); |
186
|
|
|
} else { |
187
|
|
|
$this->assertEquals('$separator, $str, $limit', $m->getParams()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'arrayHint')); |
191
|
|
|
$this->assertEquals('array $x', $m->getParams()); |
192
|
|
|
|
193
|
|
|
// Testing cache |
194
|
|
|
$m->parameters = array(); |
195
|
|
|
$this->assertEquals('array $x', $m->getParams()); |
196
|
|
|
|
197
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'classHint')); |
198
|
|
|
$this->assertEquals('Kint\\Test\\Fixtures\\TestClass $x', $m->getParams()); |
199
|
|
|
|
200
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'ref')); |
201
|
|
|
$this->assertEquals('&$x', $m->getParams()); |
202
|
|
|
|
203
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'defaultMethod')); |
204
|
|
|
$this->assertEquals('$x = 1234', $m->getParams()); |
205
|
|
|
|
206
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'mix')); |
207
|
|
|
$this->assertEquals( |
208
|
|
|
'array &$x, Kint\\Test\\Fixtures\\TestClass $y = null, $z = array(...), $_ = \'string\'', |
209
|
|
|
$m->getParams() |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @covers \Kint\Object\MethodObject::getParams |
215
|
|
|
*/ |
216
|
|
View Code Duplication |
public function testGetParamsPhp7() |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
if (!KINT_PHP70) { |
219
|
|
|
$this->markTestSkipped('Not testing PHP7+ parameter type hints on PHP5'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\Php7TestClass', 'typeHints')); |
|
|
|
|
223
|
|
|
$this->assertEquals('string $p1, int $p2, bool $p3 = false', $m->getParams()); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @covers \Kint\Object\MethodObject::__construct |
228
|
|
|
*/ |
229
|
|
View Code Duplication |
public function testReturnType() |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
if (!KINT_PHP70) { |
232
|
|
|
$this->markTestSkipped('Not testing PHP7+ return type hints on PHP5'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\Php7TestClass', 'typeHints')); |
|
|
|
|
236
|
|
|
$this->assertEquals('self', $m->returntype); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @covers \Kint\Object\MethodObject::getPhpDocUrl |
241
|
|
|
*/ |
242
|
|
|
public function testGetPhpDocUrl() |
243
|
|
|
{ |
244
|
|
|
$m = new MethodObject(new ReflectionMethod('ReflectionMethod', '__construct')); |
|
|
|
|
245
|
|
|
$this->assertEquals( |
246
|
|
|
'https://secure.php.net/reflectionmethod.construct', |
247
|
|
|
$m->getPhpDocUrl() |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @covers \Kint\Object\MethodObject::getPhpDocUrl |
253
|
|
|
*/ |
254
|
|
|
public function testGetPhpDocUrlParent() |
255
|
|
|
{ |
256
|
|
|
$m = new MethodObject(new ReflectionMethod('ReflectionMethod', '__clone')); |
|
|
|
|
257
|
|
|
$this->assertEquals( |
258
|
|
|
'https://secure.php.net/reflectionfunctionabstract.clone', |
259
|
|
|
$m->getPhpDocUrl() |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @covers \Kint\Object\MethodObject::getPhpDocUrl |
265
|
|
|
*/ |
266
|
|
|
public function testGetPhpDocUrlUserDefined() |
267
|
|
|
{ |
268
|
|
|
$m = new MethodObject(new ReflectionMethod(__CLASS__, __FUNCTION__)); |
|
|
|
|
269
|
|
|
$this->assertNull($m->getPhpDocUrl()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @covers \Kint\Object\MethodObject::getPhpDocUrl |
274
|
|
|
*/ |
275
|
|
|
public function testGetPhpDocUrlFunction() |
276
|
|
|
{ |
277
|
|
|
$m = new MethodObject(new ReflectionFunction('explode')); |
|
|
|
|
278
|
|
|
$this->assertEquals( |
279
|
|
|
'https://secure.php.net/function.explode', |
280
|
|
|
$m->getPhpDocUrl() |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.