]> git.mxchange.org Git - friendica.git/blob - tests/src/App/RouterTest.php
Merge remote-tracking branch 'upstream/develop' into aria
[friendica.git] / tests / src / App / RouterTest.php
1 <?php
2
3 namespace Friendica\Test\src\App;
4
5 use Friendica\App\Router;
6 use Friendica\Module;
7 use Friendica\Network\HTTPException\MethodNotAllowedException;
8 use Friendica\Network\HTTPException\NotFoundException;
9 use PHPUnit\Framework\TestCase;
10
11 class RouterTest extends TestCase
12 {
13         public function testGetModuleClass()
14         {
15                 $router = new Router(['REQUEST_METHOD' => Router::GET]);
16
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');
25
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'));
35         }
36
37         public function testPostModuleClass()
38         {
39                 $router = new Router(['REQUEST_METHOD' => Router::POST]);
40
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');
49
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'));
59         }
60
61         public function testGetModuleClassNotFound()
62         {
63                 $this->expectException(NotFoundException::class);
64
65                 $router = new Router(['REQUEST_METHOD' => Router::GET]);
66
67                 $router->getModuleClass('/unsupported');
68         }
69
70         public function testGetModuleClassNotFoundTypo()
71         {
72                 $this->expectException(NotFoundException::class);
73
74                 $router = new Router(['REQUEST_METHOD' => Router::GET]);
75
76                 $routeCollector = $router->getRouteCollector();
77                 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
78
79                 $router->getModuleClass('/tes');
80         }
81
82         public function testGetModuleClassNotFoundOptional()
83         {
84                 $this->expectException(NotFoundException::class);
85
86                 $router = new Router(['REQUEST_METHOD' => Router::GET]);
87
88                 $routeCollector = $router->getRouteCollector();
89                 $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName');
90
91                 $router->getModuleClass('/optional/opt');
92         }
93
94         public function testGetModuleClassNotFoundVariable()
95         {
96                 $this->expectException(NotFoundException::class);
97
98                 $router = new Router(['REQUEST_METHOD' => Router::GET]);
99
100                 $routeCollector = $router->getRouteCollector();
101                 $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName');
102
103                 $router->getModuleClass('/variable');
104         }
105
106         public function testGetModuleClassMethodNotAllowed()
107         {
108                 $this->expectException(MethodNotAllowedException::class);
109
110                 $router = new Router(['REQUEST_METHOD' => Router::POST]);
111
112                 $routeCollector = $router->getRouteCollector();
113                 $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName');
114
115                 $router->getModuleClass('/test');
116         }
117         
118         public function testPostModuleClassMethodNotAllowed()
119         {
120                 $this->expectException(MethodNotAllowedException::class);
121
122                 $router = new Router(['REQUEST_METHOD' => Router::GET]);
123
124                 $routeCollector = $router->getRouteCollector();
125                 $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName');
126
127                 $router->getModuleClass('/test');
128         }
129
130         public function dataRoutes()
131         {
132                 return [
133                         'default' => [
134                                 'routes' => [
135                                         '/'       => [Module\Home::class, [Router::GET]],
136                                         '/group'  => [
137                                                 '/route' => [Module\Friendica::class, [Router::GET]],
138                                         ],
139
140
141                                         '/group2' => [
142                                                 '/group3' => [
143                                                         '/route' => [Module\Xrd::class, [Router::GET]],
144                                                 ],
145                                         ],
146                                         '/post' => [
147                                                 '/it' => [Module\NodeInfo::class, [Router::POST]],
148                                         ],
149                                         '/double' => [Module\Profile::class, [Router::GET, Router::POST]]
150                                 ],
151                         ],
152                 ];
153         }
154
155         /**
156          * @dataProvider dataRoutes
157          */
158         public function testGetRoutes(array $routes)
159         {
160                 $router = (new Router([
161                         'REQUEST_METHOD' => Router::GET
162                 ]))->addRoutes($routes);
163
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'));
168         }
169
170         /**
171          * @dataProvider dataRoutes
172          */
173         public function testPostRouter(array $routes)
174         {
175                 $router = (new Router([
176                         'REQUEST_METHOD' => Router::POST
177                 ]))->addRoutes($routes);
178
179                 // Don't find GET
180                 $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));
181                 $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double'));
182         }
183 }