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' => Router::GET]);
17 $routeCollector = $router->getRouteCollector();
18 $routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName');
19 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
20 $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName');
21 $routeCollector->addRoute([Router::GET], '/test/sub', 'TestSubModuleClassName');
22 $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
23 $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
24 $routeCollector->addRoute([Router::GET], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
26 $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
27 $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
28 $this->assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost'));
29 $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
30 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
31 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
32 $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
33 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
34 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
37 public function testPostModuleClass()
39 $router = new Router(['REQUEST_METHOD' => Router::POST]);
41 $routeCollector = $router->getRouteCollector();
42 $routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName');
43 $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
44 $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName');
45 $routeCollector->addRoute([Router::POST], '/test/sub', 'TestSubModuleClassName');
46 $routeCollector->addRoute([Router::POST], '/optional[/option]', 'OptionalModuleClassName');
47 $routeCollector->addRoute([Router::POST], '/variable/{var}', 'VariableModuleClassName');
48 $routeCollector->addRoute([Router::POST], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
50 $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
51 $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
52 $this->assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost'));
53 $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
54 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
55 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
56 $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
57 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
58 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
61 public function testGetModuleClassNotFound()
63 $this->expectException(NotFoundException::class);
65 $router = new Router(['REQUEST_METHOD' => Router::GET]);
67 $router->getModuleClass('/unsupported');
70 public function testGetModuleClassNotFoundTypo()
72 $this->expectException(NotFoundException::class);
74 $router = new Router(['REQUEST_METHOD' => Router::GET]);
76 $routeCollector = $router->getRouteCollector();
77 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
79 $router->getModuleClass('/tes');
82 public function testGetModuleClassNotFoundOptional()
84 $this->expectException(NotFoundException::class);
86 $router = new Router(['REQUEST_METHOD' => Router::GET]);
88 $routeCollector = $router->getRouteCollector();
89 $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
91 $router->getModuleClass('/optional/opt');
94 public function testGetModuleClassNotFoundVariable()
96 $this->expectException(NotFoundException::class);
98 $router = new Router(['REQUEST_METHOD' => Router::GET]);
100 $routeCollector = $router->getRouteCollector();
101 $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
103 $router->getModuleClass('/variable');
106 public function testGetModuleClassMethodNotAllowed()
108 $this->expectException(MethodNotAllowedException::class);
110 $router = new Router(['REQUEST_METHOD' => Router::POST]);
112 $routeCollector = $router->getRouteCollector();
113 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
115 $router->getModuleClass('/test');
118 public function testPostModuleClassMethodNotAllowed()
120 $this->expectException(MethodNotAllowedException::class);
122 $router = new Router(['REQUEST_METHOD' => Router::GET]);
124 $routeCollector = $router->getRouteCollector();
125 $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
127 $router->getModuleClass('/test');
130 public function dataRoutes()
135 '/' => [Module\Home::class, [Router::GET]],
137 '/route' => [Module\Friendica::class, [Router::GET]],
143 '/route' => [Module\Xrd::class, [Router::GET]],
147 '/it' => [Module\NodeInfo::class, [Router::POST]],
149 '/double' => [Module\Profile::class, [Router::GET, Router::POST]]
156 * @dataProvider dataRoutes
158 public function testGetRoutes(array $routes)
160 $router = (new Router([
161 'REQUEST_METHOD' => Router::GET
162 ]))->addRoutes($routes);
164 $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
165 $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
166 $this->assertEquals(Module\Xrd::class, $router->getModuleClass('/group2/group3/route'));
167 $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));
171 * @dataProvider dataRoutes
173 public function testPostRouter(array $routes)
175 $router = (new Router([
176 'REQUEST_METHOD' => Router::POST
177 ]))->addRoutes($routes);
180 $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));
181 $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));