Test Setup Failed
Push — test ( 24878d...6ecee8 )
by Jonathan
02:38
created

MethodObjectTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 275
Duplicated Lines 8 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 22
loc 275
rs 10
c 1
b 0
f 0
wmc 17
lcom 1
cbo 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function testGetParamsPhp7()
217
    {
218
        if (!KINT_PHP70) {
219
            $this->markTestSkipped('Not testing PHP7+ parameter type hints on PHP5');
220
221
            return;
222
        }
223
224
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\Php7TestClass', 'typeHints'));
225
        $this->assertEquals('string $p1, int $p2, bool $p3 = false', $m->getParams());
226
    }
227
228
    /**
229
     * @covers \Kint\Object\MethodObject::__construct
230
     */
231
    public function testReturnType()
232
    {
233
        if (!KINT_PHP70) {
234
            $this->markTestSkipped('Not testing PHP7+ return type hints on PHP5');
235
236
            return;
237
        }
238
239
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\Php7TestClass', 'typeHints'));
240
        $this->assertEquals('self', $m->returntype);
241
    }
242
243
    /**
244
     * @covers \Kint\Object\MethodObject::getPhpDocUrl
245
     */
246
    public function testGetPhpDocUrl()
247
    {
248
        $m = new MethodObject(new ReflectionMethod('ReflectionMethod', '__construct'));
249
        $this->assertEquals(
250
            'https://secure.php.net/reflectionmethod.construct',
251
            $m->getPhpDocUrl()
252
        );
253
    }
254
255
    /**
256
     * @covers \Kint\Object\MethodObject::getPhpDocUrl
257
     */
258
    public function testGetPhpDocUrlParent()
259
    {
260
        $m = new MethodObject(new ReflectionMethod('ReflectionMethod', '__clone'));
261
        $this->assertEquals(
262
            'https://secure.php.net/reflectionfunctionabstract.clone',
263
            $m->getPhpDocUrl()
264
        );
265
    }
266
267
    /**
268
     * @covers \Kint\Object\MethodObject::getPhpDocUrl
269
     */
270
    public function testGetPhpDocUrlUserDefined()
271
    {
272
        $m = new MethodObject(new ReflectionMethod(__CLASS__, __FUNCTION__));
273
        $this->assertNull($m->getPhpDocUrl());
274
    }
275
276
    /**
277
     * @covers \Kint\Object\MethodObject::getPhpDocUrl
278
     */
279
    public function testGetPhpDocUrlFunction()
280
    {
281
        $m = new MethodObject(new ReflectionFunction('explode'));
282
        $this->assertEquals(
283
            'https://secure.php.net/function.explode',
284
            $m->getPhpDocUrl()
285
        );
286
    }
287
}
288