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.

101 lines
3.4 KiB

  1. <?php
  2. namespace Friendica\Test\src\App;
  3. use Friendica\App\Router;
  4. use Friendica\Module;
  5. use PHPUnit\Framework\TestCase;
  6. class RouterTest extends TestCase
  7. {
  8. public function testGetModuleClass()
  9. {
  10. $router = new Router(['GET']);
  11. $routeCollector = $router->getRouteCollector();
  12. $routeCollector->addRoute(['GET'], '/', 'IndexModuleClassName');
  13. $routeCollector->addRoute(['GET'], '/test', 'TestModuleClassName');
  14. $routeCollector->addRoute(['GET'], '/test/sub', 'TestSubModuleClassName');
  15. $routeCollector->addRoute(['GET'], '/optional[/option]', 'OptionalModuleClassName');
  16. $routeCollector->addRoute(['GET'], '/variable/{var}', 'VariableModuleClassName');
  17. $routeCollector->addRoute(['GET'], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
  18. $routeCollector->addRoute(['POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'], '/unsupported', 'UnsupportedMethodModuleClassName');
  19. $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
  20. $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
  21. $this->assertNull($router->getModuleClass('/tes'));
  22. $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
  23. $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
  24. $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
  25. $this->assertNull($router->getModuleClass('/optional/opt'));
  26. $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
  27. $this->assertNull($router->getModuleClass('/variable'));
  28. $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
  29. $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
  30. $this->assertNull($router->getModuleClass('/unsupported'));
  31. }
  32. public function dataRoutes()
  33. {
  34. return [
  35. 'default' => [
  36. 'routes' => [
  37. '/' => [Module\Home::class, [Router::GET]],
  38. '/group' => [
  39. '/route' => [Module\Friendica::class, [Router::GET]],
  40. ],
  41. '/group2' => [
  42. '/group3' => [
  43. '/route' => [Module\Xrd::class, [Router::GET]],
  44. ],
  45. ],
  46. '/post' => [
  47. '/it' => [Module\NodeInfo::class, [Router::POST]],
  48. ],
  49. '/double' => [Module\Profile::class, [Router::GET, Router::POST]]
  50. ],
  51. ],
  52. ];
  53. }
  54. /**
  55. * @dataProvider dataRoutes
  56. */
  57. public function testGetRoutes(array $routes)
  58. {
  59. $router = (new Router([
  60. 'REQUEST_METHOD' => Router::GET
  61. ]))->addRoutes($routes);
  62. $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
  63. $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
  64. $this->assertEquals(Module\Xrd::class, $router->getModuleClass('/group2/group3/route'));
  65. $this->assertNull($router->getModuleClass('/post/it'));
  66. $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));
  67. }
  68. /**
  69. * @dataProvider dataRoutes
  70. */
  71. public function testPostRouter(array $routes)
  72. {
  73. $router = (new Router([
  74. 'REQUEST_METHOD' => Router::POST
  75. ]))->addRoutes($routes);
  76. // Don't find GET
  77. $this->assertNull($router->getModuleClass('/'));
  78. $this->assertNull($router->getModuleClass('/group/route'));
  79. $this->assertNull($router->getModuleClass('/group2/group3/route'));
  80. $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));
  81. $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));
  82. }
  83. }