PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/nattaphat/hgis
PHP | 285 lines | 196 code | 50 blank | 39 comment | 3 complexity | 53ffb755a3cd1575b473a62ba12607ad 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 Doctrine\Common\Proxy\Proxy;
  22. use Doctrine\Common\Proxy\Exception\UnexpectedValueException;
  23. use PHPUnit_Framework_TestCase;
  24. use ReflectionClass;
  25. /**
  26. * Test for behavior of proxies with inherited magic methods
  27. *
  28. * @author Marco Pivetta <ocramius@gmail.com>
  29. */
  30. class ProxyMagicMethodsTest extends PHPUnit_Framework_TestCase
  31. {
  32. /**
  33. * @var \Doctrine\Common\Proxy\ProxyGenerator
  34. */
  35. protected $proxyGenerator;
  36. /**
  37. * @var LazyLoadableObject|Proxy
  38. */
  39. protected $lazyObject;
  40. protected $identifier = array(
  41. 'publicIdentifierField' => 'publicIdentifierFieldValue',
  42. 'protectedIdentifierField' => 'protectedIdentifierFieldValue',
  43. );
  44. /**
  45. * @var \PHPUnit_Framework_MockObject_MockObject|Callable
  46. */
  47. protected $initializerCallbackMock;
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function setUp()
  52. {
  53. $this->proxyGenerator = new ProxyGenerator(__DIR__ . '/generated', __NAMESPACE__ . '\\MagicMethodProxy');
  54. }
  55. public static function tearDownAfterClass()
  56. {
  57. }
  58. public function testInheritedMagicGet()
  59. {
  60. $proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicGetClass');
  61. $proxy = new $proxyClassName(
  62. function (Proxy $proxy, $method, $params) use (&$counter) {
  63. if ( ! in_array($params[0], array('publicField', 'test', 'notDefined'))) {
  64. throw new \InvalidArgumentException('Unexpected access to field "' . $params[0] . '"');
  65. }
  66. $initializer = $proxy->__getInitializer();
  67. $proxy->__setInitializer(null);
  68. $proxy->publicField = 'modifiedPublicField';
  69. $counter += 1;
  70. $proxy->__setInitializer($initializer);
  71. }
  72. );
  73. $this->assertSame('id', $proxy->id);
  74. $this->assertSame('modifiedPublicField', $proxy->publicField);
  75. $this->assertSame('test', $proxy->test);
  76. $this->assertSame('not defined', $proxy->notDefined);
  77. $this->assertSame(3, $counter);
  78. }
  79. public function testInheritedMagicSet()
  80. {
  81. $proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicSetClass');
  82. $proxy = new $proxyClassName(
  83. function (Proxy $proxy, $method, $params) use (&$counter) {
  84. if ( ! in_array($params[0], array('publicField', 'test', 'notDefined'))) {
  85. throw new \InvalidArgumentException('Unexpected access to field "' . $params[0] . '"');
  86. }
  87. $counter += 1;
  88. }
  89. );
  90. $this->assertSame('id', $proxy->id);
  91. $proxy->publicField = 'publicFieldValue';
  92. $this->assertSame('publicFieldValue', $proxy->publicField);
  93. $proxy->test = 'testValue';
  94. $this->assertSame('testValue', $proxy->testAttribute);
  95. $proxy->notDefined = 'not defined';
  96. $this->assertSame('not defined', $proxy->testAttribute);
  97. $this->assertSame(3, $counter);
  98. }
  99. public function testInheritedMagicSleep()
  100. {
  101. $proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicSleepClass');
  102. $proxy = new $proxyClassName();
  103. $this->assertSame('defaultValue', $proxy->serializedField);
  104. $this->assertSame('defaultValue', $proxy->nonSerializedField);
  105. $proxy->serializedField = 'changedValue';
  106. $proxy->nonSerializedField = 'changedValue';
  107. $unserialized = unserialize(serialize($proxy));
  108. $this->assertSame('changedValue', $unserialized->serializedField);
  109. $this->assertSame('defaultValue', $unserialized->nonSerializedField, 'Field was not returned by "__sleep"');
  110. }
  111. public function testInheritedMagicWakeup()
  112. {
  113. $proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicWakeupClass');
  114. $proxy = new $proxyClassName();
  115. $this->assertSame('defaultValue', $proxy->wakeupValue);
  116. $proxy->wakeupValue = 'changedValue';
  117. $unserialized = unserialize(serialize($proxy));
  118. $this->assertSame('newWakeupValue', $unserialized->wakeupValue, '"__wakeup" was called');
  119. $unserialized->__setInitializer(function (Proxy $proxy) {
  120. $proxy->__setInitializer(null);
  121. $proxy->publicField = 'newPublicFieldValue';
  122. });
  123. $this->assertSame('newPublicFieldValue', $unserialized->publicField, 'Proxy can still be initialized');
  124. }
  125. public function testInheritedMagicIsset()
  126. {
  127. $proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicIssetClass');
  128. $proxy = new $proxyClassName(function (Proxy $proxy, $method, $params) use (&$counter) {
  129. if (in_array($params[0], array('publicField', 'test', 'nonExisting'))) {
  130. $initializer = $proxy->__getInitializer();
  131. $proxy->__setInitializer(null);
  132. $proxy->publicField = 'modifiedPublicField';
  133. $counter += 1;
  134. $proxy->__setInitializer($initializer);
  135. return;
  136. }
  137. throw new \InvalidArgumentException(
  138. sprintf('Should not be initialized when checking isset("%s")', $params[0])
  139. );
  140. });
  141. $this->assertTrue(isset($proxy->id));
  142. $this->assertTrue(isset($proxy->publicField));
  143. $this->assertTrue(isset($proxy->test));
  144. $this->assertFalse(isset($proxy->nonExisting));
  145. $this->assertSame(3, $counter);
  146. }
  147. public function testInheritedMagicClone()
  148. {
  149. $proxyClassName = $this->generateProxyClass(__NAMESPACE__ . '\\MagicCloneClass');
  150. $proxy = new $proxyClassName(
  151. null,
  152. function ($proxy) {
  153. $proxy->cloned = true;
  154. }
  155. );
  156. $cloned = clone $proxy;
  157. $this->assertSame('newClonedValue', $cloned->clonedValue);
  158. $this->assertFalse($proxy->cloned);
  159. $this->assertTrue($cloned->cloned);
  160. }
  161. /**
  162. * @param $className
  163. *
  164. * @return string
  165. */
  166. private function generateProxyClass($className)
  167. {
  168. $proxyClassName = 'Doctrine\\Tests\\Common\\Proxy\\MagicMethodProxy\\__CG__\\' . $className;
  169. if (class_exists($proxyClassName, false)) {
  170. return $proxyClassName;
  171. }
  172. $metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
  173. $metadata
  174. ->expects($this->any())
  175. ->method('getName')
  176. ->will($this->returnValue($className));
  177. $metadata
  178. ->expects($this->any())
  179. ->method('getIdentifier')
  180. ->will($this->returnValue(array('id')));
  181. $metadata
  182. ->expects($this->any())
  183. ->method('getReflectionClass')
  184. ->will($this->returnValue(new ReflectionClass($className)));
  185. $metadata
  186. ->expects($this->any())
  187. ->method('isIdentifier')
  188. ->will($this->returnCallback(function ($fieldName) {
  189. return 'id' === $fieldName;
  190. }));
  191. $metadata
  192. ->expects($this->any())
  193. ->method('hasField')
  194. ->will($this->returnCallback(function ($fieldName) {
  195. return in_array($fieldName, array('id', 'publicField'));
  196. }));
  197. $metadata
  198. ->expects($this->any())
  199. ->method('hasAssociation')
  200. ->will($this->returnValue(false));
  201. $metadata
  202. ->expects($this->any())
  203. ->method('getFieldNames')
  204. ->will($this->returnValue(array('id', 'publicField')));
  205. $metadata
  206. ->expects($this->any())
  207. ->method('getIdentifierFieldNames')
  208. ->will($this->returnValue(array('id')));
  209. $metadata
  210. ->expects($this->any())
  211. ->method('getAssociationNames')
  212. ->will($this->returnValue(array()));
  213. $metadata
  214. ->expects($this->any())
  215. ->method('getTypeOfField')
  216. ->will($this->returnValue('string'));
  217. $this->proxyGenerator->generateProxyClass($metadata);
  218. require_once $this->proxyGenerator->getProxyFileName($className);
  219. return $proxyClassName;
  220. }
  221. }