diff --git a/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php new file mode 100644 index 000000000..827da2e14 --- /dev/null +++ b/src/ProxyManager/Generator/Util/ClassGeneratorUtils.php @@ -0,0 +1,53 @@ + + * @license MIT + */ +final class ClassGeneratorUtils +{ + /** + * @param ReflectionClass $originalClass + * @param GeneratorClass $classGenerator + * @param MethodGenerator $generatedMethod + * + * @return void|false + */ + public static function addMethodIfNotFinal( + ReflectionClass $originalClass, + GeneratorClass $classGenerator, + MethodGenerator $generatedMethod + ) { + $methodName = $generatedMethod->getName(); + + if ($originalClass->hasMethod($methodName) && $originalClass->getMethod($methodName)->isFinal()) { + return false; + } + + $classGenerator->addMethodFromGenerator($generatedMethod); + } +} diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php index e427b1cd4..c9ecd127f 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizerGenerator.php @@ -19,6 +19,7 @@ namespace ProxyManager\ProxyGenerator; use ProxyManager\Exception\InvalidProxiedClassException; +use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor; use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors; @@ -32,7 +33,9 @@ use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicUnset; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; use ReflectionClass; +use ReflectionMethod; use Zend\Code\Generator\ClassGenerator; +use Zend\Code\Generator\MethodGenerator; use Zend\Code\Reflection\MethodReflection; /** @@ -60,43 +63,36 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors()); $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodPrefixInterceptors()); - $methods = ProxiedMethodsFilter::getProxiedMethods( - $originalClass, - array('__get', '__set', '__isset', '__unset', '__clone', '__sleep') - ); - - foreach ($methods as $method) { - $classGenerator->addMethodFromGenerator( - InterceptedMethod::generateMethod( - new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), - $prefixInterceptors, - $suffixInterceptors + array_map( + function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { + ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); + }, + array_merge( + array_map( + function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptors) { + return InterceptedMethod::generateMethod( + new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), + $prefixInterceptors, + $suffixInterceptors + ); + }, + ProxiedMethodsFilter::getProxiedMethods( + $originalClass, + array('__get', '__set', '__isset', '__unset', '__clone', '__sleep') + ) + ), + array( + new Constructor($originalClass, $prefixInterceptors, $suffixInterceptors), + new SetMethodPrefixInterceptor($prefixInterceptors), + new SetMethodSuffixInterceptor($suffixInterceptors), + new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors), + new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors), + new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors), + new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors), + new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors), + new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors), ) - ); - } - - $classGenerator->addMethodFromGenerator( - new Constructor($originalClass, $prefixInterceptors, $suffixInterceptors) - ); - $classGenerator->addMethodFromGenerator(new SetMethodPrefixInterceptor($prefixInterceptors)); - $classGenerator->addMethodFromGenerator(new SetMethodSuffixInterceptor($suffixInterceptors)); - $classGenerator->addMethodFromGenerator( - new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors) - ); - $classGenerator->addMethodFromGenerator( - new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors) - ); - $classGenerator->addMethodFromGenerator( - new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors) - ); - $classGenerator->addMethodFromGenerator( - new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors) - ); - $classGenerator->addMethodFromGenerator( - new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors) - ); - $classGenerator->addMethodFromGenerator( - new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors) + ) ); } } diff --git a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php index 9abed74b6..8b6a3aafd 100644 --- a/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/AccessInterceptorValueHolderGenerator.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor; use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor; @@ -36,7 +37,9 @@ use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue; use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\MagicSleep; use ReflectionClass; +use ReflectionMethod; use Zend\Code\Generator\ClassGenerator; +use Zend\Code\Generator\MethodGenerator; use Zend\Code\Reflection\MethodReflection; /** @@ -73,54 +76,60 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors()); $classGenerator->addPropertyFromGenerator($publicProperties); - foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) { - $classGenerator->addMethodFromGenerator( - InterceptedMethod::generateMethod( - new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), - $valueHolder, - $prefixInterceptors, - $suffixInterceptors + array_map( + function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator, $valueHolder) { + ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); + }, + array_merge( + array_map( + function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptors, $valueHolder) { + return InterceptedMethod::generateMethod( + new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), + $valueHolder, + $prefixInterceptors, + $suffixInterceptors + ); + }, + ProxiedMethodsFilter::getProxiedMethods($originalClass) + ), + array( + new Constructor($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors), + new GetWrappedValueHolderValue($valueHolder), + new SetMethodPrefixInterceptor($prefixInterceptors), + new SetMethodSuffixInterceptor($suffixInterceptors), + new MagicGet( + $originalClass, + $valueHolder, + $prefixInterceptors, + $suffixInterceptors, + $publicProperties + ), + new MagicSet( + $originalClass, + $valueHolder, + $prefixInterceptors, + $suffixInterceptors, + $publicProperties + ), + new MagicIsset( + $originalClass, + $valueHolder, + $prefixInterceptors, + $suffixInterceptors, + $publicProperties + ), + new MagicUnset( + $originalClass, + $valueHolder, + $prefixInterceptors, + $suffixInterceptors, + $publicProperties + ), + new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors), + new MagicSleep($originalClass, $valueHolder), + new MagicWakeup($originalClass, $valueHolder), ) - ); - } - - $classGenerator->addMethodFromGenerator( - new Constructor($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors) - ); - $classGenerator->addMethodFromGenerator( - new GetWrappedValueHolderValue($valueHolder) - ); - $classGenerator->addMethodFromGenerator( - new SetMethodPrefixInterceptor($prefixInterceptors) - ); - $classGenerator->addMethodFromGenerator( - new SetMethodSuffixInterceptor($suffixInterceptors) - ); - - $classGenerator->addMethodFromGenerator( - new MagicGet($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties) - ); - - $classGenerator->addMethodFromGenerator( - new MagicSet($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties) - ); - - $classGenerator->addMethodFromGenerator( - new MagicIsset($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties) - ); - - $classGenerator->addMethodFromGenerator( - new MagicUnset($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties) - ); - - $classGenerator->addMethodFromGenerator( - new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors) - ); - $classGenerator->addMethodFromGenerator( - new MagicSleep($originalClass, $valueHolder) - ); - $classGenerator->addMethodFromGenerator( - new MagicWakeup($originalClass, $valueHolder) + ) ); } } diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php index 780c59b2b..2a6a0c92f 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer; use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\GetProxyInitializer; @@ -37,7 +38,9 @@ use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; use ReflectionClass; +use ReflectionMethod; use Zend\Code\Generator\ClassGenerator; +use Zend\Code\Generator\MethodGenerator; use Zend\Code\Reflection\MethodReflection; /** @@ -73,30 +76,36 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe $init = new CallInitializer($initializer, $publicPropsDefaults, $initializationTracker); - $classGenerator->addMethodFromGenerator($init); - - foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) { - $classGenerator->addMethodFromGenerator( - LazyLoadingMethodInterceptor::generateMethod( - new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), - $initializer, - $init + array_map( + function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { + ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); + }, + array_merge( + array_map( + function (ReflectionMethod $method) use ($initializer, $init) { + return LazyLoadingMethodInterceptor::generateMethod( + new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), + $initializer, + $init + ); + }, + ProxiedMethodsFilter::getProxiedMethods($originalClass) + ), + array( + $init, + new Constructor($originalClass, $initializer), + new MagicGet($originalClass, $initializer, $init, $publicProperties), + new MagicSet($originalClass, $initializer, $init, $publicProperties), + new MagicIsset($originalClass, $initializer, $init, $publicProperties), + new MagicUnset($originalClass, $initializer, $init, $publicProperties), + new MagicClone($originalClass, $initializer, $init, $publicProperties), + new MagicSleep($originalClass, $initializer, $init, $publicProperties), + new SetProxyInitializer($initializer), + new GetProxyInitializer($initializer), + new InitializeProxy($initializer, $init), + new IsProxyInitialized($initializer), ) - ); - } - - $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $initializer)); - - $classGenerator->addMethodFromGenerator(new MagicGet($originalClass, $initializer, $init, $publicProperties)); - $classGenerator->addMethodFromGenerator(new MagicSet($originalClass, $initializer, $init, $publicProperties)); - $classGenerator->addMethodFromGenerator(new MagicIsset($originalClass, $initializer, $init, $publicProperties)); - $classGenerator->addMethodFromGenerator(new MagicUnset($originalClass, $initializer, $init, $publicProperties)); - $classGenerator->addMethodFromGenerator(new MagicClone($originalClass, $initializer, $init)); - $classGenerator->addMethodFromGenerator(new MagicSleep($originalClass, $initializer, $init)); - - $classGenerator->addMethodFromGenerator(new SetProxyInitializer($initializer)); - $classGenerator->addMethodFromGenerator(new GetProxyInitializer($initializer)); - $classGenerator->addMethodFromGenerator(new InitializeProxy($initializer, $init)); - $classGenerator->addMethodFromGenerator(new IsProxyInitialized($initializer)); + ) + ); } } diff --git a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php index dc9cbce82..c80de7391 100644 --- a/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php +++ b/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolderGenerator.php @@ -36,8 +36,11 @@ use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue; +use ProxyManager\Generator\Util\ClassGeneratorUtils; use ReflectionClass; +use ReflectionMethod; use Zend\Code\Generator\ClassGenerator; +use Zend\Code\Generator\MethodGenerator; use Zend\Code\Reflection\MethodReflection; /** @@ -69,39 +72,37 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty()); $classGenerator->addPropertyFromGenerator($publicProperties); - foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) { - $classGenerator->addMethodFromGenerator( - LazyLoadingMethodInterceptor::generateMethod( - new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), - $initializer, - $valueHolder + array_map( + function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { + ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); + }, + array_merge( + array_map( + function (ReflectionMethod $method) use ($initializer, $valueHolder) { + return LazyLoadingMethodInterceptor::generateMethod( + new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), + $initializer, + $valueHolder + ); + }, + ProxiedMethodsFilter::getProxiedMethods($originalClass) + ), + array( + new Constructor($originalClass, $initializer), + new MagicGet($originalClass, $initializer, $valueHolder, $publicProperties), + new MagicSet($originalClass, $initializer, $valueHolder, $publicProperties), + new MagicIsset($originalClass, $initializer, $valueHolder, $publicProperties), + new MagicUnset($originalClass, $initializer, $valueHolder, $publicProperties), + new MagicClone($originalClass, $initializer, $valueHolder), + new MagicSleep($originalClass, $initializer, $valueHolder), + new MagicWakeup($originalClass), + new SetProxyInitializer($initializer), + new GetProxyInitializer($initializer), + new InitializeProxy($initializer, $valueHolder), + new IsProxyInitialized($valueHolder), + new GetWrappedValueHolderValue($valueHolder), ) - ); - } - - $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $initializer)); - - $classGenerator->addMethodFromGenerator( - new MagicGet($originalClass, $initializer, $valueHolder, $publicProperties) - ); - $classGenerator->addMethodFromGenerator( - new MagicSet($originalClass, $initializer, $valueHolder, $publicProperties) + ) ); - $classGenerator->addMethodFromGenerator( - new MagicIsset($originalClass, $initializer, $valueHolder, $publicProperties) - ); - $classGenerator->addMethodFromGenerator( - new MagicUnset($originalClass, $initializer, $valueHolder, $publicProperties) - ); - $classGenerator->addMethodFromGenerator(new MagicClone($originalClass, $initializer, $valueHolder)); - $classGenerator->addMethodFromGenerator(new MagicSleep($originalClass, $initializer, $valueHolder)); - $classGenerator->addMethodFromGenerator(new MagicWakeup($originalClass)); - - $classGenerator->addMethodFromGenerator(new SetProxyInitializer($initializer)); - $classGenerator->addMethodFromGenerator(new GetProxyInitializer($initializer)); - $classGenerator->addMethodFromGenerator(new InitializeProxy($initializer, $valueHolder)); - $classGenerator->addMethodFromGenerator(new IsProxyInitialized($valueHolder)); - - $classGenerator->addMethodFromGenerator(new GetWrappedValueHolderValue($valueHolder)); } } diff --git a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php index fb22f07e3..20437c45d 100644 --- a/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/NullObjectGenerator.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\NullObject\MethodGenerator\NullObjectMethodInterceptor; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; @@ -60,6 +61,6 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe ); } - $classGenerator->addMethodFromGenerator(new Constructor($originalClass)); + ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, new Constructor($originalClass)); } } diff --git a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php index ac68d39ea..19352e73e 100644 --- a/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php +++ b/src/ProxyManager/ProxyGenerator/RemoteObjectGenerator.php @@ -18,6 +18,7 @@ namespace ProxyManager\ProxyGenerator; +use ProxyManager\Generator\Util\ClassGeneratorUtils; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\Constructor; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicGet; use ProxyManager\ProxyGenerator\RemoteObject\MethodGenerator\MagicIsset; @@ -27,7 +28,9 @@ use ProxyManager\ProxyGenerator\RemoteObject\PropertyGenerator\AdapterProperty; use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter; use ReflectionClass; +use ReflectionMethod; use Zend\Code\Generator\ClassGenerator; +use Zend\Code\Generator\MethodGenerator; use Zend\Code\Reflection\MethodReflection; /** @@ -56,28 +59,32 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe $classGenerator->setImplementedInterfaces($interfaces); $classGenerator->addPropertyFromGenerator($adapter = new AdapterProperty()); - $methods = ProxiedMethodsFilter::getProxiedMethods( - $originalClass, - array('__get', '__set', '__isset', '__unset') - ); - - foreach ($methods as $method) { - $classGenerator->addMethodFromGenerator( - RemoteObjectMethod::generateMethod( - new MethodReflection( - $method->getDeclaringClass()->getName(), - $method->getName() - ), - $adapter, - $originalClass + array_map( + function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) { + ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod); + }, + array_merge( + array_map( + function (ReflectionMethod $method) use ($adapter, $originalClass) { + return RemoteObjectMethod::generateMethod( + new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()), + $adapter, + $originalClass + ); + }, + ProxiedMethodsFilter::getProxiedMethods( + $originalClass, + array('__get', '__set', '__isset', '__unset') + ) + ), + array( + new Constructor($originalClass, $adapter), + new MagicGet($originalClass, $adapter), + new MagicSet($originalClass, $adapter), + new MagicIsset($originalClass, $adapter), + new MagicUnset($originalClass, $adapter), ) - ); - } - - $classGenerator->addMethodFromGenerator(new Constructor($originalClass, $adapter)); - $classGenerator->addMethodFromGenerator(new MagicGet($originalClass, $adapter)); - $classGenerator->addMethodFromGenerator(new MagicSet($originalClass, $adapter)); - $classGenerator->addMethodFromGenerator(new MagicIsset($originalClass, $adapter)); - $classGenerator->addMethodFromGenerator(new MagicUnset($originalClass, $adapter)); + ) + ); } } diff --git a/tests/ProxyManagerTest/Functional/MultipleProxyGenerationTest.php b/tests/ProxyManagerTest/Functional/MultipleProxyGenerationTest.php index d3b9d602c..00be47dd4 100644 --- a/tests/ProxyManagerTest/Functional/MultipleProxyGenerationTest.php +++ b/tests/ProxyManagerTest/Functional/MultipleProxyGenerationTest.php @@ -102,6 +102,8 @@ public function getTestedClasses() $data = array( array('ProxyManagerTestAsset\\BaseClass'), array('ProxyManagerTestAsset\\ClassWithMagicMethods'), + array('ProxyManagerTestAsset\\ClassWithFinalMethods'), + array('ProxyManagerTestAsset\\ClassWithFinalMagicMethods'), array('ProxyManagerTestAsset\\ClassWithByRefMagicMethods'), array('ProxyManagerTestAsset\\ClassWithMixedProperties'), array('ProxyManagerTestAsset\\ClassWithPrivateProperties'), diff --git a/tests/ProxyManagerTest/Generator/Util/ClassGeneratorUtilsTest.php b/tests/ProxyManagerTest/Generator/Util/ClassGeneratorUtilsTest.php new file mode 100644 index 000000000..cf5254655 --- /dev/null +++ b/tests/ProxyManagerTest/Generator/Util/ClassGeneratorUtilsTest.php @@ -0,0 +1,72 @@ + + * @license MIT + * + * @covers ProxyManager\Generator\Util\ClassGeneratorUtils + */ +class ClassGeneratorUtilsTest extends PHPUnit_Framework_TestCase +{ + public function testCantAddAFinalMethod() + { + $classGenerator = $this->getMock('Zend\\Code\\Generator\\ClassGenerator'); + $methodGenerator = $this->getMock('Zend\\Code\\Generator\\MethodGenerator'); + + $methodGenerator + ->expects($this->once()) + ->method('getName') + ->willReturn('foo'); + + $classGenerator + ->expects($this->never()) + ->method('addMethodFromGenerator'); + + $reflection = new ReflectionClass('ProxyManagerTestAsset\\ClassWithFinalMethods'); + + ClassGeneratorUtils::addMethodIfNotFinal($reflection, $classGenerator, $methodGenerator); + } + + public function testCanAddANotFinalMethod() + { + $classGenerator = $this->getMock('Zend\\Code\\Generator\\ClassGenerator'); + $methodGenerator = $this->getMock('Zend\\Code\\Generator\\MethodGenerator'); + + $methodGenerator + ->expects($this->once()) + ->method('getName') + ->willReturn('publicMethod'); + + $classGenerator + ->expects($this->once()) + ->method('addMethodFromGenerator'); + + $reflection = new ReflectionClass('ProxyManagerTestAsset\\BaseClass'); + + ClassGeneratorUtils::addMethodIfNotFinal($reflection, $classGenerator, $methodGenerator); + } +} diff --git a/tests/ProxyManagerTestAsset/ClassWithFinalMagicMethods.php b/tests/ProxyManagerTestAsset/ClassWithFinalMagicMethods.php new file mode 100644 index 000000000..d42204b77 --- /dev/null +++ b/tests/ProxyManagerTestAsset/ClassWithFinalMagicMethods.php @@ -0,0 +1,88 @@ + + * @license MIT + */ +class ClassWithFinalMagicMethods +{ + /** + * {@inheritDoc} + */ + final public function __construct() + { + } + + /** + * {@inheritDoc} + */ + final public function __set($name, $value) + { + return array($name => $value); + } + + /** + * {@inheritDoc} + */ + final public function __get($name) + { + return $name; + } + + /** + * {@inheritDoc} + */ + final public function __isset($name) + { + return (bool) $name; + } + + /** + * {@inheritDoc} + */ + final public function __unset($name) + { + return (bool) $name; + } + + /** + * {@inheritDoc} + */ + final public function __sleep() + { + } + + /** + * {@inheritDoc} + */ + final public function __wakeup() + { + } + + /** + * {@inheritDoc} + */ + final public function __clone() + { + } +} diff --git a/tests/ProxyManagerTestAsset/ClassWithFinalMethods.php b/tests/ProxyManagerTestAsset/ClassWithFinalMethods.php new file mode 100644 index 000000000..a7c0154ba --- /dev/null +++ b/tests/ProxyManagerTestAsset/ClassWithFinalMethods.php @@ -0,0 +1,42 @@ + + * @license MIT + */ +class ClassWithFinalMethods extends PHPUnit_Framework_TestCase +{ + final public function foo() + { + } + + final private function bar() + { + } + + final protected function baz() + { + } +}