public function testSetRouter() { $this->provider->register('router', function () { $router = new Router(); $router->setRoute('my', ['GET'], '/my', []); return $router; }); $this->assertEquals('/my', $this->provider->getRouter()->createPath('my')); $newRouter = new Router(); $newRouter->setRoute('me', ['GET'], '/me', []); $this->provider->setRouter($newRouter); $this->assertEquals('/me', $this->provider->getRouter()->createPath('me')); }
public function setUp() { $_SERVER = array('SERVER_NAME' => 'localhost', 'SCRIPT_NAME' => 'index.php'); $this->provider = new ServiceProvider(); $router = new Router(); $router->setRoutes(['my' => ['methods' => ['GET'], 'route' => '/my/{id}', 'handler' => [], 'filters' => ['id' => '([\\d-]+)'], 'child_routes' => ['my-sub' => ['methods' => ['GET', 'POST'], 'route' => '/sub/{p}', 'handler' => [], 'filters' => ['p' => '(\\d+)']]]]]); $this->provider->setRouter($router); $tm = new TemplatesManager(); $layout = __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'SomeModule' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'main.phtml'; $layout2 = __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'SomeModule' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'main2.phtml'; $tm->setTemplateMap(['main' => $layout, 'main2' => $layout2]); $tm->addTemplatePath('foo', __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'SomeModule' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'foo'); $this->provider->setTemplatesManager($tm); $this->provider->setConfig(['layout' => 'main']); $file = __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'SomeModule' . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR . 'FooController.php'; require_once $file; $class = 'Foo\\FooController'; $this->controller = new $class($this->provider); }
public function testMatchWithBasePathAndMiddlewares() { $router = new Router(); $router->setBasePath('my-site'); $router->setRoutes(['my' => ['methods' => ['GET'], 'route' => '/my/{id}', 'handler' => [], 'filters' => ['id' => '([\\d-]+)'], 'middlewares' => ['midd1', 'midd2'], 'child_routes' => ['my-sub' => ['methods' => ['GET', 'POST'], 'route' => '/sub/{p}', 'handler' => [], 'filters' => ['p' => '(\\d+)'], 'middlewares' => ['midd2.1', 'midd2.2']]]]]); $router->addMiddleware('my', ['midd3.1', 'midd3.2']); $router->addMiddleware('my-sub', ['midd4.1', 'midd4.2']); $result = $router->match('GET', '/my-site/my/10'); $this->assertTrue($result->isFound()); $this->assertEquals('my', $result->getName()); $this->assertEquals([], $result->getHandler()); $this->assertEquals(['id' => 10], $result->getParams()); $this->assertEquals(['midd1', 'midd2', 'midd3.1', 'midd3.2'], $result->getMiddlewares()); $result = $router->match('GET', '/my-site/my/10/sub/22'); $this->assertTrue($result->isFound()); $this->assertEquals('my-sub', $result->getName()); $this->assertEquals([], $result->getHandler()); $this->assertEquals(['id' => 10, 'p' => 22], $result->getParams()); $this->assertEquals(['midd1', 'midd2', 'midd2.1', 'midd2.2', 'midd3.1', 'midd3.2', 'midd4.1', 'midd4.2'], $result->getMiddlewares()); }