123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace Doctrine\Tests\Common\Reflection;
- use PHPUnit_Framework_TestCase;
- use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
- use Doctrine\Common\Proxy\Proxy;
- class RuntimePublicReflectionPropertyTest extends PHPUnit_Framework_TestCase
- {
- public function testGetValueOnProxyPublicProperty()
- {
- $getCheckMock = $this->getMock('stdClass', array('callGet'));
- $getCheckMock->expects($this->never())->method('callGet');
- $initializer = function () use ($getCheckMock) {
- call_user_func($getCheckMock);
- };
- $mockProxy = new RuntimePublicReflectionPropertyTestProxyMock();
- $mockProxy->__setInitializer($initializer);
- $reflProperty = new RuntimePublicReflectionProperty(
- __NAMESPACE__ . '\RuntimePublicReflectionPropertyTestProxyMock',
- 'checkedProperty'
- );
- $this->assertSame('testValue', $reflProperty->getValue($mockProxy));
- unset($mockProxy->checkedProperty);
- $this->assertNull($reflProperty->getValue($mockProxy));
- }
- public function testSetValueOnProxyPublicProperty()
- {
- $setCheckMock = $this->getMock('stdClass', array('neverCallSet'));
- $setCheckMock->expects($this->never())->method('neverCallSet');
- $initializer = function () use ($setCheckMock) {
- call_user_func(array($setCheckMock, 'neverCallSet'));
- };
- $mockProxy = new RuntimePublicReflectionPropertyTestProxyMock();
- $mockProxy->__setInitializer($initializer);
- $reflProperty = new RuntimePublicReflectionProperty(
- __NAMESPACE__ . '\RuntimePublicReflectionPropertyTestProxyMock',
- 'checkedProperty'
- );
- $reflProperty->setValue($mockProxy, 'newValue');
- $this->assertSame('newValue', $mockProxy->checkedProperty);
- unset($mockProxy->checkedProperty);
- $reflProperty->setValue($mockProxy, 'otherNewValue');
- $this->assertSame('otherNewValue', $mockProxy->checkedProperty);
- $setCheckMock = $this->getMock('stdClass', array('callSet'));
- $setCheckMock->expects($this->once())->method('callSet');
- $initializer = function () use ($setCheckMock) {
- call_user_func(array($setCheckMock, 'callSet'));
- };
- $mockProxy->__setInitializer($initializer);
- $mockProxy->__setInitialized(true);
- unset($mockProxy->checkedProperty);
- $reflProperty->setValue($mockProxy, 'againNewValue');
- $this->assertSame('againNewValue', $mockProxy->checkedProperty);
- }
- }
- /**
- * Mock that simulates proxy public property lazy loading
- */
- class RuntimePublicReflectionPropertyTestProxyMock implements Proxy
- {
- /**
- * @var \Closure|null
- */
- private $initializer = null;
- /**
- * @var \Closure|null
- */
- private $initialized = false;
- /**
- * @var string
- */
- public $checkedProperty = 'testValue';
- /**
- * {@inheritDoc}
- */
- public function __getInitializer()
- {
- return $this->initializer;
- }
- /**
- * {@inheritDoc}
- */
- public function __setInitializer(\Closure $initializer = null)
- {
- $this->initializer = $initializer;
- }
- /**
- * {@inheritDoc}
- */
- public function __getLazyProperties()
- {
- }
- /**
- * {@inheritDoc}
- */
- public function __load()
- {
- }
- /**
- * {@inheritDoc}
- */
- public function __isInitialized()
- {
- return $this->initialized;
- }
- /**
- * {@inheritDoc}
- */
- public function __setInitialized($initialized)
- {
- $this->initialized = (bool) $initialized;
- }
- /**
- * @param string $name
- */
- public function __get($name)
- {
- if ($this->initializer) {
- $cb = $this->initializer;
- $cb();
- }
- return $this->checkedProperty;
- }
- /**
- * @param string $name
- * @param mixed $value
- */
- public function __set($name, $value)
- {
- if ($this->initializer) {
- $cb = $this->initializer;
- $cb();
- }
- // triggers notices if `$name` is used: see https://bugs.php.net/bug.php?id=63463
- $this->checkedProperty = $value;
- }
- /**
- * @param string $name
- *
- * @return integer
- */
- public function __isset($name)
- {
- if ($this->initializer) {
- $cb = $this->initializer;
- $cb();
- }
- return isset($this->checkedProperty);
- }
- /**
- * {@inheritDoc}
- */
- public function __setCloner(\Closure $cloner = null)
- {
- }
- /**
- * {@inheritDoc}
- */
- public function __getCloner()
- {
- }
- }
|