From 8748bfd8ad45a0c7c5846364629886678a094c2f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 13 Oct 2019 07:54:50 -0400 Subject: [PATCH] Fix Router tests related to unallowed methods --- tests/src/App/RouterTest.php | 87 +++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 21 deletions(-) diff --git a/tests/src/App/RouterTest.php b/tests/src/App/RouterTest.php index 2fcc02ff51..37ba50bd23 100644 --- a/tests/src/App/RouterTest.php +++ b/tests/src/App/RouterTest.php @@ -12,19 +12,44 @@ class RouterTest extends TestCase { public function testGetModuleClass() { - $router = new Router(['REQUEST_METHOD' => 'GET']); + $router = new Router(['REQUEST_METHOD' => Router::GET]); $routeCollector = $router->getRouteCollector(); - $routeCollector->addRoute(['GET'], '/', 'IndexModuleClassName'); - $routeCollector->addRoute(['GET'], '/test', 'TestModuleClassName'); - $routeCollector->addRoute(['GET'], '/test/sub', 'TestSubModuleClassName'); - $routeCollector->addRoute(['GET'], '/optional[/option]', 'OptionalModuleClassName'); - $routeCollector->addRoute(['GET'], '/variable/{var}', 'VariableModuleClassName'); - $routeCollector->addRoute(['GET'], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName'); - $routeCollector->addRoute(['POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'], '/unsupported', 'UnsupportedMethodModuleClassName'); + $routeCollector->addRoute([Router::GET], '/', 'IndexModuleClassName'); + $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); + $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName'); + $routeCollector->addRoute([Router::GET], '/test/sub', 'TestSubModuleClassName'); + $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName'); + $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName'); + $routeCollector->addRoute([Router::GET], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName'); $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/')); $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test')); + $this->assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost')); + $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub')); + $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional')); + $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option')); + $this->assertEquals('VariableModuleClassName', $router->getModuleClass('/variable/123abc')); + $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable')); + $this->assertEquals('OptionalVariableModuleClassName', $router->getModuleClass('/optionalvariable/123abc')); + } + + public function testPostModuleClass() + { + $router = new Router(['REQUEST_METHOD' => Router::POST]); + + $routeCollector = $router->getRouteCollector(); + $routeCollector->addRoute([Router::POST], '/', 'IndexModuleClassName'); + $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName'); + $routeCollector->addRoute([Router::GET, Router::POST], '/testgetpost', 'TestGetPostModuleClassName'); + $routeCollector->addRoute([Router::POST], '/test/sub', 'TestSubModuleClassName'); + $routeCollector->addRoute([Router::POST], '/optional[/option]', 'OptionalModuleClassName'); + $routeCollector->addRoute([Router::POST], '/variable/{var}', 'VariableModuleClassName'); + $routeCollector->addRoute([Router::POST], '/optionalvariable[/{option}]', 'OptionalVariableModuleClassName'); + + $this->assertEquals('IndexModuleClassName', $router->getModuleClass('/')); + $this->assertEquals('TestModuleClassName', $router->getModuleClass('/test')); + $this->assertEquals('TestGetPostModuleClassName', $router->getModuleClass('/testgetpost')); $this->assertEquals('TestSubModuleClassName', $router->getModuleClass('/test/sub')); $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional')); $this->assertEquals('OptionalModuleClassName', $router->getModuleClass('/optional/option')); @@ -37,7 +62,7 @@ class RouterTest extends TestCase { $this->expectException(NotFoundException::class); - $router = new Router(['REQUEST_METHOD' => 'GET']); + $router = new Router(['REQUEST_METHOD' => Router::GET]); $router->getModuleClass('/unsupported'); } @@ -46,10 +71,10 @@ class RouterTest extends TestCase { $this->expectException(NotFoundException::class); - $router = new Router(['REQUEST_METHOD' => 'GET']); + $router = new Router(['REQUEST_METHOD' => Router::GET]); $routeCollector = $router->getRouteCollector(); - $routeCollector->addRoute(['GET'], '/test', 'TestModuleClassName'); + $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); $router->getModuleClass('/tes'); } @@ -58,10 +83,10 @@ class RouterTest extends TestCase { $this->expectException(NotFoundException::class); - $router = new Router(['REQUEST_METHOD' => 'GET']); + $router = new Router(['REQUEST_METHOD' => Router::GET]); $routeCollector = $router->getRouteCollector(); - $routeCollector->addRoute(['GET'], '/optional[/option]', 'OptionalModuleClassName'); + $routeCollector->addRoute([Router::GET], '/optional[/option]', 'OptionalModuleClassName'); $router->getModuleClass('/optional/opt'); } @@ -70,10 +95,10 @@ class RouterTest extends TestCase { $this->expectException(NotFoundException::class); - $router = new Router(['REQUEST_METHOD' => 'GET']); + $router = new Router(['REQUEST_METHOD' => Router::GET]); $routeCollector = $router->getRouteCollector(); - $routeCollector->addRoute(['GET'], '/variable/{var}', 'VariableModuleClassName'); + $routeCollector->addRoute([Router::GET], '/variable/{var}', 'VariableModuleClassName'); $router->getModuleClass('/variable'); } @@ -82,10 +107,34 @@ class RouterTest extends TestCase { $this->expectException(MethodNotAllowedException::class); - $router = new Router(['REQUEST_METHOD' => 'POST']); + $router = new Router(['REQUEST_METHOD' => Router::POST]); $routeCollector = $router->getRouteCollector(); - $routeCollector->addRoute(['GET'], '/test', 'TestModuleClassName'); + $routeCollector->addRoute([Router::GET], '/test', 'TestModuleClassName'); + + $router->getModuleClass('/test'); + } + + public function testPostModuleClassMethodNotAllowed() + { + $this->expectException(MethodNotAllowedException::class); + + $router = new Router(['REQUEST_METHOD' => Router::GET]); + + $routeCollector = $router->getRouteCollector(); + $routeCollector->addRoute([Router::POST], '/test', 'TestModuleClassName'); + + $router->getModuleClass('/test'); + } + + public function testGetPostModuleClassMethodNotAllowed() + { + $this->expectException(MethodNotAllowedException::class); + + $router = new Router(['REQUEST_METHOD' => 'PUT']); + + $routeCollector = $router->getRouteCollector(); + $routeCollector->addRoute([Router::GET, Router::POST], '/test', 'TestModuleClassName'); $router->getModuleClass('/test'); } @@ -127,7 +176,6 @@ class RouterTest extends TestCase $this->assertEquals(Module\Home::class, $router->getModuleClass('/')); $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route')); $this->assertEquals(Module\Xrd::class, $router->getModuleClass('/group2/group3/route')); - $this->assertNull($router->getModuleClass('/post/it')); $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double')); } @@ -141,9 +189,6 @@ class RouterTest extends TestCase ]))->addRoutes($routes); // Don't find GET - $this->assertNull($router->getModuleClass('/')); - $this->assertNull($router->getModuleClass('/group/route')); - $this->assertNull($router->getModuleClass('/group2/group3/route')); $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it')); $this->assertEquals(Module\Profile::class, $router->getModuleClass('/double')); }