Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuRouteTest.php 6.2KB

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