forked from doctrine/common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProxyClassGeneratorTest.php
228 lines (184 loc) · 8.91 KB
/
ProxyClassGeneratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Tests\Common\Proxy;
use Doctrine\Common\Proxy\ProxyGenerator;
use ReflectionClass;
use ReflectionMethod;
use PHPUnit_Framework_TestCase;
/**
* Test the proxy generator. Its work is generating on-the-fly subclasses of a given model, which implement the Proxy
* pattern.
*
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
* @author Marco Pivetta <ocramius@gmail.com>
*/
class ProxyClassGeneratorTest extends PHPUnit_Framework_TestCase
{
/**
* @var string
*/
protected $proxyClass = 'Doctrine\Tests\Common\ProxyProxy\__CG__\Doctrine\Tests\Common\Proxy\LazyLoadableObject';
/**
* @var LazyLoadableObjectClassMetadata
*/
protected $metadata;
/**
* @var ProxyGenerator
*/
protected $proxyGenerator;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->metadata = new LazyLoadableObjectClassMetadata();
$this->proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
if (class_exists($this->proxyClass, false)) {
return;
}
$this->generateAndRequire($this->proxyGenerator, $this->metadata);
}
public function testReferenceProxyRespectsMethodsParametersTypeHinting()
{
$method = new ReflectionMethod($this->proxyClass, 'publicTypeHintedMethod');
$params = $method->getParameters();
$this->assertEquals(1, count($params));
$this->assertEquals('stdClass', $params[0]->getClass()->getName());
}
public function testProxyRespectsMethodsWhichReturnValuesByReference()
{
$method = new ReflectionMethod($this->proxyClass, 'byRefMethod');
$this->assertTrue($method->returnsReference());
}
public function testProxyRespectsByRefMethodParameters()
{
$method = new ReflectionMethod($this->proxyClass, 'byRefParamMethod');
$parameters = $method->getParameters();
$this->assertSame('thisIsNotByRef', $parameters[0]->getName());
$this->assertFalse($parameters[0]->isPassedByReference());
$this->assertSame('thisIsByRef', $parameters[1]->getName());
$this->assertTrue($parameters[1]->isPassedByReference());
}
public function testCreatesAssociationProxyAsSubclassOfTheOriginalOne()
{
$this->assertTrue(is_subclass_of($this->proxyClass, $this->metadata->getName()));
}
public function testNonNamespacedProxyGeneration()
{
$classCode = file_get_contents($this->proxyGenerator->getProxyFileName($this->metadata->getName()));
$this->assertNotContains("class LazyLoadableObject extends \\\\" . $this->metadata->getName(), $classCode);
$this->assertContains("class LazyLoadableObject extends \\" . $this->metadata->getName(), $classCode);
}
public function testClassWithSleepProxyGeneration()
{
if (!class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\SleepClass', false)) {
$className = 'Doctrine\Tests\Common\Proxy\SleepClass';
$metadata = $this->createClassMetadata($className, array('id'));
$proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
$this->generateAndRequire($proxyGenerator, $metadata);
}
$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxySleepClass.php');
$this->assertEquals(1, substr_count($classCode, 'function __sleep'));
$this->assertEquals(1, substr_count($classCode, 'parent::__sleep()'));
}
/**
* Check that the proxy doesn't serialize static properties (in __sleep() method)
* @group DCOM-212
*/
public function testClassWithStaticPropertyProxyGeneration()
{
if (!class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\StaticPropertyClass', false)) {
$className = 'Doctrine\Tests\Common\Proxy\StaticPropertyClass';
$metadata = $this->createClassMetadata($className, array());
$proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
$this->generateAndRequire($proxyGenerator, $metadata);
}
$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyStaticPropertyClass.php');
$this->assertEquals(1, substr_count($classCode, 'function __sleep'));
$this->assertNotContains('protectedStaticProperty', $classCode);
}
private function generateAndRequire($proxyGenerator, $metadata)
{
$proxyGenerator->generateProxyClass($metadata, $proxyGenerator->getProxyFileName($metadata->getName()));
require_once $proxyGenerator->getProxyFileName($metadata->getName());
}
public function testClassWithCallableTypeHintOnProxiedMethod()
{
if (PHP_VERSION_ID < 50400) {
$this->markTestSkipped('`callable` is only supported in PHP >=5.4.0');
}
if (!class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\CallableTypeHintClass', false)) {
$className = 'Doctrine\Tests\Common\Proxy\CallableTypeHintClass';
$metadata = $this->createClassMetadata($className, array('id'));
$proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
$this->generateAndRequire($proxyGenerator, $metadata);
}
$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyCallableTypeHintClass.php');
$this->assertEquals(1, substr_count($classCode, 'call(callable $foo)'));
}
public function testClassWithInvalidTypeHintOnProxiedMethod()
{
$className = 'Doctrine\Tests\Common\Proxy\InvalidTypeHintClass';
$metadata = $this->createClassMetadata($className, array('id'));
$proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
$this->setExpectedException(
'Doctrine\Common\Proxy\Exception\UnexpectedValueException',
'The type hint of parameter "foo" in method "invalidTypeHintMethod"'
.' in class "' . $className . '" is invalid.'
);
$proxyGenerator->generateProxyClass($metadata);
}
public function testNoConfigDirThrowsException()
{
$this->setExpectedException('Doctrine\Common\Proxy\Exception\InvalidArgumentException');
new ProxyGenerator(null, null);
}
public function testNoNamespaceThrowsException()
{
$this->setExpectedException('Doctrine\Common\Proxy\Exception\InvalidArgumentException');
new ProxyGenerator(__DIR__ . '/generated', null);
}
public function testInvalidPlaceholderThrowsException()
{
$this->setExpectedException('Doctrine\Common\Proxy\Exception\InvalidArgumentException');
$generator = new ProxyGenerator(__DIR__ . '/generated', 'SomeNamespace');
$generator->setPlaceholder('<somePlaceholder>', array());
}
public function testUseEvalIfNoFilenameIsGiven()
{
$proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
$className = __NAMESPACE__ . '\\EvalBase';
$metadata = $this->createClassMetadata($className, array('id'));
$proxyGenerator->generateProxyClass($metadata);
$reflClass = new ReflectionClass('Doctrine\Tests\Common\ProxyProxy\__CG__\Doctrine\Tests\Common\Proxy\EvalBase');
$this->assertContains("eval()'d code", $reflClass->getFileName());
}
private function createClassMetadata($className, array $ids)
{
$metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
$reflClass = new ReflectionClass($className);
$metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue($reflClass));
$metadata->expects($this->any())->method('getIdentifierFieldNames')->will($this->returnValue($ids));
$metadata->expects($this->any())->method('getName')->will($this->returnValue($className));
return $metadata;
}
}
class EvalBase
{
}