diff --git a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php index 7294097..93e9125 100644 --- a/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php +++ b/src/bitExpert/Disco/Proxy/Configuration/MethodGenerator/BeanMethod.php @@ -109,9 +109,17 @@ public static function generateMethod( $body .= $padding . ' $initializer = null;' . "\n"; $body .= $padding . ' $wrappedObject = parent::' . $methodName . '(' . $methodParamTpl . ');' . "\n"; $body .= $padding . ' $this->initializeBean($wrappedObject, "' . $methodName . '");' . "\n"; + $body .= $padding . ' if (!is_a($wrappedObject, \'' . $beanType . '\')) {' . "\n"; + $body .= $padding . ' throw new \\bitExpert\\Disco\\BeanException(sprintf(' . "\n"; + $body .= $padding . ' \'Bean "%s" has declared "%s" as return type but returned "%s"\',' . "\n"; + $body .= $padding . ' \'' . $originalMethod->getName() . '\',' . "\n"; + $body .= $padding . ' \'' . $beanType . '\',' . "\n"; + $body .= $padding . ' $wrappedObject ? get_class($wrappedObject) : \'null\'' . "\n"; + $body .= $padding . ' ));' . "\n"; + $body .= $padding . ' }'. "\n\n"; $body .= $padding . ' return true;' . "\n"; $body .= $padding . '};' . "\n\n"; - $body .= $padding . '$instance = $factory->createProxy("' . $beanType . '", $initializer);' . "\n"; + $body .= $padding . '$instance = $factory->createProxy("' . $beanType . '", $initializer);' . "\n\n"; } else { $innerpadding = $padding; if ($methodAnnotation->isSingleton()) { @@ -120,7 +128,15 @@ public static function generateMethod( } $body .= $innerpadding . '$instance = parent::' . $methodName . '(' . $methodParamTpl . ');' . "\n"; - $body .= $innerpadding . '$this->initializeBean($instance, "' . $methodName . '");' . "\n"; + $body .= $innerpadding . '$this->initializeBean($instance, "' . $methodName . '");' . "\n\n"; + $body .= $innerpadding . 'if (!is_a($instance, \'' . $beanType . '\')) {' . "\n"; + $body .= $innerpadding . ' throw new \\bitExpert\\Disco\\BeanException(sprintf(' . "\n"; + $body .= $innerpadding . ' \'Bean "%s" has declared "%s" as return type but returned "%s"\',' . "\n"; + $body .= $innerpadding . ' \'' . $originalMethod->getName() . '\',' . "\n"; + $body .= $innerpadding . ' \'' . $beanType . '\',' . "\n"; + $body .= $innerpadding . ' $instance ? get_class($instance) : \'null\'' . "\n"; + $body .= $innerpadding . ' ));' . "\n"; + $body .= $innerpadding . '}'. "\n\n"; if ($methodAnnotation->isSingleton()) { $body .= $padding . '}' . "\n"; diff --git a/tests/bitExpert/Disco/AnnotationBeanFactoryUnitTest.php b/tests/bitExpert/Disco/AnnotationBeanFactoryUnitTest.php index d167149..8f39f01 100644 --- a/tests/bitExpert/Disco/AnnotationBeanFactoryUnitTest.php +++ b/tests/bitExpert/Disco/AnnotationBeanFactoryUnitTest.php @@ -17,6 +17,7 @@ use bitExpert\Disco\Config\BeanConfigurationWithParameters; use bitExpert\Disco\Config\BeanConfigurationWithPostProcessor; use bitExpert\Disco\Config\BeanConfigurationWithProtectedMethod; +use bitExpert\Disco\Config\WrongReturnTypeConfiguration; use bitExpert\Disco\Helper\BeanFactoryAwareService; use bitExpert\Disco\Helper\MasterService; use bitExpert\Disco\Helper\SampleService; @@ -405,4 +406,54 @@ public function enablingProxyAutoloaderRegistersAdditionalAutoloader() count($autoloaderFunctionsBeforeBeanFactoryInit) + 1 === count($autoloaderFunctionsAfterBeanFactoryInit) ); } + + /** + * @test + * @expectedException \bitExpert\Disco\BeanException + */ + public function throwsExceptionIfTypeOfReturnedObjectIsNotExpectedOfNonLazyBean() + { + $this->beanFactory = new AnnotationBeanFactory(WrongReturnTypeConfiguration::class); + BeanFactoryRegistry::register($this->beanFactory); + + $this->beanFactory->get('nonLazyBeanReturningSomethingWrong'); + } + + /** + * @test + * @expectedException \bitExpert\Disco\BeanException + */ + public function throwsExceptionIfNonLazyBeanMethodDoesNotReturnAnything() + { + $this->beanFactory = new AnnotationBeanFactory(WrongReturnTypeConfiguration::class); + BeanFactoryRegistry::register($this->beanFactory); + + $this->beanFactory->get('nonLazyBeanNotReturningAnything'); + } + + /** + * @test + * @expectedException \bitExpert\Disco\BeanException + */ + public function throwsExceptionIfTypeOfReturnedObjectIsNotExpectedOfLazyBean() + { + $this->beanFactory = new AnnotationBeanFactory(WrongReturnTypeConfiguration::class); + BeanFactoryRegistry::register($this->beanFactory); + + $bean = $this->beanFactory->get('lazyBeanReturningSomethingWrong'); + $bean->setTest('test'); + } + + /** + * @test + * @expectedException \bitExpert\Disco\BeanException + */ + public function throwsExceptionIfLazyBeanMethodDoesNotReturnAnything() + { + $this->beanFactory = new AnnotationBeanFactory(WrongReturnTypeConfiguration::class); + BeanFactoryRegistry::register($this->beanFactory); + + $bean = $this->beanFactory->get('lazyBeanNotReturningAnything'); + $bean->setTest('test'); + } } diff --git a/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php b/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php new file mode 100644 index 0000000..418e6d1 --- /dev/null +++ b/tests/bitExpert/Disco/Config/WrongReturnTypeConfiguration.php @@ -0,0 +1,58 @@ +