RuntimePublicReflectionPropertyTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Doctrine\Tests\Common\Reflection;
  3. use PHPUnit_Framework_TestCase;
  4. use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
  5. use Doctrine\Common\Proxy\Proxy;
  6. class RuntimePublicReflectionPropertyTest extends PHPUnit_Framework_TestCase
  7. {
  8. public function testGetValueOnProxyPublicProperty()
  9. {
  10. $getCheckMock = $this->getMock('stdClass', array('callGet'));
  11. $getCheckMock->expects($this->never())->method('callGet');
  12. $initializer = function () use ($getCheckMock) {
  13. call_user_func($getCheckMock);
  14. };
  15. $mockProxy = new RuntimePublicReflectionPropertyTestProxyMock();
  16. $mockProxy->__setInitializer($initializer);
  17. $reflProperty = new RuntimePublicReflectionProperty(
  18. __NAMESPACE__ . '\RuntimePublicReflectionPropertyTestProxyMock',
  19. 'checkedProperty'
  20. );
  21. $this->assertSame('testValue', $reflProperty->getValue($mockProxy));
  22. unset($mockProxy->checkedProperty);
  23. $this->assertNull($reflProperty->getValue($mockProxy));
  24. }
  25. public function testSetValueOnProxyPublicProperty()
  26. {
  27. $setCheckMock = $this->getMock('stdClass', array('neverCallSet'));
  28. $setCheckMock->expects($this->never())->method('neverCallSet');
  29. $initializer = function () use ($setCheckMock) {
  30. call_user_func(array($setCheckMock, 'neverCallSet'));
  31. };
  32. $mockProxy = new RuntimePublicReflectionPropertyTestProxyMock();
  33. $mockProxy->__setInitializer($initializer);
  34. $reflProperty = new RuntimePublicReflectionProperty(
  35. __NAMESPACE__ . '\RuntimePublicReflectionPropertyTestProxyMock',
  36. 'checkedProperty'
  37. );
  38. $reflProperty->setValue($mockProxy, 'newValue');
  39. $this->assertSame('newValue', $mockProxy->checkedProperty);
  40. unset($mockProxy->checkedProperty);
  41. $reflProperty->setValue($mockProxy, 'otherNewValue');
  42. $this->assertSame('otherNewValue', $mockProxy->checkedProperty);
  43. $setCheckMock = $this->getMock('stdClass', array('callSet'));
  44. $setCheckMock->expects($this->once())->method('callSet');
  45. $initializer = function () use ($setCheckMock) {
  46. call_user_func(array($setCheckMock, 'callSet'));
  47. };
  48. $mockProxy->__setInitializer($initializer);
  49. $mockProxy->__setInitialized(true);
  50. unset($mockProxy->checkedProperty);
  51. $reflProperty->setValue($mockProxy, 'againNewValue');
  52. $this->assertSame('againNewValue', $mockProxy->checkedProperty);
  53. }
  54. }
  55. /**
  56. * Mock that simulates proxy public property lazy loading
  57. */
  58. class RuntimePublicReflectionPropertyTestProxyMock implements Proxy
  59. {
  60. /**
  61. * @var \Closure|null
  62. */
  63. private $initializer = null;
  64. /**
  65. * @var \Closure|null
  66. */
  67. private $initialized = false;
  68. /**
  69. * @var string
  70. */
  71. public $checkedProperty = 'testValue';
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function __getInitializer()
  76. {
  77. return $this->initializer;
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. public function __setInitializer(\Closure $initializer = null)
  83. {
  84. $this->initializer = $initializer;
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public function __getLazyProperties()
  90. {
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. public function __load()
  96. {
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function __isInitialized()
  102. {
  103. return $this->initialized;
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public function __setInitialized($initialized)
  109. {
  110. $this->initialized = (bool) $initialized;
  111. }
  112. /**
  113. * @param string $name
  114. */
  115. public function __get($name)
  116. {
  117. if ($this->initializer) {
  118. $cb = $this->initializer;
  119. $cb();
  120. }
  121. return $this->checkedProperty;
  122. }
  123. /**
  124. * @param string $name
  125. * @param mixed $value
  126. */
  127. public function __set($name, $value)
  128. {
  129. if ($this->initializer) {
  130. $cb = $this->initializer;
  131. $cb();
  132. }
  133. // triggers notices if `$name` is used: see https://bugs.php.net/bug.php?id=63463
  134. $this->checkedProperty = $value;
  135. }
  136. /**
  137. * @param string $name
  138. *
  139. * @return integer
  140. */
  141. public function __isset($name)
  142. {
  143. if ($this->initializer) {
  144. $cb = $this->initializer;
  145. $cb();
  146. }
  147. return isset($this->checkedProperty);
  148. }
  149. /**
  150. * {@inheritDoc}
  151. */
  152. public function __setCloner(\Closure $cloner = null)
  153. {
  154. }
  155. /**
  156. * {@inheritDoc}
  157. */
  158. public function __getCloner()
  159. {
  160. }
  161. }