public function testFiltersWithContext() { $filters = new Filters(); $filters->register('myEventType', function (Filters $contextFilters) use($filters) { $this->assertEquals($filters, $contextFilters, 'Context should pass data to our filter function'); }); $context = new Context(); $context->registerInstance($filters); $filters->trigger('myEventType', $context); }
/** * Triggers any "not allowed" filters and prepares an appropriate 404 error response. */ private function prepareNotAllowedResponse() { ob_clean(); $this->response = new Response(); $this->response->setResponseCode(ResponseCode::HTTP_METHOD_NOT_ALLOWED); $this->context->registerInstance($this->response); $this->filters->trigger(Filters::METHOD_NOT_ALLOWED, $this->context); $this->finalizeOutputBuffer(); if (empty($this->response->getBody())) { $this->serveStaticPage('method_not_allowed'); } }
public function testControllerDispatchingWithBeforeResponseSkipsPrimaryAction() { $context = new Context(); $request = new Request(); $context->registerInstance($request); $route = new Route('/', 'Enlighten\\Tests\\Routing\\Sample\\SampleControllerWithBeforeResponse'); $this->assertInstanceOf('Enlighten\\Http\\Response', $route->action($context)); $this->expectOutputString(''); // This test SHOULD result in the route's primary action NOT being called. // (if it is called, an exception is thrown - see the SampleControllerWithBeforeResponse class) }
/** * Executes a route action, given a callable and a context. * * @param callable $targetFunc The target function to be executed. * @param Context $context The context the target function should be executed under (for dependency injection). * @return mixed The function's return value. * @throws \Exception If an exception is raised during execution, it will be rethrown. */ private function executeAction(callable $targetFunc, Context $context) { $returnValue = null; $paramValues = $context->determineParamValues($targetFunc); try { $returnValue = call_user_func_array($targetFunc, $paramValues); } catch (\Exception $ex) { $context->registerInstance($ex); $this->filters->trigger(Filters::ON_EXCEPTION, $context); if (!$this->filters->anyHandlersForEvent(Filters::ON_EXCEPTION)) { // If this exception was unhandled, rethrow it so it can be handled in the global scope throw $ex; } } return $returnValue; }
public function testCreateRedirect() { // Prepare: Prepare environment to capture response $response = new Response(); $request = new Request(); $request->setRequestUri('/redirect/bla'); $context = new Context(); $context->registerInstance($response); $context->registerInstance($request); $router = new Router(); $router->setContext($context); $route = $router->createRedirect('/redirect/$testVar', '/target/$testVar', true); $this->assertEquals('/redirect/$testVar', $route->getPattern()); $routeResult = $router->route($request); $this->assertEquals($route, $routeResult); $router->dispatch($routeResult, $request); $this->assertEquals(ResponseCode::HTTP_MOVED_PERMANENTLY, $response->getResponseCode()); $this->assertEquals('/target/bla', $response->getHeader('Location')); }
public function testGetInstances() { $context = new Context(); $this->assertEquals([$context], $context->getRegisteredInstances()); $objOne = new \stdClass(); $objTwo = new \InvalidArgumentException(); $context->registerInstance($objOne); $context->registerInstance($objTwo); $actualParams = $context->getRegisteredInstances(); $this->assertEquals($actualParams[0], $context); $this->assertEquals($actualParams[1], $objOne); $this->assertEquals($actualParams[2], $objTwo); }
/** * Dispatches a Route, executing its action. * * @param Route $route The route to be executed. * @param Request $request The request information, if available. Used for mapping route variables. * @return mixed Route target function return value, if any. */ public function dispatch(Route $route, Request $request = null) { $context = $this->context; if (empty($this->context) && !empty($request)) { // If we have no context, but do have a request, prepare a context to store path variables in. // Otherwise routing path variables would be lost for no good reason. $context = new Context(); $context->registerInstance($request); } if (!empty($context)) { // If we have a context, ensure that the route is made available in it. $context->registerInstance($route); if (!empty($request)) { // If we have a request, map the path variables and pass them to the context as primitive types by name. // This will allow us to inject info from a route e.g. "/view/$userId" to a $userId variable. $pathVariables = VariableUrl::extractUrlVariables($request->getRequestUri(), $route->getPattern()); foreach ($pathVariables as $name => $value) { $context->registerVariable($name, $value); } } } return $route->action($context); }