示例#1
0
 /**
  * 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);
 }
 /**
  * Tests if the merge method fails with an exception if the class
  * name doesn't match when try to merge to descriptor instances.
  *
  * @return void
  * @expectedException AppserverIo\Routlt\Description\DescriptorException
  */
 public function testMergeWithNotMatchingDescriptor()
 {
     // initialize the annotation aliases
     $aliases = array(Resource::ANNOTATION => Resource::__getClass(), EnterpriseBean::ANNOTATION => EnterpriseBean::__getClass());
     // create a reflection class
     $reflectionClass = new ReflectionClass(__CLASS__, array(), $aliases);
     // initialize the descriptor instance from reflection class
     $this->descriptor->fromReflectionClass($reflectionClass);
     // initialize the descriptor to merge
     $descriptorToMerge = $this->getMockBuilder('AppserverIo\\Routlt\\Description\\PathDescriptor')->setMethods(array('getClassName'))->getMock();
     // mock the getClassName() method
     $descriptorToMerge->expects($this->exactly(2))->method('getClassName')->will($this->returnValue('UnknownClass'));
     // merge the descriptors
     $this->descriptor->merge($descriptorToMerge);
 }
 /**
  * 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());
 }
 /**
  * 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('type' => 'AppserverIo\\Psr\\Timer\\TimerServiceContextInterface', 'lookup' => 'php:global/example/TimerServiceContextInterface', 'description' => 'Reference to a timer service');
     // create a mock annotation implementation
     $beanAnnotation = $this->getMockBuilder('AppserverIo\\Psr\\EnterpriseBeans\\Annotations\\Resource')->setConstructorArgs(array('Resource', $values))->getMockForAbstractClass();
     // create a mock annotation
     $annotation = $this->getMockBuilder('AppserverIo\\Lang\\Reflection\\ReflectionAnnotation')->setMethods(array('getAnnotationName', 'getValues', 'newInstance'))->setConstructorArgs(array('Resource', $values))->getMock();
     // mock the ReflectionAnnotation methods
     $annotation->expects($this->once())->method('getAnnotationName')->will($this->returnValue('EnterpriseBean'));
     $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());
     // 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(Resource::ANNOTATION)->will($this->returnValue(true));
     $reflectionMethod->expects($this->once())->method('getAnnotation')->with(Resource::ANNOTATION)->will($this->returnValue($annotation));
     $reflectionMethod->expects($this->exactly(2))->method('getClassName')->will($this->returnValue(__CLASS__));
     $reflectionMethod->expects($this->exactly(2))->method('getMethodName')->will($this->returnValue('injectDummyResource'));
     // initialize the descriptor from the reflection method
     $this->descriptor->fromReflectionMethod($reflectionMethod);
     // check that the descriptor has been initialized successfully
     $this->assertSame('env/DummyResource', $this->descriptor->getName());
     $this->assertSame('Reference to a timer service', $this->descriptor->getDescription());
     $this->assertSame('php:global/example/TimerServiceContextInterface', $this->descriptor->getLookup());
     $this->assertSame('AppserverIo\\Psr\\Timer\\TimerServiceContextInterface', $this->descriptor->getType());
 }
示例#5
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 invalid
  * @Remote bean annotation throws an exception.
  *
  * @return void
  * @expectedException \Exception
  */
 public function testFromReflectionClassWithInvalidRemoteAnnotation()
 {
     // 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());
     // create a mock reflection class
     $reflectionClass = $this->getMockBuilder('AppserverIo\\Lang\\Reflection\\ReflectionClass')->setMethods(array('hasAnnotation'))->setConstructorArgs(array(__CLASS__, array(), $aliases))->getMock();
     // mock the methods
     $reflectionClass->expects($this->exactly(2))->method('hasAnnotation')->withConsecutive(array('Local'), array('Remote'))->willReturnOnConsecutiveCalls(false, true);
     // mock the methods
     $this->descriptor->expects($this->once())->method('newAnnotationInstance')->with($reflectionClass)->will($this->returnValue($annotation));
     // initialize the descriptor instance
     $this->descriptor->fromReflectionClass($reflectionClass);
 }
 /**
  * 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());
 }