Changeset 1072

Show
Ignore:
Timestamp:
11/27/07 09:21:08 (1 year ago)
Author:
mikey
Message:

allowed annotations for method and function parameters, implements enhancement #110

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationAnnotationState.php

    r488 r1072  
    3131        } 
    3232         
     33        if ('{' === $token) { 
     34            $this->parser->changeState(stubAnnotationState::ARGUMENT); 
     35            return; 
     36        } 
     37         
    3338        if ('[' === $token) { 
    3439            $this->parser->changeState(stubAnnotationState::ANNOTATION_TYPE); 
  • trunk/src/main/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationNameState.php

    r490 r1072  
    3535        return $this->name; 
    3636    } 
    37      
     37 
    3838    /** 
    3939     * mark this state as the currently used state 
     
    7575        } 
    7676         
     77        if ('{' === $token) { 
     78            if (strlen($this->name) == 0) { 
     79                throw new ReflectionException('Annotation name can not be empty'); 
     80            } 
     81             
     82            $this->checkName(); 
     83            $this->parser->registerAnnotation($this->name); 
     84            $this->parser->changeState(stubAnnotationState::ARGUMENT); 
     85            return; 
     86        } 
     87         
    7788        if ('[' === $token) { 
    7889            if (strlen($this->name) == 0) { 
     
    99110        $this->name .= $token; 
    100111    } 
    101      
     112 
    102113    /** 
    103114     * check if the name is valid 
  • trunk/src/main/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationState.php

    r454 r1072  
    4949     */ 
    5050    const PARAM_VALUE     = 7; 
     51    /** 
     52     * parser is inside a argument declaration 
     53     */ 
     54    const ARGUMENT        = 8; 
    5155 
    5256    /** 
  • trunk/src/main/php/net/stubbles/reflection/annotations/parser/stubAnnotationParser.php

    r452 r1072  
    7070     */ 
    7171    public function setAnnotationType($type); 
     72 
     73    /** 
     74     * sets the argument for which the annotation is declared 
     75     * 
     76     * @param  string  $argument  name of the argument 
     77     */ 
     78    public function setAnnotationForArgument($argument); 
    7279} 
    7380?> 
  • trunk/src/main/php/net/stubbles/reflection/annotations/parser/stubAnnotationStateParser.php

    r896 r1072  
    99 */ 
    1010stubClassLoader::load('net.stubbles.reflection.annotations.parser.stubAnnotationParser', 
     11                      'net.stubbles.reflection.annotations.parser.state.stubAnnotationArgumentState', 
    1112                      'net.stubbles.reflection.annotations.parser.state.stubAnnotationDocblockState', 
    1213                      'net.stubbles.reflection.annotations.parser.state.stubAnnotationAnnotationState', 
     
    6768        $this->states[stubAnnotationState::ANNOTATION_NAME] = new stubAnnotationNameState($this); 
    6869        $this->states[stubAnnotationState::ANNOTATION_TYPE] = new stubAnnotationTypeState($this); 
     70        $this->states[stubAnnotationState::ARGUMENT]        = new stubAnnotationArgumentState($this); 
    6971        $this->states[stubAnnotationState::PARAMS]          = new stubAnnotationParamsState($this); 
    7072        $this->states[stubAnnotationState::PARAM_NAME]      = new stubAnnotationParamNameState($this); 
     
    122124    public function registerAnnotation($name) 
    123125    { 
    124         $this->annotations[$name] = array('type' => $name, 'params' => array()); 
     126        $this->annotations[$name] = array('type'     => $name, 
     127                                          'params'   => array(), 
     128                                          'argument' => null 
     129                                    ); 
    125130        $this->currentAnnotation  = $name; 
    126131    } 
     
    171176    { 
    172177        $this->annotations[$this->currentAnnotation]['type'] = $type; 
     178    } 
     179 
     180    /** 
     181     * sets the argument for which the annotation is declared 
     182     * 
     183     * @param  string  $argument  name of the argument 
     184     */ 
     185    public function setAnnotationForArgument($argument) 
     186    { 
     187        $this->annotations[$this->currentAnnotation . '#' . $argument] = $this->annotations[$this->currentAnnotation]; 
     188        unset($this->annotations[$this->currentAnnotation]); 
     189        $this->currentAnnotation .= '#' . $argument; 
     190        $this->annotations[$this->currentAnnotation]['argument'] = $argument; 
    173191    } 
    174192 
  • trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotation.php

    r832 r1072  
    3333    const TARGET_FUNCTION = 8; 
    3434    /** 
     35     * annotation is applicable for parameters 
     36     */ 
     37    const TARGET_PARAM    = 16; 
     38    /** 
    3539     * annotation is applicable for classes, properties, methods and functions 
    3640     */ 
    37     const TARGET_ALL      = 15
     41    const TARGET_ALL      = 31
    3842 
    3943    /** 
  • trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotationFactory.php

    r1071 r1072  
    7777        } 
    7878         
    79         if (self::$annotations[$hash][$annotationName]['type'] !== $annotationName) { 
    80             $annotationType = self::findAnnotationClass($annotationName, true); 
     79        if (strpos($annotationName, '#') !== false) { 
     80            $realAnnotationName = substr($annotationName, 0, strpos($annotationName, '#')); 
     81        } else { 
     82            $realAnnotationName = $annotationName; 
     83        } 
     84         
     85        if (self::$annotations[$hash][$annotationName]['type'] !== $realAnnotationName) { 
     86            $annotationType = self::findAnnotationClass($realAnnotationName, true); 
    8187            if (is_a($annotation, $annotationType) === false) { 
    8288                throw new ReflectionException('The annotation: ' . $annotationName . ' is not an instance of ' . $annotationType . '.'); 
  • trunk/src/main/php/net/stubbles/reflection/stubReflectionParameter.php

    r697 r1072  
    77 * @subpackage  reflection 
    88 */ 
    9 stubClassLoader::load('net.stubbles.reflection.stubReflectionFunction', 
     9stubClassLoader::load('net.stubbles.reflection.annotations.stubAnnotatable', 
     10                      'net.stubbles.reflection.annotations.stubAnnotationFactory', 
     11                      'net.stubbles.reflection.stubReflectionFunction', 
    1012                      'net.stubbles.reflection.stubReflectionClass' 
    1113); 
     
    1618 * @subpackage  reflection 
    1719 */ 
    18 class stubReflectionParameter extends ReflectionParameter 
     20class stubReflectionParameter extends ReflectionParameter implements stubAnnotatable 
    1921{ 
    2022    /** 
     
    3032     */ 
    3133    protected $paramName; 
     34    /** 
     35     * doc block of method or function where this param belongs to 
     36     * 
     37     * @var  string 
     38     */ 
     39    protected $docComment; 
    3240 
    3341    /** 
     
    4250        $this->paramName    = $paramName; 
    4351        parent::__construct($functionName, $paramName); 
     52    } 
     53 
     54    /** 
     55     * check whether the class has the given annotation or not 
     56     * 
     57     * @param   string  $annotationName 
     58     * @return  bool 
     59     */ 
     60    public function hasAnnotation($annotationName) 
     61    { 
     62        if (is_array($this->functionName) === true) { 
     63            $targetName = $this->functionName[0] . '::' . $this->functionName[1] . '()'; 
     64            $ref        = new stubReflectionMethod($this->functionName[0], $this->functionName[1]); 
     65        } else { 
     66            $targetName = $this->functionName; 
     67            $ref        = new stubReflectionFunction($this->functionName); 
     68        } 
     69         
     70        return stubAnnotationFactory::has($ref->getDocComment(), $annotationName . '#' . $this->paramName, stubAnnotation::TARGET_PARAM, $targetName, $ref->getFileName()); 
     71    } 
     72 
     73    /** 
     74     * return the specified annotation 
     75     * 
     76     * @param   string          $annotationName 
     77     * @return  stubAnnotation 
     78     * @throws  ReflectionException 
     79     */ 
     80    public function getAnnotation($annotationName) 
     81    { 
     82        if (is_array($this->functionName) === true) { 
     83            $targetName = $this->functionName[0] . '::' . $this->functionName[1] . '()'; 
     84            $ref        = new stubReflectionMethod($this->functionName[0], $this->functionName[1]); 
     85        } else { 
     86            $targetName = $this->functionName; 
     87            $ref        = new stubReflectionFunction($this->functionName); 
     88        } 
     89         
     90        return stubAnnotationFactory::create($ref->getDocComment(), $annotationName . '#' . $this->paramName, stubAnnotation::TARGET_PARAM, $targetName, $ref->getFileName()); 
    4491    } 
    4592 
  • trunk/src/test/php/net/stubbles/reflection/ReflectionTestSuite.php

    r940 r1072  
    2929        $this->addTestFile(dirname(__FILE__) . '/annotations/parser/stubAnnotationStateParserTestCase.php'); 
    3030        $this->addTestFile(dirname(__FILE__) . '/annotations/parser/state/stubAnnotationAnnotationStateTestCase.php'); 
     31        $this->addTestFile(dirname(__FILE__) . '/annotations/parser/state/stubAnnotationArgumentStateTestCase.php'); 
    3132        $this->addTestFile(dirname(__FILE__) . '/annotations/parser/state/stubAnnotationDocblockStateTestCase.php'); 
    3233        $this->addTestFile(dirname(__FILE__) . '/annotations/parser/state/stubAnnotationNameStateTestCase.php'); 
  • trunk/src/test/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationAnnotationStateTestCase.php

    r454 r1072  
    2929     */ 
    3030    protected $mockAnnotationParser; 
    31      
     31 
    3232    /** 
    3333     * set up test environment 
     
    3838        $this->annotationState      = new stubAnnotationAnnotationState($this->mockAnnotationParser); 
    3939    } 
    40      
     40 
    4141    /** 
    4242     * test processing a line break 
     
    4747        $this->annotationState->process("\n"); 
    4848    } 
    49      
     49 
     50    /** 
     51     * test processing an argument parenthesis 
     52     */ 
     53    public function testProcessArgumentParenthesis() 
     54    { 
     55        $this->mockAnnotationParser->expectOnce('changeState', array(stubAnnotationState::ARGUMENT)); 
     56        $this->annotationState->process('{'); 
     57    } 
     58 
    5059    /** 
    5160     * test processing a type parenthesis 
     
    5665        $this->annotationState->process('['); 
    5766    } 
    58      
     67 
    5968    /** 
    6069     * test processing a value parenthesis 
     
    6574        $this->annotationState->process('('); 
    6675    } 
    67      
     76 
    6877    /** 
    6978     * test processing a value parenthesis 
  • trunk/src/test/php/net/stubbles/reflection/annotations/parser/state/stubAnnotationNameStateTestCase.php

    r490 r1072  
    2929     */ 
    3030    protected $mockAnnotationParser; 
    31      
     31 
    3232    /** 
    3333     * set up test environment 
     
    3838        $this->annotationNameState  = new stubAnnotationNameState($this->mockAnnotationParser); 
    3939    } 
    40      
     40 
    4141    /** 
    4242     * test processing a space element 
     
    5252        $this->annotationNameState->process(' '); 
    5353    } 
    54      
     54 
    5555    /** 
    5656     * test processing a line break 
     
    6565        $this->annotationNameState->process("\n"); 
    6666    } 
    67      
     67 
     68    /** 
     69     * test processing an argument parenthesis 
     70     */ 
     71    public function testProcessArgumentParenthesis() 
     72    { 
     73        $this->mockAnnotationParser->expectOnce('registerAnnotation', array('a')); 
     74        $this->mockAnnotationParser->expectOnce('changeState', array(stubAnnotationState::ARGUMENT)); 
     75         
     76        $this->annotationNameState->process('a'); 
     77        $this->annotationNameState->process('{'); 
     78         
     79        $this->annotationNameState->selected(); 
     80        $this->expectException('ReflectionException'); 
     81        $this->annotationNameState->process('{'); 
     82    } 
     83 
    6884    /** 
    6985     * test processing a type parenthesis 
     
    8197        $this->annotationNameState->process('['); 
    8298    } 
    83      
     99 
    84100    /** 
    85101     * test processing a value parenthesis 
     
    138154     * test processing illegal characters 
    139155     */ 
     156    public function testProcessIllegalCharactersFollowedByArgumentParenthesis() 
     157    { 
     158        $this->annotationNameState->process('a'); 
     159        $this->annotationNameState->process('1'); 
     160        $this->annotationNameState->process('_'); 
     161        $this->expectException('ReflectionException'); 
     162        $this->annotationNameState->process(')'); 
     163        $this->annotationNameState->process('{'); 
     164    } 
     165 
     166    /** 
     167     * test processing illegal characters 
     168     */ 
    140169    public function testProcessIllegalCharactersFollowedByTypeParenthesis() 
    141170    { 
  • trunk/src/test/php/net/stubbles/reflection/annotations/parser/stubAnnotationStateParserTestCase.php

    r896 r1072  
    5050} 
    5151MyTestClass::__static(); 
     52class MyTestClass2 extends stubBaseObject 
     53{ 
     54    /** 
     55     * a method with an annotation for its parameter 
     56     * 
     57     * @param  string  $bar 
     58     * @ForArgument1{bar} 
     59     * @ForArgument2{bar}(key='value') 
     60     * @MoreArgument1{bar}[Casted] 
     61     * @MoreArgument2{bar}[Casted](key='value') 
     62     * @MoreArgument3[CastedAround]{bar} 
     63     * @MoreArgument4[CastedAround]{bar}(key='value') 
     64     */ 
     65    public function foo($bar) { } 
     66} 
    5267/** 
    5368 * Test for net.stubbles.reflection.annotations.parser.stubAnnotationStateParser. 
     
    126141        $this->assertEqual($annotations['WithTypes']['params']['class']->getName(), 'MyTestClass'); 
    127142    } 
    128      
     143 
    129144    /** 
    130145     * test that tabs are recognized correctly 
     
    137152        $this->assertTrue(isset($annotations['Foo'])); 
    138153    } 
     154 
     155    /** 
     156     * test that parameter argumentations are recognized correctly 
     157     */ 
     158    public function testArgument() 
     159    { 
     160        $method                = new ReflectionMethod('MyTestClass2', 'foo'); 
     161        $annotationStateParser = new stubAnnotationStateParser(); 
     162        $annotations           = $annotationStateParser->parse($method->getDocComment()); 
     163        $this->assertTrue(isset($annotations['ForArgument1#bar'])); 
     164        $this->assertEqual($annotations['ForArgument1#bar']['argument'], 'bar'); 
     165        $this->assertEqual($annotations['ForArgument1#bar']['type'], 'ForArgument1'); 
     166        $this->assertEqual($annotations['ForArgument1#bar']['params'], array()); 
     167        $this->assertTrue(isset($annotations['ForArgument2#bar'])); 
     168        $this->assertEqual($annotations['ForArgument2#bar']['argument'], 'bar'); 
     169        $this->assertEqual($annotations['ForArgument2#bar']['type'], 'ForArgument2'); 
     170        $this->assertEqual($annotations['ForArgument2#bar']['params'], array('key' => 'value')); 
     171        $this->assertTrue(isset($annotations['MoreArgument1#bar'])); 
     172        $this->assertEqual($annotations['MoreArgument1#bar']['argument'], 'bar'); 
     173        $this->assertEqual($annotations['MoreArgument1#bar']['type'], 'Casted'); 
     174        $this->assertEqual($annotations['MoreArgument1#bar']['params'], array()); 
     175        $this->assertTrue(isset($annotations['MoreArgument2#bar'])); 
     176        $this->assertEqual($annotations['MoreArgument2#bar']['argument'], 'bar'); 
     177        $this->assertEqual($annotations['MoreArgument2#bar']['type'], 'Casted'); 
     178        $this->assertEqual($annotations['MoreArgument2#bar']['params'], array('key' => 'value')); 
     179        $this->assertTrue(isset($annotations['MoreArgument3#bar'])); 
     180        $this->assertEqual($annotations['MoreArgument3#bar']['argument'], 'bar'); 
     181        $this->assertEqual($annotations['MoreArgument3#bar']['type'], 'CastedAround'); 
     182        $this->assertEqual($annotations['MoreArgument3#bar']['params'], array()); 
     183        $this->assertTrue(isset($annotations['MoreArgument4#bar'])); 
     184        $this->assertEqual($annotations['MoreArgument4#bar']['argument'], 'bar'); 
     185        $this->assertEqual($annotations['MoreArgument4#bar']['type'], 'CastedAround'); 
     186        $this->assertEqual($annotations['MoreArgument4#bar']['params'], array('key' => 'value')); 
     187    } 
    139188} 
    140189?> 
  • trunk/src/test/php/net/stubbles/reflection/annotations/stubAnnotationFactoryApplicableTestCase.php

    r432 r1072  
    2323     */ 
    2424    protected $mockStubAnnotation; 
    25      
     25 
    2626    /** 
    2727     * create test environment 
     
    3131        $this->mockStubAnnotation = new MockStubAnnotation(); 
    3232    } 
    33      
     33 
    3434    /** 
    3535     * check that the applicable check works correct 
     
    4242        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    4343        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    44     } 
    45      
     44        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     45    } 
     46 
    4647    /** 
    4748     * check that the applicable check works correct 
     
    5455        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    5556        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    56     } 
    57      
     57        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     58    } 
     59 
    5860    /** 
    5961     * check that the applicable check works correct 
     
    6668        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    6769        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    68     } 
    69      
     70        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     71    } 
     72 
    7073    /** 
    7174     * check that the applicable check works correct 
     
    7881        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    7982        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    80     } 
    81      
     83        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     84    } 
     85 
    8286    /** 
    8387     * check that the applicable check works correct 
     
    9094        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    9195        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    92     } 
    93      
     96        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     97    } 
     98 
     99    /** 
     100     * check that the applicable check works correct 
     101     */ 
     102    public function testIsApplicableForParam() 
     103    { 
     104        $this->mockStubAnnotation->setReturnValue('getAnnotationTarget', stubAnnotation::TARGET_PARAM); 
     105        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_CLASS)); 
     106        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PROPERTY)); 
     107        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
     108        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
     109        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     110    } 
     111 
    94112    /** 
    95113     * check that the applicable check works correct 
     
    102120        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    103121        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    104     } 
    105      
     122        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     123    } 
     124 
    106125    /** 
    107126     * check that the applicable check works correct 
     
    114133        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    115134        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    116     } 
    117      
     135        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     136    } 
     137 
    118138    /** 
    119139     * check that the applicable check works correct 
     
    126146        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    127147        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    128     } 
    129      
     148        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     149    } 
     150 
     151    /** 
     152     * check that the applicable check works correct 
     153     */ 
     154    public function testIsApplicableForClassAndParam() 
     155    { 
     156        $this->mockStubAnnotation->setReturnValue('getAnnotationTarget', stubAnnotation::TARGET_CLASS + stubAnnotation::TARGET_PARAM); 
     157        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_CLASS)); 
     158        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PROPERTY)); 
     159        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
     160        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
     161        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     162    } 
     163 
    130164    /** 
    131165     * check that the applicable check works correct 
     
    138172        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    139173        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    140     } 
    141      
     174        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     175    } 
     176 
    142177    /** 
    143178     * check that the applicable check works correct 
     
    150185        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    151186        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    152     } 
    153      
     187        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     188    } 
     189 
     190    /** 
     191     * check that the applicable check works correct 
     192     */ 
     193    public function testIsApplicableForPropertyAndParam() 
     194    { 
     195        $this->mockStubAnnotation->setReturnValue('getAnnotationTarget', stubAnnotation::TARGET_PROPERTY + stubAnnotation::TARGET_PARAM); 
     196        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_CLASS)); 
     197        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PROPERTY)); 
     198        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
     199        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
     200        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     201    } 
     202 
    154203    /** 
    155204     * check that the applicable check works correct 
     
    162211        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    163212        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    164     } 
    165      
     213        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     214    } 
     215 
     216    /** 
     217     * check that the applicable check works correct 
     218     */ 
     219    public function testIsApplicableForMethodAndParam() 
     220    { 
     221        $this->mockStubAnnotation->setReturnValue('getAnnotationTarget', stubAnnotation::TARGET_METHOD + stubAnnotation::TARGET_PARAM); 
     222        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_CLASS)); 
     223        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PROPERTY)); 
     224        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
     225        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
     226        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     227    } 
     228 
     229    /** 
     230     * check that the applicable check works correct 
     231     */ 
     232    public function testIsApplicableForFunctionAndParam() 
     233    { 
     234        $this->mockStubAnnotation->setReturnValue('getAnnotationTarget', stubAnnotation::TARGET_FUNCTION + stubAnnotation::TARGET_PARAM); 
     235        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_CLASS)); 
     236        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PROPERTY)); 
     237        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
     238        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
     239        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     240    } 
     241 
    166242    /** 
    167243     * check that the applicable check works correct 
     
    175251        $this->assertFalse(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    176252    } 
    177      
     253 
    178254    /** 
    179255     * check that the applicable check works correct 
     
    187263        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    188264    } 
    189      
     265 
    190266    /** 
    191267     * check that the applicable check works correct 
     
    199275        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    200276    } 
    201      
     277 
    202278    /** 
    203279     * check that the applicable check works correct 
     
    211287        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
    212288    } 
    213      
     289 
     290    /** 
     291     * check that the applicable check works correct 
     292     */ 
     293    public function testIsApplicableForAllByAddition() 
     294    { 
     295        $this->mockStubAnnotation->setReturnValue('getAnnotationTarget', stubAnnotation::TARGET_CLASS + stubAnnotation::TARGET_PROPERTY + stubAnnotation::TARGET_METHOD + stubAnnotation::TARGET_FUNCTION + stubAnnotation::TARGET_PARAM); 
     296        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_CLASS)); 
     297        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PROPERTY)); 
     298        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
     299        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
     300        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
     301    } 
     302 
    214303    /** 
    215304     * check that the applicable check works correct 
     
    217306    public function testIsApplicableForAll() 
    218307    { 
    219         $this->mockStubAnnotation->setReturnValue('getAnnotationTarget', stubAnnotation::TARGET_CLASS + stubAnnotation::TARGET_PROPERTY + stubAnnotation::TARGET_METHOD + stubAnnotation::TARGET_FUNCTION); 
    220         $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_CLASS)); 
    221         $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PROPERTY)); 
    222         $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
    223         $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
     308        $this->mockStubAnnotation->setReturnValue('getAnnotationTarget', stubAnnotation::TARGET_ALL); 
     309        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_CLASS)); 
     310        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PROPERTY)); 
     311        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_METHOD)); 
     312        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_FUNCTION)); 
     313        $this->assertTrue(stubAnnotationFactory::isApplicable($this->mockStubAnnotation, stubAnnotation::TARGET_PARAM)); 
    224314    } 
    225315} 
  • trunk/src/test/php/net/stubbles/reflection/annotations/stubAnnotationFactoryTestCase.php

    r1069 r1072  
    121121 * @access public 
    122122 * @MyAnnotation( 
     123 *     foo=AnyTestClass.class 
     124 * ) 
     125 */'; 
     126protected $commentComplexForArgument = '/** 
     127 * Foobar bla 
     128 * 
     129 * @access public 
     130 * @MyAnnotation{foo}( 
     131 *     foo="bar", 
     132 *     argh=45, veggie="tomato" 
     133 * ) 
     134 * @AnotherAnnotation{foo}(true) 
     135 * @CastedAnnotation{foo}[AnotherAnnotation](false) 
     136 * @AnotherAnnotation{bar} 
     137 * @MyAnnotation{bar}( 
    123138 *     foo=AnyTestClass.class 
    124139 * ) 
     
    190205 
    191206    /** 
     207     * test that creating an annotation works as expected 
     208     */ 
     209    public function testCreateForArgument() 
     210    { 
     211        $myAnnotation = stubAnnotationFactory::create($this->commentComplexForArgument, 'MyAnnotation#foo', stubAnnotation::TARGET_PARAM, 'MyClass::baz()', __FILE__); 
     212        $this->assertIsA($myAnnotation, 'MyAnnotation'); 
     213        $this->assertEqual('bar', $myAnnotation->foo); 
     214        $this->assertEqual('45', $myAnnotation->argh); 
     215        $this->assertEqual('tomato', $myAnnotation->veggie); 
     216 
     217        $anotherAnnotation = stubAnnotationFactory::create($this->commentComplexForArgument, 'AnotherAnnotation#foo', stubAnnotation::TARGET_PARAM, 'MyClass::baz()', __FILE__); 
     218        $this->assertIsA($anotherAnnotation, 'AnotherAnnotation'); 
     219        $this->assertEqual('true', $anotherAnnotation->value); 
     220 
     221        $castedAnnotation = stubAnnotationFactory::create($this->commentComplexForArgument, 'CastedAnnotation#foo', stubAnnotation::TARGET_PARAM, 'MyClass::baz()', __FILE__); 
     222        $this->assertIsA($castedAnnotation, 'AnotherAnnotation'); 
     223        $this->assertFalse($castedAnnotation->value); 
     224 
     225        $emptyAnnotation = stubAnnotationFactory::create($this->commentComplexForArgument, 'AnotherAnnotation#bar', stubAnnotation::TARGET_PARAM, 'MyClass::baz()', __FILE__); 
     226        $this->assertIsA($emptyAnnotation, 'AnotherAnnotation'); 
     227 
     228        $myAnnotation = stubAnnotationFactory::create($this->commentComplexForArgument, 'MyAnnotation#bar', stubAnnotation::TARGET_PARAM, 'MyClass::baz()', __FILE__); 
     229        $this->assertIsA($myAnnotation, 'MyAnnotation'); 
     230        $this->assertIsA($myAnnotation->foo, 'stubReflectionClass'); 
     231         
     232        $this->expectException('ReflectionException'); 
     233        stubAnnotationFactory::create($this->commentComplexForArgument, 'MyAnnotation#baz', stubAnnotation::TARGET_PARAM, 'MyClass::baz()', __FILE__); 
     234    } 
     235 
     236    /** 
    192237     * test that the cache works as expected 
    193238     */ 
  • trunk/src/test/php/net/stubbles/reflection/stubReflectionParameterTestCase.php

    r697 r1072  
    88 */ 
    99stubClassLoader::load('net.stubbles.reflection.stubReflectionParameter'); 
     10/** 
     11 * annotation for parameters 
     12 * 
     13 * @package     stubbles 
     14 * @subpackage  reflection_test 
     15 */ 
     16class stubParamAnnoAnnotation extends stubAbstractAnnotation implements stubAnnotation 
     17{ 
     18    /** 
     19     * Returns the target of the annotation as bitmap. 
     20     * 
     21     * @return  int 
     22     */ 
     23    public function getAnnotationTarget() 
     24    { 
     25        return stubAnnotation::TARGET_PARAM; 
     26    } 
     27} 
     28/** 
     29 * a function 
     30 * 
     31 * @package     stubbles 
     32 * @subpackage  reflection_test 
     33 * @param       mixed  $param 
     34 * @ParamAnno{param} 
     35 */ 
    1036function stubtest_function($param) 
    1137{ 
    1238    // nothing to do 
    1339} 
     40/** 
     41 * a class for tests 
     42 * 
     43 * @package     stubbles 
     44 * @subpackage  reflection_test 
     45 */ 
    1446class stubParamTest 
    1547{ 
     48    /** 
     49     * a method 
     50     * 
     51     * @param  mixed  $param 
     52     * @ParamAnno{param} 
     53     */ 
    1654    function paramTest($param) 
    1755    { 
     
    1957    } 
    2058} 
     59/** 
     60 * another class for tests 
     61 * 
     62 * @package     stubbles 
     63 * @subpackage  reflection_test 
     64 */ 
    2165class stubParamTest2 extends stubParamTest 
    2266{ 
     67    /** 
     68     * another method 
     69     * 
     70     * @param  stubParamTest  $param2 
     71     * @ParamAnno{param2} 
     72     */ 
    2373    function paramTest2(stubParamTest $param2) 
    2474    { 
    2575        // nothing to do 
    2676    } 
    27      
     77 
     78    /** 
     79     * one more method 
     80     * 
     81     * @param  stubParamTest2  $param2 
     82     * @ParamAnno{param} 
     83     */ 
    2884    function paramTest3(self $param2) 
    2985    { 
     
    80136        $this->stubRefParamMethod3  = new stubReflectionParameter(array('stubParamTest2', 'paramTest2'), 'param2'); 
    81137        $this->stubRefParamMethod4  = new stubReflectionParameter(array('stubParamTest2', 'paramTest3'), 'param2'); 
     138    } 
     139 
     140    /** 
     141     * assure that annotations are handled correctly 
     142     */ 
     143    public function testAnnotations() 
     144    { 
     145        $this->assertTrue($this->stubRefParamFunction->hasAnnotation('ParamAnno')); 
     146        $this->assertIsA($this->stubRefParamFunction->getAnnotation('ParamAnno'), 'stubParamAnnoAnnotation'); 
     147        $this->assertTrue($this->stubRefParamMethod1->hasAnnotation('ParamAnno')); 
     148        $this->assertIsA($this->stubRefParamMethod1->getAnnotation('ParamAnno'), 'stubParamAnnoAnnotation'); 
     149        $this->assertTrue($this->stubRefParamMethod2->hasAnnotation('ParamAnno')); 
     150        $this->assertIsA($this->stubRefParamMethod2->getAnnotation('ParamAnno'), 'stubParamAnnoAnnotation'); 
     151        $this->assertTrue($this->stubRefParamMethod3->hasAnnotation('ParamAnno')); 
     152        $this->assertIsA($this->stubRefParamMethod3->getAnnotation('ParamAnno'), 'stubParamAnnoAnnotation'); 
     153        $this->assertFalse($this->stubRefParamMethod4->hasAnnotation('ParamAnno')); 
     154        $this->expectException('ReflectionException'); 
     155        $this->stubRefParamMethod4->getAnnotation('ParamAnno'); 
    82156    } 
    83157