您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuRouteTest.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. use Luticate\Utils\Business\LuBusinessException;
  3. use Luticate\Utils\Controller\LuRoute;
  4. use Luticate\Utils\Dbo\LuRouteDbo;
  5. use Luticate\Utils\Dbo\LuStringDbo;
  6. use Luticate\Utils\Middleware\LuAbstractMiddleware;
  7. /**
  8. * Created by PhpStorm.
  9. * User: robin
  10. * Date: 6/7/16
  11. * Time: 2:33 AM
  12. */
  13. class BasicTestController
  14. {
  15. public function testBasic()
  16. {
  17. return "Test. succeeded";
  18. }
  19. public function testParameter($test)
  20. {
  21. return $test;
  22. }
  23. public function testTypedParameter(TestDbo $test)
  24. {
  25. return $test->getTest();
  26. }
  27. /**
  28. * @param TestDbo $test
  29. * @ensure42
  30. * @return string
  31. */
  32. public function testTypedParameterDoc(TestDbo $test)
  33. {
  34. return $test->getTest();
  35. }
  36. /**
  37. * @param TestDbo $test
  38. * @nullable
  39. * @return string
  40. */
  41. public function testNullable(TestDbo $test = null)
  42. {
  43. return is_null($test);
  44. }
  45. public function testBeforeMiddleware(LuStringDbo $_user)
  46. {
  47. return $_user->getString();
  48. }
  49. public function testAfterMiddleware()
  50. {
  51. return "A result";
  52. }
  53. }
  54. class BeforeMiddleware implements LuAbstractMiddleware {
  55. public function onBefore($_parameters)
  56. {
  57. $_parameters["_user"] = "\"my user\"";
  58. return $_parameters;
  59. }
  60. }
  61. class AfterMiddleware implements LuAbstractMiddleware {
  62. public function onAfter()
  63. {
  64. return "AfterMiddleware";
  65. }
  66. }
  67. class AuthenticationMiddleware implements LuAbstractMiddleware {
  68. public function onBefore($_parameters, $permissions = [])
  69. {
  70. if ($permissions[0] != "ALLOWED") {
  71. throw new LuBusinessException("Unauthorized", 401);
  72. }
  73. return $_parameters;
  74. }
  75. }
  76. class LuRouteTest extends \PHPUnit_Framework_TestCase
  77. {
  78. public function testBasic()
  79. {
  80. $router = new LuRoute();
  81. $router->addRoute("GET", "/test", '\BasicTestController', "testBasic");
  82. $router->setup();
  83. $res = $router->dispatch("GET", "/test", []);
  84. $this->assertSame("Test. succeeded", $res);
  85. }
  86. public function testParameter()
  87. {
  88. $router = new LuRoute();
  89. $router->addRoute("GET", "/test", '\BasicTestController', "testParameter");
  90. $router->setup();
  91. $res = $router->dispatch("GET", "/test", ["test" => "A value"]);
  92. $this->assertSame("A value", $res);
  93. }
  94. public function testTypedParameter()
  95. {
  96. $router = new LuRoute();
  97. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameter");
  98. $router->setup();
  99. $res = $router->dispatch("GET", "/test", ["test" => '{"test": "Some value"}']);
  100. $this->assertSame("Some value", $res);
  101. }
  102. public function testTypedParameterDoc()
  103. {
  104. $router = new LuRoute();
  105. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
  106. $router->setup();
  107. $res = $router->dispatch("GET", "/test", ["test" => '{"test": "42"}']);
  108. $this->assertSame("42", $res);
  109. }
  110. public function testNullable()
  111. {
  112. $router = new LuRoute();
  113. $router->addRoute("GET", "/test", '\BasicTestController', "testNullable");
  114. $router->setup();
  115. $res = $router->dispatch("GET", "/test", ["test" => 'null']);
  116. $this->assertSame(true, $res);
  117. }
  118. public function testBeforeMiddleware()
  119. {
  120. $router = new LuRoute();
  121. $router->addRoute("GET", "/test", '\BasicTestController', "testBeforeMiddleware", [], ['BeforeMiddleware']);
  122. $router->setup();
  123. $res = $router->dispatch("GET", "/test", []);
  124. $this->assertSame("my user", $res);
  125. }
  126. public function testAfterMiddleware()
  127. {
  128. $router = new LuRoute();
  129. $router->addRoute("GET", "/test", '\BasicTestController', "testAfterMiddleware", [], ['AfterMiddleware']);
  130. $router->setup();
  131. $res = $router->dispatch("GET", "/test", []);
  132. $this->assertSame("AfterMiddleware", $res);
  133. }
  134. public function testAuthenticationMiddlewareAllowed()
  135. {
  136. $router = new LuRoute();
  137. $router->addRoute("GET", "/test", '\BasicTestController', "testBasic",
  138. ["permissions" => ["ALLOWED"]], ['AuthenticationMiddleware']);
  139. $router->setup();
  140. $res = $router->dispatch("GET", "/test", []);
  141. $this->assertSame("Test. succeeded", $res);
  142. }
  143. public function testAuthenticationMiddlewareNotAllowed()
  144. {
  145. $this->expectException(LuBusinessException::class);
  146. $router = new LuRoute();
  147. $router->addRoute("GET", "/test", '\BasicTestController', "testBasic",
  148. ["permissions" => ["NOT_ALLOWED"]], ['AuthenticationMiddleware']);
  149. $router->setup();
  150. $router->dispatch("GET", "/test", []);
  151. }
  152. public function testTypedParameterDocConstraint()
  153. {
  154. $this->expectException(LuBusinessException::class);
  155. $router = new LuRoute();
  156. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
  157. $router->setup();
  158. $router->dispatch("GET", "/test", ["test" => '{"Test": "Test."}']);
  159. }
  160. public function testMethodNotAllowed()
  161. {
  162. $this->expectException(LuBusinessException::class);
  163. $router = new LuRoute();
  164. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
  165. $router->setup();
  166. $router->dispatch("POST", "/test", ["test" => '{"Test": "Test."}']);
  167. }
  168. public function testRouteNotFound()
  169. {
  170. $this->expectException(LuBusinessException::class);
  171. $router = new LuRoute();
  172. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
  173. $router->setup();
  174. $router->dispatch("GET", "/notfound", ["test" => '{"Test": "Test."}']);
  175. }
  176. }