3 namespace Friendica\Test\src\App;
5 use Friendica\App\Router;
7 use Friendica\Network\HTTPException\MethodNotAllowedException;
8 use Friendica\Network\HTTPException\NotFoundException;
9 use PHPUnit\Framework\TestCase;
11 class RouterTest extends TestCase
13 public function testGetModuleClass()
15 $router = new Router(['REQUEST_METHOD' => 'GET']);
17 $routeCollector = $router->getRouteCollector();
18 $routeCollector->addRoute(['GET'], '/', 'IndexModuleClassName');
19 $routeCollector->addRoute(['GET'], '/test', 'TestModuleClassName');
20 $routeCollector->addRoute(['GET'], '/test/sub', 'TestSubModuleClassName');
21 $routeCollector->addRoute(['GET'], '/optional[/option]', 'OptionalModuleClassName');
22 $routeCollector->addRoute(['GET'], '/variable/{var}', 'VariableModuleClassName');
23 $routeCollector->addRoute(['GET'], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
24 $routeCollector->addRoute(['POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'], '/unsupported', 'UnsupportedMethodModuleClassName');
26 $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
27 $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
28 $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
29 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
30 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
31 $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
32 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
33 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
36 public function testGetModuleClassNotFound()
38 $this->expectException(NotFoundException::class);
40 $router = new Router(['REQUEST_METHOD' => 'GET']);
42 $router->getModuleClass('/unsupported');
45 public function testGetModuleClassNotFoundTypo()
47 $this->expectException(NotFoundException::class);
49 $router = new Router(['REQUEST_METHOD' => 'GET']);
51 $routeCollector = $router->getRouteCollector();
52 $routeCollector->addRoute(['GET'], '/test', 'TestModuleClassName');
54 $router->getModuleClass('/tes');
57 public function testGetModuleClassNotFoundOptional()
59 $this->expectException(NotFoundException::class);
61 $router = new Router(['REQUEST_METHOD' => 'GET']);
63 $routeCollector = $router->getRouteCollector();
64 $routeCollector->addRoute(['GET'], '/optional[/option]', 'OptionalModuleClassName');
66 $router->getModuleClass('/optional/opt');
69 public function testGetModuleClassNotFoundVariable()
71 $this->expectException(NotFoundException::class);
73 $router = new Router(['REQUEST_METHOD' => 'GET']);
75 $routeCollector = $router->getRouteCollector();
76 $routeCollector->addRoute(['GET'], '/variable/{var}', 'VariableModuleClassName');
78 $router->getModuleClass('/variable');
81 public function testGetModuleClassMethodNotAllowed()
83 $this->expectException(MethodNotAllowedException::class);
85 $router = new Router(['REQUEST_METHOD' => 'POST']);
87 $routeCollector = $router->getRouteCollector();
88 $routeCollector->addRoute(['GET'], '/test', 'TestModuleClassName');
90 $router->getModuleClass('/test');
93 public function dataRoutes()
98 '/' => [Module\Home::class, [Router::GET]],
100 '/route' => [Module\Friendica::class, [Router::GET]],
106 '/route' => [Module\Xrd::class, [Router::GET]],
110 '/it' => [Module\NodeInfo::class, [Router::POST]],
112 '/double' => [Module\Profile::class, [Router::GET, Router::POST]]
119 * @dataProvider dataRoutes
121 public function testGetRoutes(array $routes)
123 $router = (new Router([
124 'REQUEST_METHOD' => Router::GET
125 ]))->addRoutes($routes);
127 $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
128 $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
129 $this->assertEquals(Module\Xrd::class, $router->getModuleClass('/group2/group3/route'));
130 $this->assertNull($router->getModuleClass('/post/it'));
131 $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));
135 * @dataProvider dataRoutes
137 public function testPostRouter(array $routes)
139 $router = (new Router([
140 'REQUEST_METHOD' => Router::POST
141 ]))->addRoutes($routes);
144 $this->assertNull($router->getModuleClass('/'));
145 $this->assertNull($router->getModuleClass('/group/route'));
146 $this->assertNull($router->getModuleClass('/group2/group3/route'));
147 $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));
148 $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));