3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\App;
24 use Friendica\App\Router;
25 use Friendica\Core\Cache\ICache;
26 use Friendica\Core\L10n;
28 use Friendica\Network\HTTPException\MethodNotAllowedException;
29 use Friendica\Network\HTTPException\NotFoundException;
30 use Mockery\MockInterface;
31 use PHPUnit\Framework\TestCase;
33 class RouterTest extends TestCase
35 /** @var L10n|MockInterface */
42 protected function setUp()
46 $this->l10n = \Mockery::mock(L10n::class);
47 $this->l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
49 $this->cache = \Mockery::mock(ICache::class);
50 $this->cache->shouldReceive('get')->andReturn(null);
51 $this->cache->shouldReceive('set')->andReturn(false);
54 public function testGetModuleClass()
56 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
58 $routeCollector = $router->getRouteCollector();
59 $routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName');
60 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
61 $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName');
62 $routeCollector->addRoute([Router::GET], '/test/sub', 'TestSubModuleClassName');
63 $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
64 $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
65 $routeCollector->addRoute([Router::GET], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
67 $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
68 $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
69 $this->assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost'));
70 $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
71 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
72 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
73 $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
74 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
75 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
78 public function testPostModuleClass()
80 $router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache);
82 $routeCollector = $router->getRouteCollector();
83 $routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName');
84 $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
85 $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName');
86 $routeCollector->addRoute([Router::POST], '/test/sub', 'TestSubModuleClassName');
87 $routeCollector->addRoute([Router::POST], '/optional[/option]', 'OptionalModuleClassName');
88 $routeCollector->addRoute([Router::POST], '/variable/{var}', 'VariableModuleClassName');
89 $routeCollector->addRoute([Router::POST], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName');
91 $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/'));
92 $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test'));
93 $this->assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost'));
94 $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub'));
95 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional'));
96 $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option'));
97 $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc'));
98 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable'));
99 $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc'));
102 public function testGetModuleClassNotFound()
104 $this->expectException(NotFoundException::class);
106 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
108 $router->getModuleClass('/unsupported');
111 public function testGetModuleClassNotFoundTypo()
113 $this->expectException(NotFoundException::class);
115 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
117 $routeCollector = $router->getRouteCollector();
118 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
120 $router->getModuleClass('/tes');
123 public function testGetModuleClassNotFoundOptional()
125 $this->expectException(NotFoundException::class);
127 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
129 $routeCollector = $router->getRouteCollector();
130 $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
132 $router->getModuleClass('/optional/opt');
135 public function testGetModuleClassNotFoundVariable()
137 $this->expectException(NotFoundException::class);
139 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
141 $routeCollector = $router->getRouteCollector();
142 $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
144 $router->getModuleClass('/variable');
147 public function testGetModuleClassMethodNotAllowed()
149 $this->expectException(MethodNotAllowedException::class);
151 $router = new Router(['REQUEST_METHOD' => Router::POST], '', $this->l10n, $this->cache);
153 $routeCollector = $router->getRouteCollector();
154 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
156 $router->getModuleClass('/test');
159 public function testPostModuleClassMethodNotAllowed()
161 $this->expectException(MethodNotAllowedException::class);
163 $router = new Router(['REQUEST_METHOD' => Router::GET], '', $this->l10n, $this->cache);
165 $routeCollector = $router->getRouteCollector();
166 $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
168 $router->getModuleClass('/test');
171 public function dataRoutes()
176 '/' => [Module\Home::class, [Router::GET]],
178 '/route' => [Module\Friendica::class, [Router::GET]],
184 '/route' => [Module\Xrd::class, [Router::GET]],
188 '/it' => [Module\NodeInfo::class, [Router::POST]],
190 '/double' => [Module\Profile\Index::class, [Router::GET, Router::POST]]
197 * @dataProvider dataRoutes
199 public function testGetRoutes(array $routes)
201 $router = (new Router(
202 ['REQUEST_METHOD' => Router::GET],
206 ))->loadRoutes($routes);
208 $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
209 $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
210 $this->assertEquals(Module\Xrd::class, $router->getModuleClass('/group2/group3/route'));
211 $this->assertEquals(Module\Profile\Index::class, $router->getModuleClass('/double'));
215 * @dataProvider dataRoutes
217 public function testPostRouter(array $routes)
219 $router = (new Router([
220 'REQUEST_METHOD' => Router::POST
221 ], '', $this->l10n, $this->cache))->loadRoutes($routes);
224 $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));
225 $this->assertEquals(Module\Profile\Index::class, $router->getModuleClass('/double'));