/annotations/tests/constrained_annotation_test.php

http://addendum.googlecode.com/ · PHP · 92 lines · 56 code · 22 blank · 14 comment · 0 complexity · cf2ffdbc9f86f21124e505613a38d05e MD5 · raw file

  1. <?php
  2. require_once('simpletest/autorun.php');
  3. require_once(dirname(__FILE__).'/../../annotations.php');
  4. /** @Target("class") */
  5. class ClassRestrictedAnnotation extends Annotation {}
  6. /** @Target("method") */
  7. class MethodRestrictedAnnotation extends Annotation {}
  8. /** @Target("property") */
  9. class PropertyRestrictedAnnotation extends Annotation {}
  10. /** @Target("nested") */
  11. class NestedRestrictedAnnotation extends Annotation {}
  12. /** @Target({"class", "property"}) */
  13. class ClassOrPropertyRestrictedAnnotation extends Annotation {}
  14. class BadlyAnnotatedClass {
  15. /** @ClassRestrictedAnnotation */
  16. private $property;
  17. /** @ClassRestrictedAnnotation */
  18. public function method() {}
  19. /** @ClassOrPropertyRestrictedAnnotation */
  20. public function method2() {}
  21. }
  22. /** @ClassRestrictedAnnotation(value = @MethodRestrictedAnnotation(true)) */
  23. class ClassWithBadlyNestedAnnotations {}
  24. /** @ClassRestrictedAnnotation */
  25. class SuccesfullyAnnotatedClass {
  26. /** @PropertyRestrictedAnnotation */
  27. private $property;
  28. /** @ClassOrPropertyRestrictedAnnotation */
  29. private $property2;
  30. /** @MethodRestrictedAnnotation */
  31. public function method() {}
  32. /** @MethodRestrictedAnnotation(value = @NestedRestrictedAnnotation) */
  33. public function method2() {}
  34. }
  35. class TestOfConstrainedAnnotation extends UnitTestCase {
  36. public function testClassAnnotationThrowsErrorWhenOnMethod() {
  37. $this->expectError("Annotation 'ClassRestrictedAnnotation' not allowed on BadlyAnnotatedClass::method");
  38. $reflection = new ReflectionAnnotatedClass('BadlyAnnotatedClass');
  39. $method = $reflection->getMethod('method');
  40. }
  41. public function testClassAnnotationThrowsErrorWhenOnProperty() {
  42. $this->expectError("Annotation 'ClassRestrictedAnnotation' not allowed on BadlyAnnotatedClass::\$property");
  43. $reflection = new ReflectionAnnotatedClass('BadlyAnnotatedClass');
  44. $method = $reflection->getProperty('property');
  45. }
  46. public function testSingleTargetAnnotationThrowsNoErrorWhenOnRightPlace() {
  47. $reflection = new ReflectionAnnotatedClass('SuccesfullyAnnotatedClass');
  48. $method = $reflection->getMethod('method');
  49. $property = $reflection->getProperty('property');
  50. }
  51. public function testMultiTargetAnnotationThrowsErrorWhenOnWrongPlace() {
  52. $this->expectError("Annotation 'ClassOrPropertyRestrictedAnnotation' not allowed on BadlyAnnotatedClass::method2");
  53. $reflection = new ReflectionAnnotatedClass('BadlyAnnotatedClass');
  54. $method = $reflection->getMethod('method2');
  55. }
  56. public function testMultiTargetAnnotationThrowsNoErrorWhenOnRightPlace() {
  57. $reflection = new ReflectionAnnotatedClass('SuccesfullyAnnotatedClass');
  58. $method = $reflection->getProperty('property2');
  59. }
  60. public function testBadlyNestedAnnotationThrowsError() {
  61. $this->expectError("Annotation 'MethodRestrictedAnnotation' nesting not allowed");
  62. $this->expectError("Annotation 'MethodRestrictedAnnotation' nesting not allowed"); // because of parsing
  63. $reflection = new ReflectionAnnotatedClass('ClassWithBadlyNestedAnnotations');
  64. }
  65. public function testSuccesfullyNestedAnnotationThrowsNoError() {
  66. $reflection = new ReflectionAnnotatedClass('SuccesfullyAnnotatedClass');
  67. $reflection->getMethod('method2')->getAnnotation('MethodRestrictedAnnotation');
  68. }
  69. }
  70. ?>