Completed
Push — master ( 2f25cf...59cc7f )
by Marco
43:54 queued 18:55
created

testBodyStructureWithTypedProperties()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 162

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 162
c 0
b 0
f 0
rs 8
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManagerTest\ProxyGenerator\LazyLoadingGhost\MethodGenerator;
6
7
use PHPUnit\Framework\TestCase;
8
use PHPUnit_Framework_MockObject_MockObject;
9
use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer;
10
use ProxyManager\ProxyGenerator\Util\Properties;
11
use ProxyManagerTestAsset\ClassWithMixedProperties;
12
use ProxyManagerTestAsset\ClassWithMixedTypedProperties;
13
use ReflectionClass;
14
use Zend\Code\Generator\PropertyGenerator;
15
16
/**
17
 * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer}
18
 *
19
 * @group Coverage
20
 * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer
21
 */
22
class CallInitializerTest extends TestCase
23
{
24
    public function testBodyStructure() : void
25
    {
26
        /** @var PropertyGenerator|PHPUnit_Framework_MockObject_MockObject $initializer */
27
        $initializer = $this->createMock(PropertyGenerator::class);
28
        /** @var PropertyGenerator|PHPUnit_Framework_MockObject_MockObject $initializationTracker */
29
        $initializationTracker = $this->createMock(PropertyGenerator::class);
30
31
        $initializer->expects(self::any())->method('getName')->will(self::returnValue('init'));
32
        $initializationTracker->expects(self::any())->method('getName')->will(self::returnValue('track'));
33
34
        $callInitializer = new CallInitializer(
35
            $initializer,
36
            $initializationTracker,
37
            Properties::fromReflectionClass(new ReflectionClass(ClassWithMixedProperties::class))
38
        );
39
40
        $expectedCode = 'if ($this->track || ! $this->init) {
41
    return;
42
}
43
44
$this->track = true;
45
46
$this->publicProperty0 = \'publicProperty0\';
47
$this->publicProperty1 = \'publicProperty1\';
48
$this->publicProperty2 = \'publicProperty2\';
49
$this->protectedProperty0 = \'protectedProperty0\';
50
$this->protectedProperty1 = \'protectedProperty1\';
51
$this->protectedProperty2 = \'protectedProperty2\';
52
static $cacheProxyManagerTestAsset_ClassWithMixedProperties;
53
54
$cacheProxyManagerTestAsset_ClassWithMixedProperties ?: $cacheProxyManagerTestAsset_ClassWithMixedProperties = '
55
        . '\Closure::bind(function ($instance) {
56
    $instance->privateProperty0 = \'privateProperty0\';
57
    $instance->privateProperty1 = \'privateProperty1\';
58
    $instance->privateProperty2 = \'privateProperty2\';
59
}, null, \'ProxyManagerTestAsset\\\\ClassWithMixedProperties\');
60
61
$cacheProxyManagerTestAsset_ClassWithMixedProperties($this);
62
63
64
65
66
$properties = [
67
    \'publicProperty0\' => & $this->publicProperty0,
68
    \'publicProperty1\' => & $this->publicProperty1,
69
    \'publicProperty2\' => & $this->publicProperty2,
70
    \'\' . "\0" . \'*\' . "\0" . \'protectedProperty0\' => & $this->protectedProperty0,
71
    \'\' . "\0" . \'*\' . "\0" . \'protectedProperty1\' => & $this->protectedProperty1,
72
    \'\' . "\0" . \'*\' . "\0" . \'protectedProperty2\' => & $this->protectedProperty2,
73
];
74
75
static $cacheFetchProxyManagerTestAsset_ClassWithMixedProperties;
76
77
$cacheFetchProxyManagerTestAsset_ClassWithMixedProperties ?: $cacheFetchProxyManagerTestAsset_ClassWithMixedProperties '
78
            . '= \Closure::bind(function ($instance, array & $properties) {
