ControllerMethodReflectorTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\AppFramework\Utility;
  23. class BaseController {
  24. /**
  25. * @Annotation
  26. */
  27. public function test(){}
  28. /**
  29. * @Annotation
  30. */
  31. public function test2(){}
  32. /**
  33. * @Annotation
  34. */
  35. public function test3(){}
  36. }
  37. class MiddleController extends BaseController {
  38. /**
  39. * @NoAnnotation
  40. */
  41. public function test2() {}
  42. public function test3() {}
  43. }
  44. class EndController extends MiddleController {}
  45. class ControllerMethodReflectorTest extends \Test\TestCase {
  46. /**
  47. * @Annotation
  48. */
  49. public function testReadAnnotation(){
  50. $reader = new ControllerMethodReflector();
  51. $reader->reflect(
  52. '\OC\AppFramework\Utility\ControllerMethodReflectorTest',
  53. 'testReadAnnotation'
  54. );
  55. $this->assertTrue($reader->hasAnnotation('Annotation'));
  56. }
  57. /**
  58. * @Annotation
  59. * @param test
  60. */
  61. public function testReadAnnotationNoLowercase(){
  62. $reader = new ControllerMethodReflector();
  63. $reader->reflect(
  64. '\OC\AppFramework\Utility\ControllerMethodReflectorTest',
  65. 'testReadAnnotationNoLowercase'
  66. );
  67. $this->assertTrue($reader->hasAnnotation('Annotation'));
  68. $this->assertFalse($reader->hasAnnotation('param'));
  69. }
  70. /**
  71. * @Annotation
  72. * @param int $test
  73. */
  74. public function testReadTypeIntAnnotations(){
  75. $reader = new ControllerMethodReflector();
  76. $reader->reflect(
  77. '\OC\AppFramework\Utility\ControllerMethodReflectorTest',
  78. 'testReadTypeIntAnnotations'
  79. );
  80. $this->assertEquals('int', $reader->getType('test'));
  81. }
  82. /**
  83. * @Annotation
  84. * @param double $test something special
  85. */
  86. public function testReadTypeDoubleAnnotations(){
  87. $reader = new ControllerMethodReflector();
  88. $reader->reflect(
  89. '\OC\AppFramework\Utility\ControllerMethodReflectorTest',
  90. 'testReadTypeDoubleAnnotations'
  91. );
  92. $this->assertEquals('double', $reader->getType('test'));
  93. }
  94. public function arguments($arg, $arg2='hi') {}
  95. public function testReflectParameters() {
  96. $reader = new ControllerMethodReflector();
  97. $reader->reflect(
  98. '\OC\AppFramework\Utility\ControllerMethodReflectorTest',
  99. 'arguments'
  100. );
  101. $this->assertEquals(array('arg' => null, 'arg2' => 'hi'), $reader->getParameters());
  102. }
  103. public function arguments2($arg) {}
  104. public function testReflectParameters2() {
  105. $reader = new ControllerMethodReflector();
  106. $reader->reflect(
  107. '\OC\AppFramework\Utility\ControllerMethodReflectorTest',
  108. 'arguments2'
  109. );
  110. $this->assertEquals(array('arg' => null), $reader->getParameters());
  111. }
  112. public function testInheritance() {
  113. $reader = new ControllerMethodReflector();
  114. $reader->reflect('OC\AppFramework\Utility\EndController', 'test');
  115. $this->assertTrue($reader->hasAnnotation('Annotation'));
  116. }
  117. public function testInheritanceOverride() {
  118. $reader = new ControllerMethodReflector();
  119. $reader->reflect('OC\AppFramework\Utility\EndController', 'test2');
  120. $this->assertTrue($reader->hasAnnotation('NoAnnotation'));
  121. $this->assertFalse($reader->hasAnnotation('Annotation'));
  122. }
  123. public function testInheritanceOverrideNoDocblock() {
  124. $reader = new ControllerMethodReflector();
  125. $reader->reflect('OC\AppFramework\Utility\EndController', 'test3');
  126. $this->assertFalse($reader->hasAnnotation('Annotation'));
  127. }
  128. }