/** * Base on router config, init Di for router * @return \Phalex\Di\DiManager * @throws Exception\RuntimeException */ public function initRouterDi() { $config = $this->getConfig(); if (!isset($config['router'])) { throw new Exception\RuntimeException('Cannot init DI for router. Cannot find router configuration'); } $config = $config['router']->toArray(); $router = new Router($this->diFactory); foreach ($config as $name => $routeInfo) { $router->addRoute($name, $routeInfo); } $this->diFactory->set('router', $router, true); return $this; }
/** * @group listener */ public function testBootSuccess() { $di = $this->getDi(); Route::reset(); $router = new Router($di); $router->addRoute('controller/action', ['route' => '/test-boot/:controller/:action', 'definitions' => ['controller' => 1, 'action' => 2, 'module' => 'Application', 'namespace' => 'Application\\Controller'], 'methods' => ['GET', 'POST']]); $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/test-boot/static/route'; $appMock = m::mock(PhalconApp::class); $appMock->router = $router; $appMock->shouldReceive('getDI')->andReturn($di); $eventMock = m::mock(Event::class); (new ListenApp())->boot($eventMock, $appMock); $this->assertTrue($di->has('matchedRoute')); $this->assertTrue(isset($di['matchedRoute'])); $this->assertInstanceOf(Route::class, $di['matchedRoute']); $this->assertEquals('controller/action', $di['matchedRoute']->getName()); }
/** * @group before_match */ public function testBeforeMatchRaiseInvalidClass() { require_once 'tests/module/Application/src/Router/BeforeMatchFail.php'; $excMsg = sprintf('"%s" must be implemented "%s"', BeforeMatchFail::class, BeforeMatchInterface::class); $this->setExpectedException(RuntimeException::class, $excMsg); Route::reset(); $router = new Router($this->getDi()); $router->addRoute('edit-classname', ['route' => '/edit2/([1-9][0-9]*)', 'definitions' => ['controller' => 'posts2', 'action' => 'edit2', 'id' => 1], 'before_match' => ['class_name' => BeforeMatchFail::class]]); $router->handle('/edit2/100'); }