|
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 testBodyStructureWithStaticProperties() |
|
102
|
|
|
{ |
|
103
|
|
|
/* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */ |
|
104
|
|
|
$valueHolder = $this->getMock(PropertyGenerator::class); |
|
105
|
|
|
|
|
106
|
|
|
$valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo')); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
$constructor = Constructor::generateMethod(new ReflectionClass(ClassWithMixedProperties::class), $valueHolder); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertSame('__construct', $constructor->getName()); |
|
111
|
|
|
$this->assertCount(0, $constructor->getParameters()); |
|
112
|
|
|
|
|
113
|
|
|
$expectedCode = <<<'PHP' |
|
114
|
|
|
static $reflection; |
|
115
|
|
|
|
|
116
|
|
|
if (! $this->foo) { |
|
117
|
|
|
$reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithMixedProperties'); |
|
118
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
119
|
|
|
|
|
120
|
|
|
unset($this->publicProperty0); |
|
121
|
|
|
unset($this->publicProperty1); |
|
122
|
|
|
unset($this->publicProperty2); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->foo->__construct(); |
|
126
|
|
|
PHP; |
|
127
|
|
|
|
|
128
|
|
|
$this->assertSame($expectedCode, $constructor->getBody()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function testBodyStructureWithVariadicArguments() |
|
132
|
|
|
{ |
|
133
|
|
|
/* @var $valueHolder PropertyGenerator|\PHPUnit_Framework_MockObject_MockObject */ |
|
134
|
|
|
$valueHolder = $this->getMock(PropertyGenerator::class); |
|
135
|
|
|
|
|
136
|
|
|
$valueHolder->expects($this->any())->method('getName')->will($this->returnValue('foo')); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
$constructor = Constructor::generateMethod( |
|
139
|
|
|
new ReflectionClass(ClassWithVariadicConstructorArgument::class), |
|
140
|
|
|
$valueHolder |
|
|
|
|
|
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertSame('__construct', $constructor->getName()); |
|
144
|
|
|
$this->assertCount(2, $constructor->getParameters()); |
|
145
|
|
|
|
|
146
|
|
|
$expectedCode = <<<'PHP' |
|
147
|
|
|
static $reflection; |
|
148
|
|
|
|
|
149
|
|
|
if (! $this->foo) { |
|
150
|
|
|
$reflection = $reflection ?: new \ReflectionClass('ProxyManagerTestAsset\\ClassWithVariadicConstructorArgument'); |
|
151
|
|
|
$this->foo = $reflection->newInstanceWithoutConstructor(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$this->foo->__construct($foo, ...$bar); |
|
155
|
|
|
PHP; |
|
156
|
|
|
|
|
157
|
|
|
$this->assertSame($expectedCode, $constructor->getBody()); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.