Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

184 lines
6.7KB

  1. <?php
  2. namespace Friendica\Test\src\App;
  3. use Friendica\App\Router;
  4. use Friendica\Module;
  5. use Friendica\Network\HTTPException\MethodNotAllowedException;
  6. use Friendica\Network\HTTPException\NotFoundException;
  7. use PHPUnit\Framework\TestCase;
  8. class RouterTest extends TestCase
  9. {
  10. public function testGetModuleClass()
  11. {
  12. $router = new Router(['REQUEST_METHOD' => Router::GET]);
  13. $routeCollector = $router->getRouteCollector();
  14. $routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName');
  15. $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
  16. $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName');
  17. $routeCollector->addRoute([Router::GET], '/test/sub', 'TestSubModuleClassName');
  18. $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
  19. $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
  20. $routeCollector->addRoute([Router::GET], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
  21. $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
  22. $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
  23. $this->assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost'));
  24. $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
  25. $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
  26. $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
  27. $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
  28. $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
  29. $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
  30. }
  31. public function testPostModuleClass()
  32. {
  33. $router = new Router(['REQUEST_METHOD' => Router::POST]);
  34. $routeCollector = $router->getRouteCollector();
  35. $routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName');
  36. $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
  37. $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName');
  38. $routeCollector->addRoute([Router::POST], '/test/sub', 'TestSubModuleClassName');
  39. $routeCollector->addRoute([Router::POST], '/optional[/option]', 'OptionalModuleClassName');
  40. $routeCollector->addRoute([Router::POST], '/variable/{var}', 'VariableModuleClassName');
  41. $routeCollector->addRoute([Router::POST], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
  42. $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
  43. $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
  44. $this->assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost'));
  45. $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
  46. $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
  47. $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
  48. $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
  49. $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
  50. $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
  51. }
  52. public function testGetModuleClassNotFound()
  53. {
  54. $this->expectException(NotFoundException::class);
  55. $router = new Router(['REQUEST_METHOD' => Router::GET]);
  56. $router->getModuleClass('/unsupported');
  57. }
  58. public function testGetModuleClassNotFoundTypo()
  59. {
  60. $this->expectException(NotFoundException::class);
  61. $router = new Router(['REQUEST_METHOD' => Router::GET]);
  62. $routeCollector = $router->getRouteCollector();
  63. $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
  64. $router->getModuleClass('/tes');
  65. }
  66. public function testGetModuleClassNotFoundOptional()
  67. {
  68. $this->expectException(NotFoundException::class);
  69. $router = new Router(['REQUEST_METHOD' => Router::GET]);
  70. $routeCollector = $router->getRouteCollector();
  71. $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
  72. $router->getModuleClass('/optional/opt');
  73. }
  74. public function testGetModuleClassNotFoundVariable()
  75. {
  76. $this->expectException(NotFoundException::class);
  77. $router = new Router(['REQUEST_METHOD' => Router::GET]);
  78. $routeCollector = $router->getRouteCollector();
  79. $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
  80. $router->getModuleClass('/variable');
  81. }
  82. public function testGetModuleClassMethodNotAllowed()
  83. {
  84. $this->expectException(MethodNotAllowedException::class);
  85. $router = new Router(['REQUEST_METHOD' => Router::POST]);
  86. $routeCollector = $router->getRouteCollector();
  87. $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
  88. $router->getModuleClass('/test');
  89. }
  90. public function testPostModuleClassMethodNotAllowed()
  91. {
  92. $this->expectException(MethodNotAllowedException::class);
  93. $router = new Router(['REQUEST_METHOD' => Router::GET]);
  94. $routeCollector = $router->getRouteCollector();
  95. $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
  96. $router->getModuleClass('/test');
  97. }
  98. public function dataRoutes()
  99. {
  100. return [
  101. 'default' => [
  102. 'routes' => [
  103. '/' => [Module\Home::class, [Router::GET]],
  104. '/group' => [
  105. '/route' => [Module\Friendica::class, [Router::GET]],
  106. ],
  107. '/group2' => [
  108. '/group3' => [
  109. '/route' => [Module\Xrd::class, [Router::GET]],
  110. ],
  111. ],
  112. '/post' => [
  113. '/it' => [Module\NodeInfo::class, [Router::POST]],
  114. ],
  115. '/double' => [Module\Profile::class, [Router::GET, Router::POST]]
  116. ],
  117. ],
  118. ];
  119. }
  120. /**
  121. * @dataProvider dataRoutes
  122. */
  123. public function testGetRoutes(array $routes)
  124. {
  125. $router = (new Router([
  126. 'REQUEST_METHOD' => Router::GET
  127. ]))->loadRoutes($routes);
  128. $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
  129. $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
  130. $this->assertEquals(Module\Xrd::class, $router->getModuleClass('/group2/group3/route'));
  131. $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));
  132. }
  133. /**
  134. * @dataProvider dataRoutes
  135. */
  136. public function testPostRouter(array $routes)
  137. {
  138. $router = (new Router([
  139. 'REQUEST_METHOD' => Router::POST
  140. ]))->loadRoutes($routes);
  141. // Don't find GET
  142. $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));
  143. $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));
  144. }
  145. }