3 namespace Friendica\Test\src\App;
5 use Friendica\App\Router;
7 use PHPUnit\Framework\TestCase;
9 class RouterTest extends TestCase
11 public function testGetModuleClass()
13 $router = new Router(['GET']);
15 $routeCollector = $router->getRouteCollector();
16 $routeCollector->addRoute(['GET'], '/', 'IndexModuleClassName');
17 $routeCollector->addRoute(['GET'], '/test', 'TestModuleClassName');
18 $routeCollector->addRoute(['GET'], '/test/sub', 'TestSubModuleClassName');
19 $routeCollector->addRoute(['GET'], '/optional[/option]', 'OptionalModuleClassName');
20 $routeCollector->addRoute(['GET'], '/variable/{var}', 'VariableModuleClassName');
21 $routeCollector->addRoute(['GET'], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
22 $routeCollector->addRoute(['POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'], '/unsupported', 'UnsupportedMethodModuleClassName');
24 $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
26 $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
27 $this->assertNull($router->getModuleClass('/tes'));
29 $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
31 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
32 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
33 $this->assertNull($router->getModuleClass('/optional/opt'));
35 $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
36 $this->assertNull($router->getModuleClass('/variable'));
38 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
39 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
41 $this->assertNull($router->getModuleClass('/unsupported'));
44 public function dataRoutes()
49 '/' => [Module\Home::class, [Router::GET]],
51 '/route' => [Module\Friendica::class, [Router::GET]],
57 '/route' => [Module\Xrd::class, [Router::GET]],
61 '/it' => [Module\NodeInfo::class, [Router::POST]],
63 '/double' => [Module\Profile::class, [Router::GET, Router::POST]]
70 * @dataProvider dataRoutes
72 public function testGetRoutes(array $routes)
74 $router = (new Router([
75 'REQUEST_METHOD' => Router::GET
76 ]))->addRoutes($routes);
78 $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
79 $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
80 $this->assertEquals(Module\Xrd::class, $router->getModuleClass('/group2/group3/route'));
81 $this->assertNull($router->getModuleClass('/post/it'));
82 $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));
86 * @dataProvider dataRoutes
88 public function testPostRouter(array $routes)
90 $router = (new Router([
91 'REQUEST_METHOD' => Router::POST
92 ]))->addRoutes($routes);
95 $this->assertNull($router->getModuleClass('/'));
96 $this->assertNull($router->getModuleClass('/group/route'));
97 $this->assertNull($router->getModuleClass('/group2/group3/route'));
98 $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));
99 $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));