Boilerplate to use a Directus Instance to Build a Custom Website, Content will be Manage by Directus
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.

266 lines
6.9 KiB

5 years ago
  1. <?php
  2. /**
  3. * Flight: An extensible micro-framework.
  4. *
  5. * @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
  6. * @license MIT, http://flightphp.com/license
  7. */
  8. require_once 'vendor/autoload.php';
  9. require_once __DIR__.'/../flight/autoload.php';
  10. class RouterTest extends PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * @var \flight\net\Router
  14. */
  15. private $router;
  16. /**
  17. * @var \flight\net\Request
  18. */
  19. private $request;
  20. /**
  21. * @var \flight\core\Dispatcher
  22. */
  23. private $dispatcher;
  24. function setUp(){
  25. $this->router = new \flight\net\Router();
  26. $this->request = new \flight\net\Request();
  27. $this->dispatcher = new \flight\core\Dispatcher();
  28. }
  29. // Simple output
  30. function ok(){
  31. echo 'OK';
  32. }
  33. // Checks if a route was matched with a given output
  34. function check($str = '') {
  35. /*
  36. $route = $this->router->route($this->request);
  37. $params = array_values($route->params);
  38. $this->assertTrue(is_callable($route->callback));
  39. call_user_func_array($route->callback, $params);
  40. */
  41. $this->routeRequest();
  42. $this->expectOutputString($str);
  43. }
  44. function routeRequest() {
  45. $dispatched = false;
  46. while ($route = $this->router->route($this->request)) {
  47. $params = array_values($route->params);
  48. if ($route->pass) {
  49. $params[] = $route;
  50. }
  51. $continue = $this->dispatcher->execute(
  52. $route->callback,
  53. $params
  54. );
  55. $dispatched = true;
  56. if (!$continue) break;
  57. $this->router->next();
  58. $dispatched = false;
  59. }
  60. if (!$dispatched) {
  61. echo '404';
  62. }
  63. }
  64. // Default route
  65. function testDefaultRoute(){
  66. $this->router->map('/', array($this, 'ok'));
  67. $this->request->url = '/';
  68. $this->check('OK');
  69. }
  70. // Simple path
  71. function testPathRoute(){
  72. $this->router->map('/path', array($this, 'ok'));
  73. $this->request->url = '/path';
  74. $this->check('OK');
  75. }
  76. // POST route
  77. function testPostRoute(){
  78. $this->router->map('POST /', array($this, 'ok'));
  79. $this->request->url = '/';
  80. $this->request->method = 'POST';
  81. $this->check('OK');
  82. }
  83. // Either GET or POST route
  84. function testGetPostRoute(){
  85. $this->router->map('GET|POST /', array($this, 'ok'));
  86. $this->request->url = '/';
  87. $this->request->method = 'GET';
  88. $this->check('OK');
  89. }
  90. // Test regular expression matching
  91. function testRegEx(){
  92. $this->router->map('/num/[0-9]+', array($this, 'ok'));
  93. $this->request->url = '/num/1234';
  94. $this->check('OK');
  95. }
  96. // Passing URL parameters
  97. function testUrlParameters(){
  98. $this->router->map('/user/@id', function($id){
  99. echo $id;
  100. });
  101. $this->request->url = '/user/123';
  102. $this->check('123');
  103. }
  104. // Passing URL parameters matched with regular expression
  105. function testRegExParameters(){
  106. $this->router->map('/test/@name:[a-z]+', function($name){
  107. echo $name;
  108. });
  109. $this->request->url = '/test/abc';
  110. $this->check('abc');
  111. }
  112. // Optional parameters
  113. function testOptionalParameters(){
  114. $this->router->map('/blog(/@year(/@month(/@day)))', function($year, $month, $day){
  115. echo "$year,$month,$day";
  116. });
  117. $this->request->url = '/blog/2000';
  118. $this->check('2000,,');
  119. }
  120. // Regex in optional parameters
  121. function testRegexOptionalParameters(){
  122. $this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){
  123. echo "$controller,$method,$id";
  124. });
  125. $this->request->url = '/user/delete/123';
  126. $this->check('user,delete,123');
  127. }
  128. // Regex in optional parameters
  129. function testRegexEmptyOptionalParameters(){
  130. $this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){
  131. echo "$controller,$method,$id";
  132. });
  133. $this->request->url = '/user/delete/';
  134. $this->check('user,delete,');
  135. }
  136. // Wildcard matching
  137. function testWildcard(){
  138. $this->router->map('/account/*', array($this, 'ok'));
  139. $this->request->url = '/account/123/abc/xyz';
  140. $this->check('OK');
  141. }
  142. // Check if route object was passed
  143. function testRouteObjectPassing(){
  144. $this->router->map('/yes_route', function($route){
  145. $this->assertTrue(is_object($route));
  146. $this->assertTrue(is_array($route->methods));
  147. $this->assertTrue(is_array($route->params));
  148. $this->assertEquals(sizeof($route->params), 0);
  149. $this->assertEquals($route->regex, null);
  150. $this->assertEquals($route->splat, '');
  151. $this->assertTrue($route->pass);
  152. }, true);
  153. $this->request->url = '/yes_route';
  154. $this->check();
  155. $this->router->map('/no_route', function($route = null){
  156. $this->assertTrue(is_null($route));
  157. }, false);
  158. $this->request->url = '/no_route';
  159. $this->check();
  160. }
  161. function testRouteWithParameters() {
  162. $this->router->map('/@one/@two', function($one, $two, $route){
  163. $this->assertEquals(sizeof($route->params), 2);
  164. $this->assertEquals($route->params['one'], $one);
  165. $this->assertEquals($route->params['two'], $two);
  166. }, true);
  167. $this->request->url = '/1/2';
  168. $this->check();
  169. }
  170. // Test splat
  171. function testSplatWildcard(){
  172. $this->router->map('/account/*', function($route){
  173. echo $route->splat;
  174. }, true);
  175. $this->request->url = '/account/456/def/xyz';
  176. $this->check('456/def/xyz');
  177. }
  178. // Test splat without trailing slash
  179. function testSplatWildcardTrailingSlash(){
  180. $this->router->map('/account/*', function($route){
  181. echo $route->splat;
  182. }, true);
  183. $this->request->url = '/account';
  184. $this->check();
  185. }
  186. // Test splat with named parameters
  187. function testSplatNamedPlusWildcard(){
  188. $this->router->map('/account/@name/*', function($name, $route){
  189. echo $route->splat;
  190. $this->assertEquals('abc', $name);
  191. }, true);
  192. $this->request->url = '/account/abc/456/def/xyz';
  193. $this->check('456/def/xyz');
  194. }
  195. // Test not found
  196. function testNotFound() {
  197. $this->router->map('/does_exist', array($this, 'ok'));
  198. $this->request->url = '/does_not_exist';
  199. $this->check('404');
  200. }
  201. // Test case sensitivity
  202. function testCaseSensitivity() {
  203. $this->router->map('/hello', array($this, 'ok'));
  204. $this->request->url = '/HELLO';
  205. $this->router->case_sensitive = true;
  206. $this->check('404');
  207. }
  208. }