/** * @param int $statusCode * @param string $statusReasonPhrase * @param object $error * @param string $outputFormat * @return void */ public function render($statusCode, $statusReasonPhrase, $error, $outputFormat = null) { $rootPath = Config::getString('hyperframework.web.error_view.root_path', ''); if ($rootPath === '') { $rootPath = Config::getString('hyperframework.web.view.root_path', ''); if ($rootPath === '') { $rootPath = 'views' . DIRECTORY_SEPARATOR . '_error'; } else { $rootPath = FilePathCombiner::combine($rootPath, '_error'); } } $files = [ViewPathBuilder::build($statusCode, $outputFormat), ViewPathBuilder::build('default', $outputFormat)]; $rootPath = FileFullPathBuilder::build($rootPath); $path = null; foreach ($files as $file) { $file = FilePathCombiner::combine($rootPath, $file); if (file_exists($file)) { $path = $file; break; } } if ($path === null) { Response::setHeader('Content-Type: text/plain; charset=utf-8'); echo $statusCode; if ((string) $statusReasonPhrase !== '') { echo ' ', $statusReasonPhrase; } } else { $view = ViewFactory::createView(['status_code' => $statusCode, 'status_reason_pharse' => $statusReasonPhrase, 'error' => $error]); $view->render($path); } }
/** * @return void */ protected function initializeToken() { $this->setToken($this->generateToken()); $cookieDomain = Config::getString('hyperframework.web.csrf_protection.cookie_domain', ''); if ($cookieDomain === '') { $cookieDomain = null; } Response::setCookie($this->getTokenName(), $this->getToken(), ['domain' => $cookieDomain]); }
public function testRenderDefaultView() { Config::set('hyperframework.web.error_view.root_path', 'invalid'); $this->expectOutputString('404 not found'); $engine = $this->getMock('Hyperframework\\Web\\ResponseEngine'); $engine->expects($this->once())->method('setHeader')->with('Content-Type: text/plain; charset=utf-8'); Response::setEngine($engine); $view = new ErrorView(); $view->render(404, 'not found', null); }
/** * @expectedException Hyperframework\Web\ForbiddenException */ public function testInvalidToken() { $engine2 = $this->getMock('Hyperframework\\Web\\ResponseEngine'); $engine2->expects($this->once())->method('setCookie'); Response::setEngine($engine2); $engine = new CsrfProtectionEngine(); $_SERVER['REQUEST_METHOD'] = 'POST'; Request::setBody(['_csrf_token' => 'invalid']); $_COOKIE['_csrf_token'] = 'token'; $engine->run(); }
public function renderView() { if ($this->isViewEnabled() === false) { return; } Response::setHeader('Content-Type: application/json'); $json = json_encode($this->getActionResult(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); if ($json === false) { throw new UnexpectedValueException('The action result is invalid.'); } echo $json; }
private function mockEngineMethod($method) { $engine = $this->getMock('Hyperframework\\Web\\ResponseEngine'); Response::setEngine($engine); return $engine->expects($this->once())->method($method); }
/** * @return void */ private function rewriteHttpHeaders() { Response::removeHeaders(); $error = $this->getError(); if ($error instanceof HttpException) { foreach ($error->getHttpHeaders() as $header) { Response::setHeader($header); } } else { Response::setHeader('HTTP/1.1 500 Internal Server Error'); } }
public function testRedirect() { $engine = $this->getMock('Hyperframework\\Web\\ResponseEngine'); $engine->expects($this->once())->method('setHeader')->with('Location: /', true, 302); $app = $this->getMockBuilder('Hyperframework\\Web\\App')->setConstructorArgs([dirname(__DIR__)])->getMock(); $app->expects($this->once())->method('quit'); Response::setEngine($engine); $this->router = $this->getMockBuilder('Hyperframework\\Web\\Test\\Router')->setMethods(['getApp'])->setConstructorArgs([$app])->disableOriginalConstructor()->getMock(); $this->router->expects($this->once())->method('getApp')->willReturn($app); $this->callProtectedMethod($this->router, 'redirect', ['/']); }
public function testRedirect() { $app = new App(dirname(__DIR__)); $router = $app->getRouter(); $router->setAction('index'); $controller = $this->getMockBuilder('Hyperframework\\Web\\Test\\IndexController')->setConstructorArgs([$app])->setMethods(['handleAction', 'quit'])->getMock(); $controller->expects($this->once())->method('quit'); $engine = $this->getMock('Hyperframework\\Web\\ResponseEngine'); $engine->expects($this->once())->method('setHeader')->with('Location: /', true, 302); Response::setEngine($engine); $controller->redirect('/'); }
public function testRewriteHttpHeadersForHttpException() { $engine = $this->getMock('Hyperframework\\Web\\ResponseEngine'); $engine->expects($this->once())->method('removeHeaders'); $engine->expects($this->once())->method('setHeader')->with('HTTP/1.1 404 Not Found'); $engine->method('headersSent')->willReturn(false); Response::setEngine($engine); $handler = $this->getMockBuilder('Hyperframework\\Web\\ErrorHandler')->setMethods(['renderErrorView', 'getError'])->getMock(); $handler->method('getError')->willReturn(new NotFoundException()); $this->callProtectedMethod($handler, 'handle'); }
/** * @param string $url * @param int $statusCode * @return void */ public function redirect($url, $statusCode = 302) { Response::setHeader('Location: ' . $url, true, $statusCode); $this->quit(); }
/** * @param string $url * @param int $statusCode * @return void */ protected function redirect($url, $statusCode = 302) { Response::setHeader('Location: ' . $url, true, $statusCode); $this->getApp()->quit(); }