/** * Makes a stream and and fills it with input data. * * @return \Es\Http\Stream The stream */ public static function make() { $stream = new Stream(); $source = static::getSource(); $stream->copy($source); if (!static::$source) { fclose($source); } return $stream; }
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); }
public function testMakeCreateInput() { $source = Stream::fopen(); fwrite($source, 'Lorem ipsum dolor sit amet'); InputFactory::setSource($source); $input = InputFactory::make(); $request = ServerRequestFactory::make(); $body = $request->getBody(); $this->assertEquals($body->getContents(), $input->getContents()); InputFactory::setSource(null); }
/** * 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 testCallThrowsExceptionWhenMethodNotExists() { $stream = new Stream(false); $this->setExpectedException('InvalidArgumentException'); $stream->stream_not_exists_method(); }
public function testGetMetadataReturnsNullWhenSpecifiedKeyNotFound() { $stream = new Stream(); $this->assertNull($stream->getMetadata('non-existent-key')); }
/** * 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); }