You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 extends LuAbstractMiddleware {
  55. public function onBefore(LuRouteDbo $route, $parameters)
  56. {
  57. $parameters["_user"] = "\"my user\"";
  58. return $parameters;
  59. }
  60. public function onAfter(LuRouteDbo $route, $parameters, $result)
  61. {
  62. return $result;
  63. }
  64. }
  65. class AfterMiddleware extends LuAbstractMiddleware {
  66. public function onBefore(LuRouteDbo $route, $parameters)
  67. {
  68. return $parameters;
  69. }
  70. public function onAfter(LuRouteDbo $route, $parameters, $result)
  71. {
  72. return "AfterMiddleware";
  73. }
  74. }
  75. class AuthenticationMiddleware extends LuAbstractMiddleware {
  76. public function onBefore(LuRouteDbo $route, $parameters)
  77. {
  78. $permissions = $parameters['_middleware']['permissions'] ?? [];
  79. if ($permissions[0] != "ALLOWED") {
  80. throw new LuBusinessException("Unauthorized", 401);
  81. }
  82. return $parameters;
  83. }
  84. public function onAfter(LuRouteDbo $route, $parameters, $result)
  85. {
  86. return $result;
  87. }
  88. }
  89. class LuRouteTest extends \PHPUnit_Framework_TestCase
  90. {
  91. public function testBasic()
  92. {
  93. $router = new LuRoute();
  94. $router->addRoute("GET", "/test", '\BasicTestController', "testBasic");
  95. $router->setup();
  96. $res = $router->dispatch("GET", "/test", []);
  97. $this->assertSame("Test. succeeded", $res);
  98. }
  99. public function testParameter()
  100. {
  101. $router = new LuRoute();
  102. $router->addRoute("GET", "/test", '\BasicTestController', "testParameter");
  103. $router->setup();
  104. $res = $router->dispatch("GET", "/test", ["test" => "A value"]);
  105. $this->assertSame("A value", $res);
  106. }
  107. public function testTypedParameter()
  108. {
  109. $router = new LuRoute();
  110. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameter");
  111. $router->setup();
  112. $res = $router->dispatch("GET", "/test", ["test" => '{"test": "Some value"}']);
  113. $this->assertSame("Some value", $res);
  114. }
  115. public function testTypedParameterDoc()
  116. {
  117. $router = new LuRoute();
  118. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
  119. $router->setup();
  120. $res = $router->dispatch("GET", "/test", ["test" => '{"test": "42"}']);
  121. $this->assertSame("42", $res);
  122. }
  123. public function testNullable()
  124. {
  125. $router = new LuRoute();
  126. $router->addRoute("GET", "/test", '\BasicTestController', "testNullable");
  127. $router->setup();
  128. $res = $router->dispatch("GET", "/test", ["test" => 'null']);
  129. $this->assertSame(true, $res);
  130. }
  131. public function testBeforeMiddleware()
  132. {
  133. $router = new LuRoute();
  134. $router->addRoute("GET", "/test", '\BasicTestController', "testBeforeMiddleware", [], ['BeforeMiddleware']);
  135. $router->setup();
  136. $res = $router->dispatch("GET", "/test", []);
  137. $this->assertSame("my user", $res);
  138. }
  139. public function testAfterMiddleware()
  140. {
  141. $router = new LuRoute();
  142. $router->addRoute("GET", "/test", '\BasicTestController', "testAfterMiddleware", [], ['AfterMiddleware']);
  143. $router->setup();
  144. $res = $router->dispatch("GET", "/test", []);
  145. $this->assertSame("AfterMiddleware", $res);
  146. }
  147. public function testAuthenticationMiddlewareAllowed()
  148. {
  149. $router = new LuRoute();
  150. $router->addRoute("GET", "/test", '\BasicTestController', "testBasic",
  151. ["permissions" => ["ALLOWED"]], ['AuthenticationMiddleware']);
  152. $router->setup();
  153. $res = $router->dispatch("GET", "/test", []);
  154. $this->assertSame("Test. succeeded", $res);
  155. }
  156. public function testAuthenticationMiddlewareNotAllowed()
  157. {
  158. $this->expectException(LuBusinessException::class);
  159. $router = new LuRoute();
  160. $router->addRoute("GET", "/test", '\BasicTestController', "testBasic",
  161. ["permissions" => ["NOT_ALLOWED"]], ['AuthenticationMiddleware']);
  162. $router->setup();
  163. $router->dispatch("GET", "/test", []);
  164. }
  165. public function testTypedParameterDocConstraint()
  166. {
  167. $this->expectException(LuBusinessException::class);
  168. $router = new LuRoute();
  169. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
  170. $router->setup();
  171. $router->dispatch("GET", "/test", ["test" => '{"Test": "Test."}']);
  172. }
  173. public function testMethodNotAllowed()
  174. {
  175. $this->expectException(LuBusinessException::class);
  176. $router = new LuRoute();
  177. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
  178. $router->setup();
  179. $router->dispatch("POST", "/test", ["test" => '{"Test": "Test."}']);
  180. }
  181. public function testRouteNotFound()
  182. {
  183. $this->expectException(LuBusinessException::class);
  184. $router = new LuRoute();
  185. $router->addRoute("GET", "/test", '\BasicTestController', "testTypedParameterDoc");
  186. $router->setup();
  187. $router->dispatch("GET", "/notfound", ["test" => '{"Test": "Test."}']);
  188. }
  189. }