79
    $properties[\'\' . "\0" . \'ProxyManagerTestAsset\\\\ClassWithMixedProperties\' . "\0" . \'privateProperty0\'] = '
80
            . '& $instance->privateProperty0;
81
    $properties[\'\' . "\0" . \'ProxyManagerTestAsset\\\\ClassWithMixedProperties\' . "\0" . \'privateProperty1\'] = '
82
            . '& $instance->privateProperty1;
83
    $properties[\'\' . "\0" . \'ProxyManagerTestAsset\\\\ClassWithMixedProperties\' . "\0" . \'privateProperty2\'] = '
84
            . '& $instance->privateProperty2;
85
}, $this, \'ProxyManagerTestAsset\\\\ClassWithMixedProperties\');
86
87
$cacheFetchProxyManagerTestAsset_ClassWithMixedProperties($this, $properties);
88
89
$result = $this->init->__invoke($this, $methodName, $parameters, $this->init, $properties);
90
$this->track = false;
91
92
return $result;';
93
94
        self::assertSame(
95
            $expectedCode,
96
            $callInitializer->getBody()
97
        );
98
    }
99
100
101
    public function testBodyStructureWithTypedProperties() : void
102
    {
103
        /** @var PropertyGenerator|PHPUnit_Framework_MockObject_MockObject $initializer */
104
        $initializer = $this->createMock(PropertyGenerator::class);
105
        /** @var PropertyGenerator|PHPUnit_Framework_MockObject_MockObject $initializationTracker */
106
        $initializationTracker = $this->createMock(PropertyGenerator::class);
107
108
        $initializer->expects(self::any())->method('getName')->will(self::returnValue('init'));
109
        $initializationTracker->expects(self::any())->method('getName')->will(self::returnValue('track'));
110
111
        $callInitializer = new CallInitializer(
112
            $initializer,
113
            $initializationTracker,
114
            Properties::fromReflectionClass(new ReflectionClass(ClassWithMixedTypedProperties::class))
115
        );
116
117
        $expectedCode = <<<'PHP'
118
if ($this->track || ! $this->init) {
119
    return;
120
}
121
122
$this->track = true;
123
124
$this->publicUnTypedProperty = 'publicUnTypedProperty';
125
$this->publicUnTypedPropertyWithoutDefaultValue = NULL;
126
$this->publicBoolProperty = true;
127
$this->publicNullableBoolProperty = true;
128
$this->publicIntProperty = 123;
129
$this->publicNullableIntProperty = 123;
130
$this->publicFloatProperty = 123.456;
131
$this->publicNullableFloatProperty = 123.456;
132
$this->publicStringProperty = 'publicStringProperty';
133
$this->publicNullableStringProperty = 'publicStringProperty';
134
$this->publicArrayProperty = array (
135
  0 => 'publicArrayProperty',
136
);
137
$this->publicNullableArrayProperty = array (
138
  0 => 'publicArrayProperty',
139
);
140
$this->publicIterableProperty = array (
141
  0 => 'publicIterableProperty',
142
);
143
$this->publicNullableIterableProperty = array (
144
  0 => 'publicIterableProperty',
145
);
146
$this->protectedUnTypedProperty = 'protectedUnTypedProperty';
147
$this->protectedUnTypedPropertyWithoutDefaultValue = NULL;
148
$this->protectedBoolProperty = true;
149
$this->protectedNullableBoolProperty = true;
150
$this->protectedIntProperty = 123;
151
$this->protectedNullableIntProperty = 123;
152
$this->protectedFloatProperty = 123.456;
153
$this->protectedNullableFloatProperty = 123.456;
154
$this->protectedStringProperty = 'protectedStringProperty';
155
$this->protectedNullableStringProperty = 'protectedStringProperty';
156
$this->protectedArrayProperty = array (
157
  0 => 'protectedArrayProperty',
158
);
159
$this->protectedNullableArrayProperty = array (
160
  0 => 'protectedArrayProperty',
161
);
162
$this->protectedIterableProperty = array (
163
  0 => 'protectedIterableProperty',
164
);
165
$this->protectedNullableIterableProperty = array (
166
  0 => 'protectedIterableProperty',
167
);
168
static $cacheProxyManagerTestAsset_ClassWithMixedTypedProperties;
169
170
$cacheProxyManagerTestAsset_ClassWithMixedTypedProperties ?: $cacheProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(function ($instance) {
171
    $instance->privateUnTypedProperty = 'privateUnTypedProperty';
172
    $instance->privateUnTypedPropertyWithoutDefaultValue = NULL;
173
    $instance->privateBoolProperty = true;
174
    $instance->privateNullableBoolProperty = true;
175
    $instance->privateIntProperty = 123;
176
    $instance->privateNullableIntProperty = 123;
177
    $instance->privateFloatProperty = 123.456;
178
    $instance->privateNullableFloatProperty = 123.456;
179
    $instance->privateStringProperty = 'privateStringProperty';
180
    $instance->privateNullableStringProperty = 'privateStringProperty';
181
    $instance->privateArrayProperty = array (
182
  0 => 'privateArrayProperty',
183
);
184
    $instance->privateNullableArrayProperty = array (
185
  0 => 'privateArrayProperty',
186
);
187
    $instance->privateIterableProperty = array (
188
  0 => 'privateIterableProperty',
189
);
190
    $instance->privateNullableIterableProperty = array (
191
  0 => 'privateIterableProperty',
192
);
193
}, null, 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties');
194
195
$cacheProxyManagerTestAsset_ClassWithMixedTypedProperties($this);
196
197
198
199
200
$properties = [
201
    'publicUnTypedProperty' => & $this->publicUnTypedProperty,
202
    'publicUnTypedPropertyWithoutDefaultValue' => & $this->publicUnTypedPropertyWithoutDefaultValue,
203
    'publicBoolProperty' => & $this->publicBoolProperty,
204
    'publicNullableBoolProperty' => & $this->publicNullableBoolProperty,
205
    'publicIntProperty' => & $this->publicIntProperty,
206
    'publicNullableIntProperty' => & $this->publicNullableIntProperty,
207
    'publicFloatProperty' => & $this->publicFloatProperty,
208
    'publicNullableFloatProperty' => & $this->publicNullableFloatProperty,
209
    'publicStringProperty' => & $this->publicStringProperty,
210
    'publicNullableStringProperty' => & $this->publicNullableStringProperty,
211
    'publicArrayProperty' => & $this->publicArrayProperty,
212
    'publicNullableArrayProperty' => & $this->publicNullableArrayProperty,
213
    'publicIterableProperty' => & $this->publicIterableProperty,
214
    'publicNullableIterableProperty' => & $this->publicNullableIterableProperty,
215
    '' . "\0" . '*' . "\0" . 'protectedUnTypedProperty' => & $this->protectedUnTypedProperty,
216
    '' . "\0" . '*' . "\0" . 'protectedUnTypedPropertyWithoutDefaultValue' => & $this->protectedUnTypedPropertyWithoutDefaultValue,
217
    '' . "\0" . '*' . "\0" . 'protectedBoolProperty' => & $this->protectedBoolProperty,
218
    '' . "\0" . '*' . "\0" . 'protectedNullableBoolProperty' => & $this->protectedNullableBoolProperty,
219
    '' . "\0" . '*' . "\0" . 'protectedIntProperty' => & $this->protectedIntProperty,
220
    '' . "\0" . '*' . "\0" . 'protectedNullableIntProperty' => & $this->protectedNullableIntProperty,
221
    '' . "\0" . '*' . "\0" . 'protectedFloatProperty' => & $this->protectedFloatProperty,
222
    '' . "\0" . '*' . "\0" . 'protectedNullableFloatProperty' => & $this->protectedNullableFloatProperty,
223
    '' . "\0" . '*' . "\0" . 'protectedStringProperty' => & $this->protectedStringProperty,
224
    '' . "\0" . '*' . "\0" . 'protectedNullableStringProperty' => & $this->protectedNullableStringProperty,
225
    '' . "\0" . '*' . "\0" . 'protectedArrayProperty' => & $this->protectedArrayProperty,
226
    '' . "\0" . '*' . "\0" . 'protectedNullableArrayProperty' => & $this->protectedNullableArrayProperty,
227
    '' . "\0" . '*' . "\0" . 'protectedIterableProperty' => & $this->protectedIterableProperty,
228
    '' . "\0" . '*' . "\0" . 'protectedNullableIterableProperty' => & $this->protectedNullableIterableProperty,
229
];
230
231
static $cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties;
232
233
$cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties ?: $cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties = \Closure::bind(function ($instance, array & $properties) {
234
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateUnTypedProperty'] = & $instance->privateUnTypedProperty;
235
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateUnTypedPropertyWithoutDefaultValue'] = & $instance->privateUnTypedPropertyWithoutDefaultValue;
236
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateBoolProperty'] = & $instance->privateBoolProperty;
237
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableBoolProperty'] = & $instance->privateNullableBoolProperty;
238
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateIntProperty'] = & $instance->privateIntProperty;
239
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableIntProperty'] = & $instance->privateNullableIntProperty;
240
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateFloatProperty'] = & $instance->privateFloatProperty;
241
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableFloatProperty'] = & $instance->privateNullableFloatProperty;
242
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateStringProperty'] = & $instance->privateStringProperty;
243
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableStringProperty'] = & $instance->privateNullableStringProperty;
244
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateArrayProperty'] = & $instance->privateArrayProperty;
245
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableArrayProperty'] = & $instance->privateNullableArrayProperty;
246
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateIterableProperty'] = & $instance->privateIterableProperty;
247
    $properties['' . "\0" . 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties' . "\0" . 'privateNullableIterableProperty'] = & $instance->privateNullableIterableProperty;
248
}, $this, 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties');
249
250
$cacheFetchProxyManagerTestAsset_ClassWithMixedTypedProperties($this, $properties);
251
252
$result = $this->init->__invoke($this, $methodName, $parameters, $this->init, $properties);
253
$this->track = false;
254
255
return $result;
256
PHP;
257
258
        self::assertSame(
259
            $expectedCode,
260
            $callInitializer->getBody()
261
        );
262
    }
263
}
264