PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/common/tests/Doctrine/Tests/Common/Proxy/ProxyClassGeneratorTest.php

https://gitlab.com/Snizer/PI-DEV-TUNISIAMALL3A6-WEB
PHP | 249 lines | 163 code | 46 blank | 40 comment | 7 complexity | 0499b2f8e9067f5b189ec9cff9af78dd MD5 | raw file
  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. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Tests\Common\Proxy;
  20. use Doctrine\Common\Proxy\ProxyGenerator;
  21. use ReflectionClass;
  22. use ReflectionMethod;
  23. use PHPUnit_Framework_TestCase;
  24. /**
  25. * Test the proxy generator. Its work is generating on-the-fly subclasses of a given model, which implement the Proxy
  26. * pattern.
  27. *
  28. * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
  29. * @author Marco Pivetta <ocramius@gmail.com>
  30. */
  31. class ProxyClassGeneratorTest extends PHPUnit_Framework_TestCase
  32. {
  33. /**
  34. * @var string
  35. */
  36. protected $proxyClass = 'Doctrine\Tests\Common\ProxyProxy\__CG__\Doctrine\Tests\Common\Proxy\LazyLoadableObject';
  37. /**
  38. * @var LazyLoadableObjectClassMetadata
  39. */
  40. protected $metadata;
  41. /**
  42. * @var ProxyGenerator
  43. */
  44. protected $proxyGenerator;
  45. /**
  46. * {@inheritDoc}
  47. */
  48. protected function setUp()
  49. {
  50. $this->metadata = new LazyLoadableObjectClassMetadata();
  51. $this->proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
  52. if (class_exists($this->proxyClass, false)) {
  53. return;
  54. }
  55. $this->generateAndRequire($this->proxyGenerator, $this->metadata);
  56. }
  57. public function testReferenceProxyRespectsMethodsParametersTypeHinting()
  58. {
  59. $method = new ReflectionMethod($this->proxyClass, 'publicTypeHintedMethod');
  60. $params = $method->getParameters();
  61. $this->assertEquals(1, count($params));
  62. $this->assertEquals('stdClass', $params[0]->getClass()->getName());
  63. }
  64. public function testProxyRespectsMethodsWhichReturnValuesByReference()
  65. {
  66. $method = new ReflectionMethod($this->proxyClass, 'byRefMethod');
  67. $this->assertTrue($method->returnsReference());
  68. }
  69. public function testProxyRespectsByRefMethodParameters()
  70. {
  71. $method = new ReflectionMethod($this->proxyClass, 'byRefParamMethod');
  72. $parameters = $method->getParameters();
  73. $this->assertSame('thisIsNotByRef', $parameters[0]->getName());
  74. $this->assertFalse($parameters[0]->isPassedByReference());
  75. $this->assertSame('thisIsByRef', $parameters[1]->getName());
  76. $this->assertTrue($parameters[1]->isPassedByReference());
  77. }
  78. public function testCreatesAssociationProxyAsSubclassOfTheOriginalOne()
  79. {
  80. $this->assertTrue(is_subclass_of($this->proxyClass, $this->metadata->getName()));
  81. }
  82. public function testNonNamespacedProxyGeneration()
  83. {
  84. $classCode = file_get_contents($this->proxyGenerator->getProxyFileName($this->metadata->getName()));
  85. $this->assertNotContains("class LazyLoadableObject extends \\\\" . $this->metadata->getName(), $classCode);
  86. $this->assertContains("class LazyLoadableObject extends \\" . $this->metadata->getName(), $classCode);
  87. }
  88. public function testClassWithSleepProxyGeneration()
  89. {
  90. if (!class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\SleepClass', false)) {
  91. $className = 'Doctrine\Tests\Common\Proxy\SleepClass';
  92. $metadata = $this->createClassMetadata($className, array('id'));
  93. $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
  94. $this->generateAndRequire($proxyGenerator, $metadata);
  95. }
  96. $classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxySleepClass.php');
  97. $this->assertEquals(1, substr_count($classCode, 'function __sleep'));
  98. $this->assertEquals(1, substr_count($classCode, 'parent::__sleep()'));
  99. }
  100. /**
  101. * Check that the proxy doesn't serialize static properties (in __sleep() method)
  102. * @group DCOM-212
  103. */
  104. public function testClassWithStaticPropertyProxyGeneration()
  105. {
  106. if (!class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\StaticPropertyClass', false)) {
  107. $className = 'Doctrine\Tests\Common\Proxy\StaticPropertyClass';
  108. $metadata = $this->createClassMetadata($className, array());
  109. $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
  110. $this->generateAndRequire($proxyGenerator, $metadata);
  111. }
  112. $classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyStaticPropertyClass.php');
  113. $this->assertEquals(1, substr_count($classCode, 'function __sleep'));
  114. $this->assertNotContains('protectedStaticProperty', $classCode);
  115. }
  116. private function generateAndRequire($proxyGenerator, $metadata)
  117. {
  118. $proxyGenerator->generateProxyClass($metadata, $proxyGenerator->getProxyFileName($metadata->getName()));
  119. require_once $proxyGenerator->getProxyFileName($metadata->getName());
  120. }
  121. public function testClassWithCallableTypeHintOnProxiedMethod()
  122. {
  123. if (PHP_VERSION_ID < 50400) {
  124. $this->markTestSkipped('`callable` is only supported in PHP >=5.4.0');
  125. }
  126. if (!class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\CallableTypeHintClass', false)) {
  127. $className = 'Doctrine\Tests\Common\Proxy\CallableTypeHintClass';
  128. $metadata = $this->createClassMetadata($className, array('id'));
  129. $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
  130. $this->generateAndRequire($proxyGenerator, $metadata);
  131. }
  132. $classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyCallableTypeHintClass.php');
  133. $this->assertEquals(1, substr_count($classCode, 'call(callable $foo)'));
  134. }
  135. public function testClassWithVariadicArgumentOnProxiedMethod()
  136. {
  137. if (PHP_VERSION_ID < 50600) {
  138. $this->markTestSkipped('`...` is only supported in PHP >=5.6.0');
  139. }
  140. if (!class_exists('Doctrine\Tests\Common\ProxyProxy\__CG__\VariadicTypeHintClass', false)) {
  141. $className = 'Doctrine\Tests\Common\Proxy\VariadicTypeHintClass';
  142. $metadata = $this->createClassMetadata($className, array('id'));
  143. $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
  144. $this->generateAndRequire($proxyGenerator, $metadata);
  145. }
  146. $classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyVariadicTypeHintClass.php');
  147. $this->assertEquals(1, substr_count($classCode, 'function addType(...$types)'));
  148. $this->assertEquals(1, substr_count($classCode, '__invoke($this, \'addType\', array($types))'));
  149. $this->assertEquals(1, substr_count($classCode, 'parent::addType(...$types)'));
  150. }
  151. public function testClassWithInvalidTypeHintOnProxiedMethod()
  152. {
  153. $className = 'Doctrine\Tests\Common\Proxy\InvalidTypeHintClass';
  154. $metadata = $this->createClassMetadata($className, array('id'));
  155. $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
  156. $this->setExpectedException(
  157. 'Doctrine\Common\Proxy\Exception\UnexpectedValueException',
  158. 'The type hint of parameter "foo" in method "invalidTypeHintMethod"'
  159. .' in class "' . $className . '" is invalid.'
  160. );
  161. $proxyGenerator->generateProxyClass($metadata);
  162. }
  163. public function testNoConfigDirThrowsException()
  164. {
  165. $this->setExpectedException('Doctrine\Common\Proxy\Exception\InvalidArgumentException');
  166. new ProxyGenerator(null, null);
  167. }
  168. public function testNoNamespaceThrowsException()
  169. {
  170. $this->setExpectedException('Doctrine\Common\Proxy\Exception\InvalidArgumentException');
  171. new ProxyGenerator(__DIR__ . '/generated', null);
  172. }
  173. public function testInvalidPlaceholderThrowsException()
  174. {
  175. $this->setExpectedException('Doctrine\Common\Proxy\Exception\InvalidArgumentException');
  176. $generator = new ProxyGenerator(__DIR__ . '/generated', 'SomeNamespace');
  177. $generator->setPlaceholder('<somePlaceholder>', array());
  178. }
  179. public function testUseEvalIfNoFilenameIsGiven()
  180. {
  181. $proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . 'Proxy', true);
  182. $className = __NAMESPACE__ . '\\EvalBase';
  183. $metadata = $this->createClassMetadata($className, array('id'));
  184. $proxyGenerator->generateProxyClass($metadata);
  185. $reflClass = new ReflectionClass('Doctrine\Tests\Common\ProxyProxy\__CG__\Doctrine\Tests\Common\Proxy\EvalBase');
  186. $this->assertContains("eval()'d code", $reflClass->getFileName());
  187. }
  188. private function createClassMetadata($className, array $ids)
  189. {
  190. $metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
  191. $reflClass = new ReflectionClass($className);
  192. $metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue($reflClass));
  193. $metadata->expects($this->any())->method('getIdentifierFieldNames')->will($this->returnValue($ids));
  194. $metadata->expects($this->any())->method('getName')->will($this->returnValue($className));
  195. return $metadata;
  196. }
  197. }
  198. class EvalBase
  199. {
  200. }