router = new Router(); $this->config = Factory::getInstance()->config; // Append required routes Factory::getInstance()->controllers->addComponentPath('test' . DS . 'controllers'); Factory::getInstance()->views->addComponentPath('test' . DS . 'views'); } /** * @coversNothing */ public function testGetRouterClass() { $this->assertInstanceOf('FuzeWorks\Router', $this->router); } /* Route Parsing ------------------------------------------------------ */ /** * @depends testGetRouterClass * @covers ::addRoute * @covers ::getRoutes */ public function testAddRoutes() { $routeConfig = function () { }; $this->router->addRoute('testRoute', $routeConfig); $this->assertArrayHasKey('testRoute', $this->router->getRoutes()); $this->assertEquals($routeConfig, $this->router->getRoutes()['testRoute']); } /** * @depends testAddRoutes * @covers ::addRoute * @covers ::getRoutes */ public function testAddBlankRoute() { $this->router->addRoute('testBlankRoute'); $this->assertArrayHasKey('testBlankRoute', $this->router->getRoutes()); $this->assertEquals(['callable' => [$this->router, 'defaultCallable']], $this->router->getRoutes()['testBlankRoute']); } /** * @depends testAddRoutes * @covers ::addRoute * @covers ::getRoutes */ public function testAppendRoutes() { $testRouteFunction = [function () { }]; $testAppendRouteFunction = [function () { }]; $this->router->addRoute('testRoute', $testRouteFunction); $this->router->addRoute('testAppendRoute', $testAppendRouteFunction, false); // Test if the order is correct $this->assertSame( ['testRoute' => $testRouteFunction, 'testAppendRoute' => $testAppendRouteFunction], $this->router->getRoutes() ); // Test if the order is not incorrect $this->assertNotSame( ['testAppendRoute' => $testAppendRouteFunction, 'testRoute' => $testRouteFunction], $this->router->getRoutes() ); } /** * @depends testAddRoutes * @covers ::addRoute * @covers ::getRoutes * @covers ::removeRoute */ public function testRemoveRoutes() { // First add routes $this->router->addRoute('testRemoveRoute', function () { }); $this->assertArrayHasKey('testRemoveRoute', $this->router->getRoutes()); // Then remove $this->router->removeRoute('testRemoveRoute'); $this->assertArrayNotHasKey('testRemoveRoute', $this->router->getRoutes()); } /** * @depends testAddRoutes * @covers ::init * @covers ::addRoute */ public function testParseRouting() { // Prepare the routes so they can be parsed $this->config->routes->set('testParseRouting', function () { }); $this->router->init(); // Now verify whether the passing has been processed correctly $this->assertArrayHasKey('testParseRouting', $this->router->getRoutes()); } /** * @depends testParseRouting * @covers ::init */ public function testWildcardParsing() { // Prepare the routes so they can be parsed $this->config->routes->set('testWildcardParsing/:any/:num', function () { }); $this->router->init(); // Now verify whether the route has been skipped $this->assertArrayHasKey('testWildcardParsing/[^/]+/[0-9]+', $this->router->getRoutes()); } /** * @depends testParseRouting * @covers ::init */ public function testBlankRouteParsing() { // Prepare the routes so they can be parsed $this->config->routes->set(0, 'testBlankRouteParsing'); $this->router->init(); // Now verify whether the route has been parsed $this->assertArrayHasKey('testBlankRouteParsing', $this->router->getRoutes()); } /* defaultCallable() -------------------------------------------------- */ /** * @depends testGetRouterClass * @covers ::defaultCallable */ public function testDefaultCallable() { $matches = [ 'viewName' => 'TestDefaultCallable', 'viewType' => 'test', 'viewMethod' => 'someMethod' ]; $this->assertNull($this->router->getCurrentController()); $this->assertNull($this->router->getCurrentView()); $this->assertEquals('Verify Output', $this->router->defaultCallable($matches, '.*$')); $this->assertInstanceOf('\Application\Controller\TestDefaultCallableController', $this->router->getCurrentController()); $this->assertInstanceOf('\Application\View\TestDefaultCallableTestView', $this->router->getCurrentView()); } /** * @depends testDefaultCallable * @covers ::defaultCallable */ public function testDefaultCallableMissingMethod() { $matches = [ 'viewName' => 'TestDefaultCallable', 'viewType' => 'test', 'viewMethod' => 'missing' ]; $this->assertNull($this->router->getCurrentController()); $this->assertNull($this->router->getCurrentView()); $this->assertFalse($this->router->defaultCallable($matches, '.*$')); $this->assertInstanceOf('\Application\Controller\TestDefaultCallableController', $this->router->getCurrentController()); $this->assertInstanceOf('\Application\View\TestDefaultCallableTestView', $this->router->getCurrentView()); } /** * @depends testDefaultCallable * @covers ::defaultCallable */ public function testDefaultCallableMissingView() { $matches = [ 'viewName' => 'TestDefaultCallableMissingView', 'viewType' => 'test', 'viewMethod' => 'missing' ]; $this->assertNull($this->router->getCurrentController()); $this->assertNull($this->router->getCurrentView()); $this->assertFalse($this->router->defaultCallable($matches, '.*$')); $this->assertInstanceOf('\Application\Controller\TestDefaultCallableMissingViewController', $this->router->getCurrentController()); $this->assertNull($this->router->getCurrentView()); } /** * @depends testDefaultCallable * @covers ::defaultCallable */ public function testDefaultCallableMissingController() { $matches = [ 'viewName' => 'TestDefaultCallableMissingController', 'viewType' => 'test', 'viewMethod' => 'missing' ]; $this->assertNull($this->router->getCurrentController()); $this->assertNull($this->router->getCurrentView()); $this->assertFalse($this->router->defaultCallable($matches, '.*$')); $this->assertNull($this->router->getCurrentController()); $this->assertNull($this->router->getCurrentView()); } /** * @depends testDefaultCallable * @covers ::defaultCallable * @expectedException \FuzeWorks\Exception\HaltException */ public function testDefaultCallableHalt() { $matches = [ 'viewName' => 'TestDefaultCallableHalt', 'viewType' => 'test', 'viewMethod' => 'someMethod' ]; $this->assertNull($this->router->getCurrentController()); $this->assertNull($this->router->getCurrentView()); $this->router->defaultCallable($matches, '.*$'); $this->assertInstanceOf('\Application\Controller\TestDefaultCallableHaltController', $this->router->getCurrentController()); $this->assertInstanceOf('\Application\View\TestDefaultCallableHaltTestView', $this->router->getCurrentView()); } /** * @depends testDefaultCallable * @covers ::defaultCallable */ public function testDefaultCallableEmptyName() { $matches = [ 'viewType' => 'test', 'viewMethod' => 'someMethod' ]; $this->assertNull($this->router->getCurrentController()); $this->assertNull($this->router->getCurrentView()); $this->assertFalse($this->router->defaultCallable($matches, '.*$')); $this->assertNull($this->router->getCurrentController()); $this->assertNull($this->router->getCurrentView()); } /* route() ------------------------------------------------------------ */ }