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