RuntimePublicReflectionPropertyTest.php in Plug 7
File
lib/doctrine/common/tests/Doctrine/Tests/Common/Reflection/RuntimePublicReflectionPropertyTest.php
View source
<?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);
}
}
class RuntimePublicReflectionPropertyTestProxyMock implements Proxy {
private $initializer = null;
private $initialized = false;
public $checkedProperty = 'testValue';
public function __getInitializer() {
return $this->initializer;
}
public function __setInitializer(\Closure $initializer = null) {
$this->initializer = $initializer;
}
public function __getLazyProperties() {
}
public function __load() {
}
public function __isInitialized() {
return $this->initialized;
}
public function __setInitialized($initialized) {
$this->initialized = (bool) $initialized;
}
public function __get($name) {
if ($this->initializer) {
$cb = $this->initializer;
$cb();
}
return $this->checkedProperty;
}
public function __set($name, $value) {
if ($this->initializer) {
$cb = $this->initializer;
$cb();
}
$this->checkedProperty = $value;
}
public function __isset($name) {
if ($this->initializer) {
$cb = $this->initializer;
$cb();
}
return isset($this->checkedProperty);
}
public function __setCloner(\Closure $cloner = null) {
}
public function __getCloner() {
}
}