Example #1
0
 /**
  * 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
  * 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());
 }