* @license MIT * * @group Functional * @coversNothing */ class NullObjectFunctionalTest extends PHPUnit_Framework_TestCase { /** * @dataProvider getProxyMethods */ public function testMethodCalls($className, $instance, $method, $params, $expectedValue) { $proxyName = $this->generateProxy($className); /* @var $proxy \ProxyManager\Proxy\NullObjectInterface */ $proxy = new $proxyName(); $this->assertSame(null, call_user_func_array(array($proxy, $method), $params)); } /** * @dataProvider getProxyMethods */ public function testMethodCallsAfterUnSerialization($className, $instance, $method, $params, $expectedValue) { $proxyName = $this->generateProxy($className); /* @var $proxy \ProxyManager\Proxy\NullObjectInterface */ $proxy = unserialize(serialize(new $proxyName())); $this->assertSame(null, call_user_func_array(array($proxy, $method), $params)); } /** * @dataProvider getProxyMethods */ public function testMethodCallsAfterCloning($className, $instance, $method, $params, $expectedValue) { $proxyName = $this->generateProxy($className); /* @var $proxy \ProxyManager\Proxy\NullObjectInterface */ $proxy = new $proxyName(); $cloned = clone $proxy; $this->assertSame(null, call_user_func_array(array($cloned, $method), $params)); } /** * @dataProvider getPropertyAccessProxies */ public function testPropertyReadAccess($instance, $proxy, $publicProperty, $propertyValue) { /* @var $proxy \ProxyManager\Proxy\NullObjectInterface */ $this->assertSame(null, $proxy->$publicProperty); } /** * @dataProvider getPropertyAccessProxies */ public function testPropertyWriteAccess($instance, $proxy, $publicProperty) { /* @var $proxy \ProxyManager\Proxy\NullObjectInterface */ $newValue = uniqid(); $proxy->$publicProperty = $newValue; $this->assertSame($newValue, $proxy->$publicProperty); } /** * @dataProvider getPropertyAccessProxies */ public function testPropertyExistence($instance, $proxy, $publicProperty) { /* @var $proxy \ProxyManager\Proxy\NullObjectInterface */ $this->assertSame(null, $proxy->$publicProperty); } /** * @dataProvider getPropertyAccessProxies */ public function testPropertyUnset($instance, $proxy, $publicProperty) { /* @var $proxy \ProxyManager\Proxy\NullObjectInterface */ unset($proxy->$publicProperty); $this->assertTrue(isset($instance->$publicProperty)); $this->assertFalse(isset($proxy->$publicProperty)); } /** * Generates a proxy for the given class name, and retrieves its class name * * @param string $parentClassName * * @return string */ private function generateProxy($parentClassName) { $generatedClassName = __NAMESPACE__ . '\\' . UniqueIdentifierGenerator::getIdentifier('Foo'); $generator = new NullObjectGenerator(); $generatedClass = new ClassGenerator($generatedClassName); $strategy = new EvaluatingGeneratorStrategy(); $generator->generate(new ReflectionClass($parentClassName), $generatedClass); $strategy->generate($generatedClass); return $generatedClassName; } /** * Generates a list of object | invoked method | parameters | expected result * * @return array */ public function getProxyMethods() { $selfHintParam = new ClassWithSelfHint(); $data = array( array( 'ProxyManagerTestAsset\\BaseClass', new BaseClass(), 'publicMethod', array(), 'publicMethodDefault' ), array( 'ProxyManagerTestAsset\\BaseClass', new BaseClass(), 'publicTypeHintedMethod', array('param' => new \stdClass()), 'publicTypeHintedMethodDefault' ), array( 'ProxyManagerTestAsset\\BaseClass', new BaseClass(), 'publicByReferenceMethod', array(), 'publicByReferenceMethodDefault' ), array( 'ProxyManagerTestAsset\\BaseInterface', new BaseClass(), 'publicMethod', array(), 'publicMethodDefault' ), ); if (PHP_VERSION_ID >= 50401) { // PHP < 5.4.1 misbehaves, throwing strict standards, see https://bugs.php.net/bug.php?id=60573 $data[] = array( 'ProxyManagerTestAsset\\ClassWithSelfHint', new ClassWithSelfHint(), 'selfHintMethod', array('parameter' => $selfHintParam), $selfHintParam ); } return $data; } /** * Generates proxies and instances with a public property to feed to the property accessor methods * * @return array */ public function getPropertyAccessProxies() { $instance1 = new BaseClass(); $proxyName1 = $this->generateProxy(get_class($instance1)); $instance2 = new BaseClass(); $proxyName2 = $this->generateProxy(get_class($instance2)); return array( array( $instance1, new $proxyName1($instance1), 'publicProperty', 'publicPropertyDefault', ), array( $instance2, unserialize(serialize(new $proxyName2($instance2))), 'publicProperty', 'publicPropertyDefault', ), ); } }