/** * Check that the merge() method works as expected. * * @return void */ public function testMerge() { // initialize the annotation aliases $aliases = array(Result::ANNOTATION => Result::__getClass(), Results::ANNOTATION => Results::__getClass(), 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('getName', 'getActions', 'getResults', 'getClassName', 'getResReferences', 'getEpbReferences'))->getMock(); // create a action descriptor $action = new ActionDescriptor(); $action->setName('/test'); $action->setMethodName('someMethod'); // create a result descriptor $result = new ResultDescriptor(); $result->setName('failure'); $result->setType('DummyType'); $result->setResult('/path/to/dummy.phtml'); // create a resource descriptor $resReference = new ResReferenceDescriptor(); $resReference->setName('SomeResource'); // create an EPB descriptor $epbReference = new EpbReferenceDescriptor(); $epbReference->getName('UserSessionBean'); // mock the methods $descriptorToMerge->expects($this->once())->method('getName')->will($this->returnValue('/anotherIndex')); $descriptorToMerge->expects($this->once())->method('getActions')->will($this->returnValue(array($action))); $descriptorToMerge->expects($this->once())->method('getResults')->will($this->returnValue(array($result))); $descriptorToMerge->expects($this->once())->method('getClassName')->will($this->returnValue(__CLASS__)); $descriptorToMerge->expects($this->once())->method('getEpbReferences')->will($this->returnValue(array($epbReference))); $descriptorToMerge->expects($this->once())->method('getResReferences')->will($this->returnValue(array($resReference))); // merge the descriptors $this->descriptor->merge($descriptorToMerge); // check the merge values $this->assertSame('/anotherIndex', $this->descriptor->getName()); $this->assertSame(__CLASS__, $this->descriptor->getClassName()); $this->assertCount(3, $this->descriptor->getActions()); $this->assertCount(2, $this->descriptor->getResults()); $this->assertCount(4, $this->descriptor->getReferences()); $this->assertCount(2, $this->descriptor->getResReferences()); $this->assertCount(2, $this->descriptor->getEpbReferences()); // load the actions $actions = $this->descriptor->getActions(); // check that the method name has been overwritten $this->assertEquals($action->getMethodName(), $actions['/test'][HttpProtocol::METHOD_GET]->getMethodName()); }
/** * Initializes the bean configuration instance from the passed reflection class instance. * * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration * * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The initialized descriptor */ public function fromReflectionClass(ClassInterface $reflectionClass) { // add the annotation alias to the reflection class $reflectionClass->addAnnotationAlias(Path::ANNOTATION, Path::__getClass()); $reflectionClass->addAnnotationAlias(Result::ANNOTATION, Result::__getClass()); $reflectionClass->addAnnotationAlias(Results::ANNOTATION, Results::__getClass()); // query if we've an action if ($reflectionClass->implementsInterface('AppserverIo\\Routlt\\ActionInterface') === false && $reflectionClass->toPhpReflectionClass()->isAbstract() === false) { // if not, do nothing return; } // query if we've a servlet with a @Path annotation if ($reflectionClass->hasAnnotation(Path::ANNOTATION) === false) { // if not, do nothing return; } // create a new annotation instance $reflectionAnnotation = $this->newAnnotationInstance($reflectionClass); // load class name $this->setClassName($reflectionClass->getName()); // initialize the annotation instance $annotationInstance = $reflectionAnnotation->newInstance($reflectionAnnotation->getAnnotationName(), $reflectionAnnotation->getValues()); // load the default name to register in naming directory if ($nameAttribute = $annotationInstance->getName()) { $name = $nameAttribute; } else { // if @Annotation(name=****) is NOT set, we use the class name by default $name = strtolower(str_replace('\\', '/', $reflectionClass->getName())); } // prepare and set the name $this->setName(sprintf('/%s', ltrim($name, '/'))); // we've to check for method annotations that declare the action methods foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { if ($action = ActionDescriptor::newDescriptorInstance()->fromReflectionMethod($reflectionMethod)) { $this->addAction($action); } } // we've to check for result annotations if ($reflectionClass->hasAnnotation(Results::ANNOTATION)) { // create the reflection annotation instance $resultsAnnotation = $reflectionClass->getAnnotation(Results::ANNOTATION); // initialize the @Results annotation instance $resultsAnnotationInstance = $resultsAnnotation->newInstance($resultsAnnotation->getAnnotationName(), $resultsAnnotation->getValues()); // load the results foreach ($resultsAnnotationInstance->getResults() as $resultAnnotation) { $this->addResult(ResultDescriptor::newDescriptorInstance()->fromReflectionAnnotation($resultAnnotation)); } } // we've to check for property annotations that references EPB or resources foreach ($reflectionClass->getProperties() as $reflectionProperty) { // load the EPB references if ($epbReference = EpbReferenceDescriptor::newDescriptorInstance()->fromReflectionProperty($reflectionProperty)) { $this->addEpbReference($epbReference); } // load the resource references if ($resReference = ResReferenceDescriptor::newDescriptorInstance()->fromReflectionProperty($reflectionProperty)) { $this->addResReference($resReference); } } // we've to check for method annotations that references EPB or resources foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { // load the EPB references if ($epbReference = EpbReferenceDescriptor::newDescriptorInstance()->fromReflectionMethod($reflectionMethod)) { $this->addEpbReference($epbReference); } // load the resource references if ($resReference = ResReferenceDescriptor::newDescriptorInstance()->fromReflectionMethod($reflectionMethod)) { $this->addResReference($resReference); } } // return the instance return $this; }
/** * Initializes the bean configuration instance with the references of the passed reflection class instance. * * @param \AppserverIo\Lang\Reflection\ClassInterface $reflectionClass The reflection class with the bean configuration * * @return void */ public function referencesFromReflectionClass(ClassInterface $reflectionClass) { // we've to check for property annotations that references EPB or resources foreach ($reflectionClass->getProperties() as $reflectionProperty) { // load the EPB references if ($epbReference = EpbReferenceDescriptor::newDescriptorInstance()->fromReflectionProperty($reflectionProperty)) { $this->addEpbReference($epbReference); } // load the resource references if ($resReference = ResReferenceDescriptor::newDescriptorInstance()->fromReflectionProperty($reflectionProperty)) { $this->addResReference($resReference); } // load the persistence unit references if ($persistenceUnitReference = PersistenceUnitReferenceDescriptor::newDescriptorInstance()->fromReflectionProperty($reflectionProperty)) { $this->addPersistenceUnitReference($persistenceUnitReference); } } // we've to check for method annotations that references EPB or resources foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { // load the EPB references if ($epbReference = EpbReferenceDescriptor::newDescriptorInstance()->fromReflectionMethod($reflectionMethod)) { $this->addEpbReference($epbReference); } // load the resource references if ($resReference = ResReferenceDescriptor::newDescriptorInstance()->fromReflectionMethod($reflectionMethod)) { $this->addResReference($resReference); } // load the persistence unit references if ($persistenceUnitReference = PersistenceUnitReferenceDescriptor::newDescriptorInstance()->fromReflectionMethod($reflectionMethod)) { $this->addPersistenceUnitReference($persistenceUnitReference); } } }
/** * Tests the static newDescriptorInstance() method. * * @return void */ public function testNewDescriptorInstance() { $this->assertInstanceOf('AppserverIo\\Description\\ResReferenceDescriptor', ResReferenceDescriptor::newDescriptorInstance()); }