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 testGetPostModuleClassMethodNotAllowed()
132 $this->expectException(MethodNotAllowedException::class);
134 $router = new Router(['REQUEST_METHOD' => 'PUT']);
136 $routeCollector = $router->getRouteCollector();
137 $routeCollector->addRoute([Router::GET, Router::POST], '/test', 'TestModuleClassName');
139 $router->getModuleClass('/test');
142 public function dataRoutes()
147 '/' => [Module\Home::class, [Router::GET]],
149 '/route' => [Module\Friendica::class, [Router::GET]],
155 '/route' => [Module\Xrd::class, [Router::GET]],
159 '/it' => [Module\NodeInfo::class, [Router::POST]],
161 '/double' => [Module\Profile::class, [Router::GET, Router::POST]]
168 * @dataProvider dataRoutes
170 public function testGetRoutes(array $routes)
172 $router = (new Router([
173 'REQUEST_METHOD' => Router::GET
174 ]))->addRoutes($routes);
176 $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
177 $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
178 $this->assertEquals(Module\Xrd::class, $router->getModuleClass('/group2/group3/route'));
179 $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));
183 * @dataProvider dataRoutes
185 public function testPostRouter(array $routes)
187 $router = (new Router([
188 'REQUEST_METHOD' => Router::POST
189 ]))->addRoutes($routes);
192 $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));
193 $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));