|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ProxyManagerTest\ProxyGenerator\ValueHolder\MethodGenerator; |
|
6
|
|
|
|
|
7
|
|
|
use Laminas\Code\Generator\PropertyGenerator; |
|
8
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor; |
|
11
|
|
|
use ProxyManagerTestAsset\ClassWithMixedProperties; |
|
12
|
|
|
use ProxyManagerTestAsset\ClassWithVariadicConstructorArgument; |
|
13
|
|
|
use ProxyManagerTestAsset\EmptyClass; |
|
14
|
|
|
use ProxyManagerTestAsset\ProxyGenerator\LazyLoading\MethodGenerator\ClassWithTwoPublicProperties; |
|
15
|
|
|
use ReflectionClass; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Tests for {@see \ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor} |
|
19
|
|
|
* |
|
20
|
|
|
* @covers \ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor |
|
21
|
|
|
* @group Coverage |
|
22
|
|
|
*/ |
|
23
|
|
|
final class ConstructorTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
public function testBodyStructure() : void |
|
26
|
|
|
{ |
|
27
|
|
|
$valueHolder = $this->createMock(PropertyGenerator::class); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$valueHolder->method('getName')->willReturn('foo'); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
$constructor = Constructor::generateMethod( |
|
32
|
|
|
new ReflectionClass( |
|
33
|
|
|
ClassWithTwoPublicProperties::class |
|
34
|
|
|
), |
|
35
|
|
|
$valueHolder |
|
|
|
|
|
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
self::assertSame('__construct', $constructor->getName()); |
|
|
|
|
|
|
39
|
|
|
self::assertCount(0, $constructor->getParameters()); |
|
|
|
|
|
|
40
|
|
|
self::assertSame( |
|
|
|
|
|
|
41
|
|
|
'static $reflection; |
|
42
|
|
|
|
|
43
|
|
|
if (! $this->foo) { |
|
44
|
|
|
$reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\ProxyGenerator\\\\LazyLoading\\\\' |
|
45
|
|
|
. 'MethodGenerator\\\\ClassWithTwoPublicProperties\'); |
|
46
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
47
|
|
|
unset($this->bar, $this->baz); |
|
48
|
|
|
|
|
49
|
|
|
}', |
|
50
|
|
|
$constructor->getBody() |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testBodyStructureWithoutPublicProperties() : void |
|
55
|
|
|
{ |
|
56
|
|
|
$valueHolder = $this->createMock(PropertyGenerator::class); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
$valueHolder->method('getName')->willReturn('foo'); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$constructor = Constructor::generateMethod( |
|
61
|
|
|
new ReflectionClass(EmptyClass::class), |
|
62
|
|
|
$valueHolder |
|
|
|
|
|
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
self::assertSame('__construct', $constructor->getName()); |
|
|
|
|
|
|
66
|
|
|
self::assertCount(0, $constructor->getParameters()); |
|
|
|
|
|
|
67
|
|
|
self::assertSame( |
|
|
|
|
|
|
68
|
|
|
'static $reflection; |
|
69
|
|
|
|
|
70
|
|
|
if (! $this->foo) { |
|
71
|
|
|
$reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\EmptyClass\'); |
|
72
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
73
|
|
|
}', |
|
74
|
|
|
$constructor->getBody() |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testBodyStructureWithStaticProperties() : void |
|
79
|
|
|
{ |
|
80
|
|
|
$valueHolder = $this->createMock(PropertyGenerator::class); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$valueHolder->method('getName')->willReturn('foo'); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$constructor = Constructor::generateMethod(new ReflectionClass(ClassWithMixedProperties::class), $valueHolder); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
self::assertSame('__construct', $constructor->getName()); |
|
|
|
|
|
|
87
|
|
|
self::assertCount(0, $constructor->getParameters()); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$expectedCode = 'static $reflection; |
|
90
|
|
|
|
|
91
|
|
|
if (! $this->foo) { |
|
92
|
|
|
$reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\ClassWithMixedProperties\'); |
|
93
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
94
|
|
|
unset($this->publicProperty0, $this->publicProperty1, $this->publicProperty2, $this->protectedProperty0, ' |
|
95
|
|
|
. '$this->protectedProperty1, $this->protectedProperty2); |
|
96
|
|
|
|
|
97
|
|
|
\Closure::bind(function (\ProxyManagerTestAsset\ClassWithMixedProperties $instance) { |
|
98
|
|
|
unset($instance->privateProperty0, $instance->privateProperty1, $instance->privateProperty2); |
|
99
|
|
|
}, $this, \'ProxyManagerTestAsset\\\\ClassWithMixedProperties\')->__invoke($this); |
|
100
|
|
|
|
|
101
|
|
|
}'; |
|
102
|
|
|
|
|
103
|
|
|
self::assertSame($expectedCode, $constructor->getBody()); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testBodyStructureWithVariadicArguments() : void |
|
107
|
|
|
{ |
|
108
|
|
|
$valueHolder = $this->createMock(PropertyGenerator::class); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
$valueHolder->method('getName')->willReturn('foo'); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
$constructor = Constructor::generateMethod( |
|
113
|
|
|
new ReflectionClass(ClassWithVariadicConstructorArgument::class), |
|
114
|
|
|
$valueHolder |
|
|
|
|
|
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
self::assertSame('__construct', $constructor->getName()); |
|
|
|
|
|
|
118
|
|
|
self::assertCount(2, $constructor->getParameters()); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$expectedCode = <<<'PHP' |
|
121
|
|
|
static $reflection; |
|
122
|
|
|
|
|
123
|
|
|
if (! $this->foo) { |
|
124
|
|
|
$reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithVariadicConstructorArgument'); |
|
125
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
126
|
|
|
\Closure::bind(function (\ProxyManagerTestAsset\ClassWithVariadicConstructorArgument $instance) { |
|
127
|
|
|
unset($instance->foo, $instance->bar); |
|
128
|
|
|
}, $this, 'ProxyManagerTestAsset\\ClassWithVariadicConstructorArgument')->__invoke($this); |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$this->foo->__construct($foo, ...$bar); |
|
133
|
|
|
PHP; |
|
134
|
|
|
|
|
135
|
|
|
self::assertSame($expectedCode, $constructor->getBody()); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.