Beispiel #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 fromReflectionProperty() method
  * and an empty annotation works as expected.
  *
  * @return void
  */
 public function testFromReflectionPropertyAndAnnotationWithSomeAttributes()
 {
     // prepare the annotation values
     $values = array('description' => 'A Description', 'beanInterface' => 'DummyEnterpriseBeanLocal', 'beanName' => 'DummyEnterpriseBean', 'lookup' => 'php:global/example/DummyEnterpriseBean');
     // create a mock annotation implementation
     $beanAnnotation = $this->getMockBuilder('AppserverIo\\Psr\\EnterpriseBeans\\Annotations\\EnterpriseBean')->setConstructorArgs(array('EnterpriseBean', $values))->getMockForAbstractClass();
     // create a mock annotation
     $annotation = $this->getMockBuilder('AppserverIo\\Lang\\Reflection\\ReflectionAnnotation')->setMethods(array('getAnnotationName', 'getValues', 'newInstance'))->setConstructorArgs(array('EnterpriseBean', $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(EnterpriseBean::ANNOTATION => EnterpriseBean::__getClass());
     // initialize the reflection property
     $reflectionProperty = $this->getMockBuilder('AppserverIo\\Lang\\Reflection\\ReflectionProperty')->setConstructorArgs(array(__CLASS__, array(), $aliases))->setMethods(array('hasAnnotation', 'getAnnotation', 'getClassName', 'getPropertyName'))->getMock();
     // mock the methods
     $reflectionProperty->expects($this->once())->method('hasAnnotation')->with(EnterpriseBean::ANNOTATION)->will($this->returnValue(true));
     $reflectionProperty->expects($this->once())->method('getAnnotation')->with(EnterpriseBean::ANNOTATION)->will($this->returnValue($annotation));
     $reflectionProperty->expects($this->exactly(1))->method('getClassName')->will($this->returnValue(__CLASS__));
     $reflectionProperty->expects($this->exactly(2))->method('getPropertyName')->will($this->returnValue('dummyEnterpriseBean'));
     // initialize the descriptor from the reflection property
     $this->descriptor->fromReflectionProperty($reflectionProperty);
     // check that the descriptor has been initialized successfully
     $this->assertSame('env/DummyEnterpriseBean', $this->descriptor->getName());
     $this->assertSame('DummyEnterpriseBeanLocal', $this->descriptor->getBeanInterface());
     $this->assertSame('DummyEnterpriseBean', $this->descriptor->getBeanName());
     $this->assertSame('A Description', $this->descriptor->getDescription());
     $this->assertSame('php:global/example/DummyEnterpriseBean', $this->descriptor->getLookup());
 }
Beispiel #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());
 }