/** * Creates a new reflection class instance from the passed PHP reflection class. * * @param \ReflectionClass $reflectionClass The PHP reflection class to load the data from * @param array $annotationsToIgnore An array with annotations names we want to ignore when loaded * @param array $annotationAliases An array with annotation aliases used when create annotation instances * * @return \AppserverIo\Lang\Reflection\ReflectionClass The instance */ public static function fromPhpReflectionClass(\ReflectionClass $reflectionClass, array $annotationsToIgnore = array(), array $annotationAliases = array()) { // initialize the array with the annotations we want to ignore $annotationsToIgnore = array_merge($annotationsToIgnore, array('author', 'package', 'license', 'copyright', 'param', 'return', 'throws', 'see', 'link')); // initialize the array with the aliases for the enterprise bean annotations $annotationAliases = array_merge(array(PersistenceUnit::ANNOTATION => PersistenceUnit::__getClass(), MessageDriven::ANNOTATION => MessageDriven::__getClass(), PostConstruct::ANNOTATION => PostConstruct::__getClass(), PreDestroy::ANNOTATION => PreDestroy::__getClass(), PostDetach::ANNOTATION => PostDetach::__getClass(), PreAttach::ANNOTATION => PreAttach::__getClass(), Schedule::ANNOTATION => Schedule::__getClass(), Singleton::ANNOTATION => Singleton::__getClass(), Startup::ANNOTATION => Startup::__getClass(), Stateful::ANNOTATION => Stateful::__getClass(), Stateless::ANNOTATION => Stateless::__getClass(), Timeout::ANNOTATION => Timeout::__getClass(), EnterpriseBean::ANNOTATION => EnterpriseBean::__getClass(), Resource::ANNOTATION => Resource::__getClass())); // create a new timed object instance return new TimedObject($reflectionClass->getName(), $annotationsToIgnore, $annotationAliases); }
/** * Test that initization with the fromReflectionMethod() method * and an empty annotation works as expected. * * @return void */ public function testFromReflectionMethodAndAnnotationWithAttributes() { // prepare the empty annotation values $values = array('name' => 'ReferenceToMyPersistenceUnit', 'unitName' => 'MyPersistenceUnit'); // create a mock annotation implementation $beanAnnotation = $this->getMockBuilder('AppserverIo\\Psr\\EnterpriseBeans\\Annotations\\PersistenceUnit')->setConstructorArgs(array('PersistenceUnit', $values))->getMockForAbstractClass(); // create a mock annotation $annotation = $this->getMockBuilder('AppserverIo\\Lang\\Reflection\\ReflectionAnnotation')->setMethods(array('getAnnotationName', 'getValues', 'newInstance'))->setConstructorArgs(array('PersistenceUnit', $values))->getMock(); // mock the ReflectionAnnotation methods $annotation->expects($this->once())->method('getAnnotationName')->will($this->returnValue('PersistenceUnit')); $annotation->expects($this->once())->method('getValues')->will($this->returnValue($values)); $annotation->expects($this->once())->method('newInstance')->will($this->returnValue($beanAnnotation)); // initialize the annotation aliases $aliases = array(PersistenceUnit::ANNOTATION => PersistenceUnit::__getClass()); // initialize the reflection method $reflectionMethod = $this->getMockBuilder('AppserverIo\\Lang\\Reflection\\ReflectionMethod')->setConstructorArgs(array(__CLASS__, array(), $aliases))->setMethods(array('hasAnnotation', 'getAnnotation', 'getClassName', 'getMethodName'))->getMock(); // mock the methods $reflectionMethod->expects($this->once())->method('hasAnnotation')->with(PersistenceUnit::ANNOTATION)->will($this->returnValue(true)); $reflectionMethod->expects($this->once())->method('getAnnotation')->with(PersistenceUnit::ANNOTATION)->will($this->returnValue($annotation)); $reflectionMethod->expects($this->once())->method('getClassName')->will($this->returnValue(__CLASS__)); $reflectionMethod->expects($this->once())->method('getMethodName')->will($this->returnValue('injectDummyPersistenceUnit')); // initialize the descriptor from the reflection method $this->descriptor->fromReflectionMethod($reflectionMethod); // check that the descriptor has been initialized successfully $this->assertSame('env/persistence/ReferenceToMyPersistenceUnit', $this->descriptor->getName()); $this->assertSame('MyPersistenceUnit', $this->descriptor->getUnitName()); }
/** * Returns a reflection class instance for the passed class name. * * @param string $className The class name to return the reflection instance for * * @return \AppserverIo\Lang\Reflection\ReflectionClass The reflection instance */ public function newReflectionClass($className) { // initialize the array with the annotations we want to ignore $annotationsToIgnore = array('author', 'package', 'license', 'copyright', 'param', 'return', 'throws', 'see', 'link'); // initialize the array with the aliases for the enterprise bean annotations $annotationAliases = array(Route::ANNOTATION => Route::__getClass(), Resource::ANNOTATION => Resource::__getClass(), Timeout::ANNOTATION => Timeout::__getClass(), Stateless::ANNOTATION => Stateless::__getClass(), Stateful::ANNOTATION => Stateful::__getClass(), Startup::ANNOTATION => Startup::__getClass(), Singleton::ANNOTATION => Singleton::__getClass(), Schedule::ANNOTATION => Schedule::__getClass(), PreAttach::ANNOTATION => PreAttach::__getClass(), PostDetach::ANNOTATION => PostDetach::__getClass(), PreDestroy::ANNOTATION => PreDestroy::__getClass(), PostConstruct::ANNOTATION => PostConstruct::__getClass(), MessageDriven::ANNOTATION => MessageDriven::__getClass(), EnterpriseBean::ANNOTATION => EnterpriseBean::__getClass(), PersistenceUnit::ANNOTATION => PersistenceUnit::__getClass()); // return the reflection class instance return new ReflectionClass($className, $annotationsToIgnore, $annotationAliases); }
/** * Tests if the deployment initialization from a reflection class with a bean annotation * without the name attribute works as expected. * * @return void */ public function testFromReflectionClassWithAnnotationWithoutNameAttribute() { // prepare the annotation values $values = array(); // create a mock annotation implementation $beanAnnotation = $this->getMockBuilder('AppserverIo\\Psr\\EnterpriseBeans\\Annotations\\AbstractBeanAnnotation')->setConstructorArgs(array('Stateless', $values))->getMockForAbstractClass(); // create a mock annotation $annotation = $this->getMockBuilder('AppserverIo\\Lang\\Reflection\\ReflectionAnnotation')->setMethods(array('getAnnotationName', 'getValues', 'newInstance'))->setConstructorArgs(array('Stateless', $values))->getMock(); // mock the ReflectionAnnotation methods $annotation->expects($this->once())->method('getAnnotationName')->will($this->returnValue('Stateless')); $annotation->expects($this->once())->method('getValues')->will($this->returnValue($values)); $annotation->expects($this->once())->method('newInstance')->will($this->returnValue($beanAnnotation)); // initialize the annotation aliases $aliases = array(Resource::ANNOTATION => Resource::__getClass(), EnterpriseBean::ANNOTATION => EnterpriseBean::__getClass(), PersistenceUnit::ANNOTATION => PersistenceUnit::__getClass()); // create a reflection class $reflectionClass = new ReflectionClass(__CLASS__, array(), $aliases); // mock the methods $this->descriptor->expects($this->once())->method('newAnnotationInstance')->with($reflectionClass)->will($this->returnValue($annotation)); // initialize the descriptor instance $this->descriptor->fromReflectionClass($reflectionClass); // check the name parsed from the reflection class $this->assertSame(__CLASS__, $this->descriptor->getClassName()); $this->assertSame('BeanDescriptorTest', $this->descriptor->getName()); $this->assertCount(1, $this->descriptor->getEpbReferences()); $this->assertCount(1, $this->descriptor->getResReferences()); $this->assertCount(1, $this->descriptor->getPersistenceUnitReferences()); $this->assertCount(3, $this->descriptor->getReferences()); }
/** * Tests if the deployment initialization from a reflection class with a bean annotation * containing the name attribute works as expected. * * @return void */ public function testFromReflectionClassWithAnnotationContainingNameAttribute() { // prepare the annotation values $values = array('name' => 'testServlet', 'displayName' => 'Test Servlet', 'description' => 'A test servlet implementation', 'urlPattern' => array('/annotated.do', '/annotated.do*'), 'initParams' => array('testParam' => 'testValue')); // create a mock annotation implementation $beanAnnotation = $this->getMockBuilder('AppserverIo\\Psr\\Servlet\\Annotations\\Route')->setConstructorArgs(array('Route', $values))->getMockForAbstractClass(); // create a mock annotation $annotation = $this->getMockBuilder('AppserverIo\\Lang\\Reflection\\ReflectionAnnotation')->setMethods(array('getAnnotationName', 'getValues', 'newInstance'))->setConstructorArgs(array('Route', $values))->getMock(); // mock the ReflectionAnnotation methods $annotation->expects($this->once())->method('getAnnotationName')->will($this->returnValue('Route')); $annotation->expects($this->once())->method('getValues')->will($this->returnValue($values)); $annotation->expects($this->once())->method('newInstance')->will($this->returnValue($beanAnnotation)); // initialize the annotation aliases $aliases = array(Route::ANNOTATION => Route::__getClass(), Resource::ANNOTATION => Resource::__getClass(), EnterpriseBean::ANNOTATION => EnterpriseBean::__getClass(), PersistenceUnit::ANNOTATION => PersistenceUnit::__getClass()); // create a servlet instance $servlet = $this->getMockBuilder('AppserverIo\\Psr\\Servlet\\ServletInterface')->setMethods(get_class_methods('AppserverIo\\Psr\\Servlet\\ServletInterface'))->getMock(); // create a PHP \ReflectionClass instance $phpReflectionClass = $this->getMockBuilder('\\ReflectionClass')->setMethods(array('isAbstract', 'isInterface'))->disableOriginalConstructor()->getMock(); // mock the methods $phpReflectionClass->expects($this->once())->method('isAbstract')->will($this->returnValue(false)); $phpReflectionClass->expects($this->once())->method('isInterface')->will($this->returnValue(false)); // create a ReflectionClass instance $reflectionClass = $this->getMockBuilder('AppserverIo\\Lang\\Reflection\\ReflectionClass')->setMethods(array('toPhpReflectionClass', 'implementsInterface', 'hasAnnotation', 'getAnnotation', 'getProperties', 'getMethods'))->setConstructorArgs(array($servlet, array(), $aliases))->getMock(); // initialize the mock ReflectionProperty instances $properties = array(new ReflectionProperty(__CLASS__, 'dummyResource', array(), $aliases), new ReflectionProperty(__CLASS__, 'dummyEnterpriseBean', array(), $aliases), new ReflectionProperty(__CLASS__, 'dummyPersistenceUnit', array(), $aliases)); // initialize the mock ReflectionMethod instances $methods = array(new ReflectionMethod(__CLASS__, 'injectDummyResource', array(), $aliases), new ReflectionMethod(__CLASS__, 'injectDummyEnterpriseBean', array(), $aliases), new ReflectionMethod(__CLASS__, 'injectDummyPersistenceUnit', array(), $aliases)); // mock the methods $reflectionClass->expects($this->any())->method('toPhpReflectionClass')->will($this->returnValue($phpReflectionClass)); $reflectionClass->expects($this->once())->method('implementsInterface')->will($this->returnValue(true)); $reflectionClass->expects($this->any())->method('hasAnnotation')->with(Route::ANNOTATION)->will($this->returnValue(true)); $reflectionClass->expects($this->any())->method('getAnnotation')->with(Route::ANNOTATION)->will($this->returnValue($annotation)); $reflectionClass->expects($this->once())->method('getProperties')->will($this->returnValue($properties)); $reflectionClass->expects($this->once())->method('getMethods')->will($this->returnValue($methods)); // mock the methods $this->descriptor->expects($this->any())->method('newAnnotationInstance')->with($reflectionClass)->will($this->returnValue($annotation)); // initialize the descriptor instance $this->descriptor->fromReflectionClass($reflectionClass); // check the name parsed from the reflection class $this->assertSame($phpReflectionClass->getName(), $this->descriptor->getClassName()); $this->assertSame('testServlet', $this->descriptor->getName()); $this->assertSame('A test servlet implementation', $this->descriptor->getDescription()); $this->assertSame('Test Servlet', $this->descriptor->getDisplayName()); $this->assertCount(1, $this->descriptor->getEpbReferences()); $this->assertCount(1, $this->descriptor->getResReferences()); $this->assertCount(1, $this->descriptor->getPersistenceUnitReferences()); $this->assertCount(3, $this->descriptor->getReferences()); }