public function testInvoke() { $var = 'bar'; Debug::dump($var); $system = System::init([], true); $view = $this->getMock(View::CLASS, ['render']); $services = new Services(); $services->set('System', $system); $services->set('View', $view); $this->setServices($services); $html = '<body></body>'; $stream = Stream::make($html); $response = new Response(200, $stream, ['Content-Type' => ['text/html']]); $event = new SystemEvent(); $event->setResult(SystemEvent::FINISH, $response); $model = new ViewModel(); $listener = new InjectDumpListener(); $listener->setModel($model); $view->expects($this->once())->method('render')->with($this->identicalTo($model))->will($this->returnValue('foo')); $listener($event); $this->assertTrue(isset($model['dumps'])); $this->assertSame($model['dumps'], Debug::getDumpInstances()); $injected = $event->getResult(SystemEvent::FINISH); $this->assertInstanceOf(Response::CLASS, $injected); $body = $injected->getBody(); $this->assertSame('<body>foo</body>', (string) $body); }
/** * Renders the view. * * @param \Es\System\SystemEvent $event The system event */ public function __invoke(SystemEvent $event) { $view = $this->getView(); $layout = $view->getLayout(); $result = $view->render($layout); $contentType = $layout->getContentType(); $server = $this->getServer(); $response = $server->getResponse()->withHeader('Content-Type', $contentType)->withBody(Stream::make($result)); $event->setResult(SystemEvent::FINISH, $response); }
/** * Handles a unexpected error in production mode. * * @param \Es\System\SystemEvent $systemEvent The system event * @param \Exception|\Error $exception The exception or the error */ public function handleProductionError(SystemEvent $systemEvent, $exception) { $details = ['details' => 'The resource is temporary unavailable']; $result = json_encode($details); $body = Stream::make($result); $status = 503; if ($exception instanceof ExceptionInterface && $exception->getCode()) { $status = $exception->getCode(); } $response = new Response($status, $body, ['Content-Type' => 'application/problem+json']); $this->processResponse($response, $systemEvent); }
/** * Removes from the response of type "text/html" the blank lines. * * @param \Es\System\SystemEvent $event The system event */ public function __invoke(SystemEvent $event) { $result = $event->getResult(SystemEvent::FINISH); if ($result instanceof ResponseInterface) { $contentType = $result->getHeaderLine('Content-Type'); if (0 === strpos($contentType, 'text/html')) { $body = (string) $result->getBody(); $cleaned = preg_replace("#(\\s)*(\n)+(\r)*#", "\n", $body); $stream = Stream::make($cleaned); $event->setResult(SystemEvent::FINISH, $result->withBody($stream)); } } }
public function testInvokeWithTextHtmlContent() { $source = "Lorem ipsum\n\n dolor sit amet,\n\n\n consectetur adipiscing elit"; $expected = "Lorem ipsum\n dolor sit amet,\n consectetur adipiscing elit"; $body = Stream::make($source); $headers = ['Content-type' => ['text/html']]; $response = new Response(200, $body, $headers); $event = new SystemEvent(); $event->setResult(SystemEvent::FINISH, $response); $listener = new ClearOutputListener(); $listener($event); $result = $event->getResult(SystemEvent::FINISH); $this->assertInstanceOf(ResponseInterface::CLASS, $result); $this->assertSame($expected, (string) $result->getBody()); }
/** * Injects the debug toolbar to the body of the response. * * @param \Es\Debug\ToolbarEvent $toolbarEvent */ public function __invoke(ToolbarEvent $toolbarEvent) { $system = $this->getSystem(); $event = $system->getEvent(); $result = $event->getResult(SystemEvent::FINISH); if ($result instanceof ResponseInterface) { $contentType = $result->getHeaderLine('Content-Type'); if (0 === strpos($contentType, 'text/html')) { $view = $this->getView(); $model = $toolbarEvent->getContext(); $injection = $view->render($model); $body = (string) $result->getBody(); $html = str_replace('</body>', $injection . '</body>', $body); $stream = Stream::make($html); $event->setResult(SystemEvent::FINISH, $result->withBody($stream)); } } }
public function testCopyCopiesContentFromAbstractStream() { $content = 'Lorem ipsum dolor sit amet'; $source = Stream::make($content); $stream = new Stream(); $stream->copy($source); $this->assertSame($content, $stream->getContents()); }
public function testGetContentsReturnContents() { $stream = Stream::make('1234567890'); $resource = $stream->getResource(); fseek($resource, 5); $this->assertEquals('67890', $stream->getContents()); }
/** * Handles a unexpected error in production mode. * * @param \Es\System\SystemEvent $systemEvent The system event * @param \Exception|\Error $exception The exception or the error */ public function handleProductionError(SystemEvent $systemEvent, $exception) { $renderer = $this->getRenderer(); $result = $renderer->render('error/production'); $body = Stream::make($result); $status = 503; if ($exception instanceof ExceptionInterface && $exception->getCode()) { $status = $exception->getCode(); } $response = new Response($status, $body, ['Content-Type' => 'text/html']); $this->processResponse($response, $systemEvent); }