/annotations/tests/constrained_annotation_test.php
PHP | 92 lines | 56 code | 22 blank | 14 comment | 0 complexity | cf2ffdbc9f86f21124e505613a38d05e MD5 | raw file
Possible License(s): LGPL-2.1
1<?php 2 require_once('simpletest/autorun.php'); 3 require_once(dirname(__FILE__).'/../../annotations.php'); 4 5 /** @Target("class") */ 6 class ClassRestrictedAnnotation extends Annotation {} 7 8 /** @Target("method") */ 9 class MethodRestrictedAnnotation extends Annotation {} 10 11 /** @Target("property") */ 12 class PropertyRestrictedAnnotation extends Annotation {} 13 14 /** @Target("nested") */ 15 class NestedRestrictedAnnotation extends Annotation {} 16 17 /** @Target({"class", "property"}) */ 18 class ClassOrPropertyRestrictedAnnotation extends Annotation {} 19 20 21 class BadlyAnnotatedClass { 22 /** @ClassRestrictedAnnotation */ 23 private $property; 24 25 /** @ClassRestrictedAnnotation */ 26 public function method() {} 27 28 /** @ClassOrPropertyRestrictedAnnotation */ 29 public function method2() {} 30 } 31 32 /** @ClassRestrictedAnnotation(value = @MethodRestrictedAnnotation(true)) */ 33 class ClassWithBadlyNestedAnnotations {} 34 35 /** @ClassRestrictedAnnotation */ 36 class SuccesfullyAnnotatedClass { 37 /** @PropertyRestrictedAnnotation */ 38 private $property; 39 40 /** @ClassOrPropertyRestrictedAnnotation */ 41 private $property2; 42 43 /** @MethodRestrictedAnnotation */ 44 public function method() {} 45 46 /** @MethodRestrictedAnnotation(value = @NestedRestrictedAnnotation) */ 47 public function method2() {} 48 } 49 50 class TestOfConstrainedAnnotation extends UnitTestCase { 51 public function testClassAnnotationThrowsErrorWhenOnMethod() { 52 $this->expectError("Annotation 'ClassRestrictedAnnotation' not allowed on BadlyAnnotatedClass::method"); 53 $reflection = new ReflectionAnnotatedClass('BadlyAnnotatedClass'); 54 $method = $reflection->getMethod('method'); 55 } 56 57 public function testClassAnnotationThrowsErrorWhenOnProperty() { 58 $this->expectError("Annotation 'ClassRestrictedAnnotation' not allowed on BadlyAnnotatedClass::\$property"); 59 $reflection = new ReflectionAnnotatedClass('BadlyAnnotatedClass'); 60 $method = $reflection->getProperty('property'); 61 } 62 63 public function testSingleTargetAnnotationThrowsNoErrorWhenOnRightPlace() { 64 $reflection = new ReflectionAnnotatedClass('SuccesfullyAnnotatedClass'); 65 $method = $reflection->getMethod('method'); 66 $property = $reflection->getProperty('property'); 67 } 68 69 public function testMultiTargetAnnotationThrowsErrorWhenOnWrongPlace() { 70 $this->expectError("Annotation 'ClassOrPropertyRestrictedAnnotation' not allowed on BadlyAnnotatedClass::method2"); 71 $reflection = new ReflectionAnnotatedClass('BadlyAnnotatedClass'); 72 $method = $reflection->getMethod('method2'); 73 } 74 75 public function testMultiTargetAnnotationThrowsNoErrorWhenOnRightPlace() { 76 $reflection = new ReflectionAnnotatedClass('SuccesfullyAnnotatedClass'); 77 $method = $reflection->getProperty('property2'); 78 } 79 80 public function testBadlyNestedAnnotationThrowsError() { 81 $this->expectError("Annotation 'MethodRestrictedAnnotation' nesting not allowed"); 82 $this->expectError("Annotation 'MethodRestrictedAnnotation' nesting not allowed"); // because of parsing 83 $reflection = new ReflectionAnnotatedClass('ClassWithBadlyNestedAnnotations'); 84 } 85 86 public function testSuccesfullyNestedAnnotationThrowsNoError() { 87 $reflection = new ReflectionAnnotatedClass('SuccesfullyAnnotatedClass'); 88 $reflection->getMethod('method2')->getAnnotation('MethodRestrictedAnnotation'); 89 90 } 91 } 92?>