1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ProxyManagerTest\ProxyGenerator\Util; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use ProxyManager\ProxyGenerator\Util\Properties; |
9
|
|
|
use ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator; |
10
|
|
|
use ProxyManagerTestAsset\BaseClass; |
11
|
|
|
use ProxyManagerTestAsset\ClassWithCollidingPrivateInheritedProperties; |
12
|
|
|
use ProxyManagerTestAsset\ClassWithMixedProperties; |
13
|
|
|
use ProxyManagerTestAsset\ClassWithMixedTypedProperties; |
14
|
|
|
use ProxyManagerTestAsset\EmptyClass; |
15
|
|
|
use ReflectionClass; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Tests for {@see \ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator} |
19
|
|
|
* |
20
|
|
|
* @covers \ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator |
21
|
|
|
* @group Coverage |
22
|
|
|
*/ |
23
|
|
|
class UnsetPropertiesGeneratorTest extends TestCase |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @dataProvider classNamesProvider |
27
|
|
|
*/ |
28
|
|
|
public function testGeneratedCode(string $className, string $expectedCode, string $instanceName) : void |
29
|
|
|
{ |
30
|
|
|
self::assertSame( |
31
|
|
|
$expectedCode, |
32
|
|
|
UnsetPropertiesGenerator::generateSnippet( |
33
|
|
|
Properties::fromReflectionClass(new ReflectionClass($className)), |
34
|
|
|
$instanceName |
35
|
|
|
) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string[][] |
41
|
|
|
*/ |
42
|
|
|
public function classNamesProvider() : array |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
EmptyClass::class => [ |
46
|
|
|
EmptyClass::class, |
47
|
|
|
'', |
48
|
|
|
'foo', |
49
|
|
|
], |
50
|
|
|
BaseClass::class => [ |
51
|
|
|
BaseClass::class, |
52
|
|
|
'unset($foo->publicProperty, $foo->protectedProperty); |
53
|
|
|
|
54
|
|
|
\Closure::bind(function (\ProxyManagerTestAsset\BaseClass $instance) { |
55
|
|
|
unset($instance->privateProperty); |
56
|
|
|
}, $foo, \'ProxyManagerTestAsset\\\\BaseClass\')->__invoke($foo); |
57
|
|
|
|
58
|
|
|
', |
59
|
|
|
'foo', |
60
|
|
|
], |
61
|
|
|
ClassWithMixedProperties::class => [ |
62
|
|
|
ClassWithMixedProperties::class, |
63
|
|
|
'unset($foo->publicProperty0, $foo->publicProperty1, $foo->publicProperty2, $foo->protectedProperty0, ' |
64
|
|
|
. '$foo->protectedProperty1, $foo->protectedProperty2); |
65
|
|
|
|
66
|
|
|
\Closure::bind(function (\ProxyManagerTestAsset\ClassWithMixedProperties $instance) { |
67
|
|
|
unset($instance->privateProperty0, $instance->privateProperty1, $instance->privateProperty2); |
68
|
|
|
}, $foo, \'ProxyManagerTestAsset\\\\ClassWithMixedProperties\')->__invoke($foo); |
69
|
|
|
|
70
|
|
|
', |
71
|
|
|
'foo', |
72
|
|
|
], |
73
|
|
|
ClassWithCollidingPrivateInheritedProperties::class => [ |
74
|
|
|
ClassWithCollidingPrivateInheritedProperties::class, |
75
|
|
|
'\Closure::bind(function (\ProxyManagerTestAsset\ClassWithCollidingPrivateInheritedProperties ' |
76
|
|
|
. '$instance) { |
77
|
|
|
unset($instance->property0); |
78
|
|
|
}, $bar, \'ProxyManagerTestAsset\\\\ClassWithCollidingPrivateInheritedProperties\')->__invoke($bar); |
79
|
|
|
|
80
|
|
|
\Closure::bind(function (\ProxyManagerTestAsset\ClassWithPrivateProperties $instance) { |
81
|
|
|
unset($instance->property0, $instance->property1, $instance->property2, $instance->property3, ' |
82
|
|
|
. '$instance->property4, $instance->property5, $instance->property6, $instance->property7, ' |
83
|
|
|
. '$instance->property8, $instance->property9); |
84
|
|
|
}, $bar, \'ProxyManagerTestAsset\\\\ClassWithPrivateProperties\')->__invoke($bar); |
85
|
|
|
|
86
|
|
|
', |
87
|
|
|
'bar', |
88
|
|
|
], |
89
|
|
|
ClassWithMixedTypedProperties::class => [ |
90
|
|
|
ClassWithMixedTypedProperties::class, |
91
|
|
|
<<<'PHP' |
92
|
|
|
unset($bar->publicUnTypedProperty, $bar->publicUnTypedPropertyWithoutDefaultValue, $bar->publicBoolProperty, $bar->publicNullableBoolProperty, $bar->publicIntProperty, $bar->publicNullableIntProperty, $bar->publicFloatProperty, $bar->publicNullableFloatProperty, $bar->publicStringProperty, $bar->publicNullableStringProperty, $bar->publicArrayProperty, $bar->publicNullableArrayProperty, $bar->publicIterableProperty, $bar->publicNullableIterableProperty, $bar->protectedUnTypedProperty, $bar->protectedUnTypedPropertyWithoutDefaultValue, $bar->protectedBoolProperty, $bar->protectedNullableBoolProperty, $bar->protectedIntProperty, $bar->protectedNullableIntProperty, $bar->protectedFloatProperty, $bar->protectedNullableFloatProperty, $bar->protectedStringProperty, $bar->protectedNullableStringProperty, $bar->protectedArrayProperty, $bar->protectedNullableArrayProperty, $bar->protectedIterableProperty, $bar->protectedNullableIterableProperty); |
|
|
|
|
93
|
|
|
|
94
|
|
|
\Closure::bind(function (\ProxyManagerTestAsset\ClassWithMixedTypedProperties $instance) { |
95
|
|
|
unset($instance->privateUnTypedProperty, $instance->privateUnTypedPropertyWithoutDefaultValue, $instance->privateBoolProperty, $instance->privateNullableBoolProperty, $instance->privateIntProperty, $instance->privateNullableIntProperty, $instance->privateFloatProperty, $instance->privateNullableFloatProperty, $instance->privateStringProperty, $instance->privateNullableStringProperty, $instance->privateArrayProperty, $instance->privateNullableArrayProperty, $instance->privateIterableProperty, $instance->privateNullableIterableProperty); |
96
|
|
|
}, $bar, 'ProxyManagerTestAsset\\ClassWithMixedTypedProperties')->__invoke($bar); |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
PHP, |
100
|
|
|
'bar', |
101
|
|
|
], |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|