ControllerMethodReflectorTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 Test\AppFramework\Utility;
  23. use OC\AppFramework\Utility\ControllerMethodReflector;
  24. class BaseController {
  25. /**
  26. * @Annotation
  27. */
  28. public function test(){}
  29. /**
  30. * @Annotation
  31. */
  32. public function test2(){}
  33. /**
  34. * @Annotation
  35. */
  36. public function test3(){}
  37. }
  38. class MiddleController extends BaseController {
  39. /**
  40. * @NoAnnotation
  41. */
  42. public function test2() {}
  43. public function test3() {}
  44. }
  45. class EndController extends MiddleController {}
  46. class ControllerMethodReflectorTest extends \Test\TestCase {
  47. /**
  48. * @Annotation
  49. */
  50. public function testReadAnnotation(){
  51. $reader = new ControllerMethodReflector();
  52. $reader->reflect(
  53. '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
  54. 'testReadAnnotation'
  55. );
  56. $this->assertTrue($reader->hasAnnotation('Annotation'));
  57. }
  58. /**
  59. * @Annotation(parameter=value)
  60. */
  61. public function testGetAnnotationParameterSingle() {
  62. $reader = new ControllerMethodReflector();
  63. $reader->reflect(
  64. __CLASS__,
  65. __FUNCTION__
  66. );
  67. $this->assertSame('value', $reader->getAnnotationParameter('Annotation', 'parameter'));
  68. }
  69. /**
  70. * @Annotation(parameter1=value1, parameter2=value2,parameter3=value3)
  71. */
  72. public function testGetAnnotationParameterMultiple() {
  73. $reader = new ControllerMethodReflector();
  74. $reader->reflect(
  75. __CLASS__,
  76. __FUNCTION__
  77. );
  78. $this->assertSame('value1', $reader->getAnnotationParameter('Annotation', 'parameter1'));
  79. $this->assertSame('value2', $reader->getAnnotationParameter('Annotation', 'parameter2'));
  80. $this->assertSame('value3', $reader->getAnnotationParameter('Annotation', 'parameter3'));
  81. }
  82. /**
  83. * @Annotation
  84. * @param test
  85. */
  86. public function testReadAnnotationNoLowercase(){
  87. $reader = new ControllerMethodReflector();
  88. $reader->reflect(
  89. '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
  90. 'testReadAnnotationNoLowercase'
  91. );
  92. $this->assertTrue($reader->hasAnnotation('Annotation'));
  93. $this->assertFalse($reader->hasAnnotation('param'));
  94. }
  95. /**
  96. * @Annotation
  97. * @param int $test
  98. */
  99. public function testReadTypeIntAnnotations(){
  100. $reader = new ControllerMethodReflector();
  101. $reader->reflect(
  102. '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
  103. 'testReadTypeIntAnnotations'
  104. );
  105. $this->assertEquals('int', $reader->getType('test'));
  106. }
  107. /**
  108. * @Annotation
  109. * @param int $a
  110. * @param int $b
  111. */
  112. public function arguments3($a, float $b, int $c, $d){}
  113. /**
  114. * @requires PHP 7
  115. */
  116. public function testReadTypeIntAnnotationsScalarTypes(){
  117. $reader = new ControllerMethodReflector();
  118. $reader->reflect(
  119. '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
  120. 'arguments3'
  121. );
  122. $this->assertEquals('int', $reader->getType('a'));
  123. $this->assertEquals('float', $reader->getType('b'));
  124. $this->assertEquals('int', $reader->getType('c'));
  125. $this->assertNull($reader->getType('d'));
  126. }
  127. /**
  128. * @Annotation
  129. * @param double $test something special
  130. */
  131. public function testReadTypeDoubleAnnotations(){
  132. $reader = new ControllerMethodReflector();
  133. $reader->reflect(
  134. '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
  135. 'testReadTypeDoubleAnnotations'
  136. );
  137. $this->assertEquals('double', $reader->getType('test'));
  138. }
  139. /**
  140. * @Annotation
  141. * @param string $foo
  142. */
  143. public function testReadTypeWhitespaceAnnotations(){
  144. $reader = new ControllerMethodReflector();
  145. $reader->reflect(
  146. '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
  147. 'testReadTypeWhitespaceAnnotations'
  148. );
  149. $this->assertEquals('string', $reader->getType('foo'));
  150. }
  151. public function arguments($arg, $arg2='hi') {}
  152. public function testReflectParameters() {
  153. $reader = new ControllerMethodReflector();
  154. $reader->reflect(
  155. '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
  156. 'arguments'
  157. );
  158. $this->assertEquals(array('arg' => null, 'arg2' => 'hi'), $reader->getParameters());
  159. }
  160. public function arguments2($arg) {}
  161. public function testReflectParameters2() {
  162. $reader = new ControllerMethodReflector();
  163. $reader->reflect(
  164. '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
  165. 'arguments2'
  166. );
  167. $this->assertEquals(array('arg' => null), $reader->getParameters());
  168. }
  169. public function testInheritance() {
  170. $reader = new ControllerMethodReflector();
  171. $reader->reflect('Test\AppFramework\Utility\EndController', 'test');
  172. $this->assertTrue($reader->hasAnnotation('Annotation'));
  173. }
  174. public function testInheritanceOverride() {
  175. $reader = new ControllerMethodReflector();
  176. $reader->reflect('Test\AppFramework\Utility\EndController', 'test2');
  177. $this->assertTrue($reader->hasAnnotation('NoAnnotation'));
  178. $this->assertFalse($reader->hasAnnotation('Annotation'));
  179. }
  180. public function testInheritanceOverrideNoDocblock() {
  181. $reader = new ControllerMethodReflector();
  182. $reader->reflect('Test\AppFramework\Utility\EndController', 'test3');
  183. $this->assertFalse($reader->hasAnnotation('Annotation'));
  184. }
  185. }