|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace ProxyManagerTest\ProxyGenerator\ValueHolder\MethodGenerator; |
|
20
|
|
|
|
|
21
|
|
|
use PHPUnit_Framework_TestCase; |
|
22
|
|
|
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor; |
|
23
|
|
|
use ProxyManagerTestAsset\ClassWithMixedProperties; |
|
24
|
|
|
use ProxyManagerTestAsset\ClassWithVariadicConstructorArgument; |
|
25
|
|
|
use ProxyManagerTestAsset\EmptyClass; |
|
26
|
|
|
use ProxyManagerTestAsset\ProxyGenerator\LazyLoading\MethodGenerator\ClassWithTwoPublicProperties; |
|
27
|
|
|
use ReflectionClass; |
|
28
|
|
|
use Zend\Code\Generator\PropertyGenerator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Tests for {@see \ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor} |
|
32
|
|
|
* |
|
33
|
|
|
* @author Marco Pivetta <[email protected]> |
|
34
|
|
|
* @license MIT |
|
35
|
|
|
* |
|
36
|
|
|
* @covers \ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor |
|
37
|
|
|
* @group Coverage |
|
38
|
|
|
*/ |
|
39
|
|
|
class ConstructorTest extends PHPUnit_Framework_TestCase |
|
40
|
|
|
{ |
|
41
|
|
|
public function testBodyStructure() |
|
42
|
|
|
{ |
|
43
|
|
|
/* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */ |
|
44
|
|
|
$valueHolder = $this->getMock(PropertyGenerator::class); |
|
45
|
|
|
|
|
46
|
|
|
$valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo')); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
$constructor = Constructor::generateMethod( |
|
49
|
|
|
new ReflectionClass( |
|
50
|
|
|
ClassWithTwoPublicProperties::class |
|
51
|
|
|
), |
|
52
|
|
|
$valueHolder |
|
|
|
|
|
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertSame('__construct', $constructor->getName()); |
|
56
|
|
|
$this->assertCount(0, $constructor->getParameters()); |
|
57
|
|
|
$this->assertSame( |
|
58
|
|
|
'static $reflection; |
|
59
|
|
|
|
|
60
|
|
|
if (! $this->foo) { |
|
61
|
|
|
$reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\ProxyGenerator\\\\LazyLoading\\\\' |
|
62
|
|
|
. 'MethodGenerator\\\\ClassWithTwoPublicProperties\'); |
|
63
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
64
|
|
|
|
|
65
|
|
|
unset($this->bar); |
|
66
|
|
|
unset($this->baz); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->foo->__construct();', |
|
70
|
|
|
$constructor->getBody() |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testBodyStructureWithoutPublicProperties() |
|
75
|
|
|
{ |
|
76
|
|
|
/* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */ |
|
77
|
|
|
$valueHolder = $this->getMock(PropertyGenerator::class); |
|
78
|
|
|
|
|
79
|
|
|
$valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo')); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$constructor = Constructor::generateMethod( |
|
82
|
|
|
new ReflectionClass(EmptyClass::class), |
|
83
|
|
|
$valueHolder |
|
|
|
|
|
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertSame('__construct', $constructor->getName()); |
|
87
|
|
|
$this->assertCount(0, $constructor->getParameters()); |
|
88
|
|
|
$this->assertSame( |
|
89
|
|
|
'static $reflection; |
|
90
|
|
|
|
|
91
|
|
|
if (! $this->foo) { |
|
92
|
|
|
$reflection = $reflection ?: new \ReflectionClass(\'ProxyManagerTestAsset\\\\EmptyClass\'); |
|
93
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->foo->__construct();', |
|
97
|
|
|
$constructor->getBody() |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testBodyStructureWithPhp4StyleConstructor() |
|
102
|
|
|
{ |
|
103
|
|
|
if (PHP_VERSION_ID >= 70000) { |
|
104
|
|
|
$this->markTestSkipped('Can\'t run this test on PHP7 or high version!'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$className = uniqid('ClassWithPhp4Constructor'); |
|
108
|
|
|
|
|
109
|
|
|
eval('class ' . $className . '{ public function ' . $className . '($first, $second, $third) {}}'); |
|
110
|
|
|
|
|
111
|
|
|
/* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */ |
|
112
|
|
|
$valueHolder = $this->getMock(PropertyGenerator::class); |
|
113
|
|
|
|
|
114
|
|
|
$valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo')); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$constructor = Constructor::generateMethod( |
|
117
|
|
|
new ReflectionClass($className), |
|
118
|
|
|
$valueHolder |
|
|
|
|
|
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertSame($className, $constructor->getName()); |
|
122
|
|
|
$this->assertCount(3, $constructor->getParameters()); |
|
123
|
|
|
$this->assertSame( |
|
124
|
|
|
'static $reflection; |
|
125
|
|
|
|
|
126
|
|
|
if (! $this->foo) { |
|
127
|
|
|
$reflection = $reflection ?: new \ReflectionClass(\'' . $className . '\'); |
|
128
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->foo->' . $className . '($first, $second, $third);', |
|
132
|
|
|
$constructor->getBody() |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testBodyStructureWithStaticProperties() |
|
137
|
|
|
{ |
|
138
|
|
|
/* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */ |
|
139
|
|
|
$valueHolder = $this->getMock(PropertyGenerator::class); |
|
140
|
|
|
|
|
141
|
|
|
$valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo')); |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
$constructor = Constructor::generateMethod(new ReflectionClass(ClassWithMixedProperties::class), $valueHolder); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertSame('__construct', $constructor->getName()); |
|
146
|
|
|
$this->assertCount(0, $constructor->getParameters()); |
|
147
|
|
|
|
|
148
|
|
|
$expectedCode = <<<'PHP' |
|
149
|
|
|
static $reflection; |
|
150
|
|
|
|
|
151
|
|
|
if (! $this->foo) { |
|
152
|
|
|
$reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithMixedProperties'); |
|
153
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
154
|
|
|
|
|
155
|
|
|
unset($this->publicProperty0); |
|
156
|
|
|
unset($this->publicProperty1); |
|
157
|
|
|
unset($this->publicProperty2); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$this->foo->__construct(); |
|
161
|
|
|
PHP; |
|
162
|
|
|
|
|
163
|
|
|
$this->assertSame($expectedCode, $constructor->getBody()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testBodyStructureWithVariadicArguments() |
|
167
|
|
|
{ |
|
168
|
|
|
/* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */ |
|
169
|
|
|
$valueHolder = $this->getMock(PropertyGenerator::class); |
|
170
|
|
|
|
|
171
|
|
|
$valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo')); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
$constructor = Constructor::generateMethod( |
|
174
|
|
|
new ReflectionClass(ClassWithVariadicConstructorArgument::class), |
|
175
|
|
|
$valueHolder |
|
|
|
|
|
|
176
|
|
|
); |
|
177
|
|
|
|
|
178
|
|
|
$this->assertSame('__construct', $constructor->getName()); |
|
179
|
|
|
$this->assertCount(2, $constructor->getParameters()); |
|
180
|
|
|
|
|
181
|
|
|
$expectedCode = <<<'PHP' |
|
182
|
|
|
static $reflection; |
|
183
|
|
|
|
|
184
|
|
|
if (! $this->foo) { |
|
185
|
|
|
$reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithVariadicConstructorArgument'); |
|
186
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$this->foo->__construct($foo, ...$bar); |
|
190
|
|
|
PHP; |
|
191
|
|
|
|
|
192
|
|
|
$this->assertSame($expectedCode, $constructor->getBody()); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: