ControllerMethodReflectorTest.php 6.3 KB

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