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); }
/** * Creates the view model. * * @param SystemEvent $event The system event */ public function __invoke(SystemEvent $event) { $result = $event->getResult(SystemEvent::DISPATCH); if (false !== $result && !$result instanceof ViewModelInterface && !$result instanceof ResponseInterface) { $model = new ViewModel($result); $event->setResult(SystemEvent::DISPATCH, $model); } }
/** * @dataProvider invalidResponseDataProvider */ public function testInvokeRaiseExceptionIfEventResultIsNotPsrResponse($response) { $event = new SystemEvent(); $event->setResult(SystemEvent::FINISH, $response); $listener = new ResponseListener(); $this->setExpectedException('UnexpectedValueException'); $listener($event); }
/** * 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 testInvoke() { $result = ['foo' => 'bar', 'bat' => 'baz']; $event = new SystemEvent(); $event->setResult(SystemEvent::DISPATCH, $result); $listener = new CreateModelListener(); $listener($event); $model = $event->getResult(SystemEvent::DISPATCH); $this->assertInstanceOf(ViewModel::CLASS, $model); $this->assertSame($result, $model->getVariables()); }
/** * Processes the response. * * @param \Psr\Http\Message\ResponseInterface $response The response * @param \Es\System\SystemEvent $event The system event */ public function processResponse(ResponseInterface $response, SystemEvent $event) { if ($event->getName() === SystemEvent::FINISH) { $event->stopPropagation(true); $server = $this->getServer(); $emitter = $server->getEmitter(); $emitter->emit($response); return; } $events = $this->getEvents(); $event->setResult(SystemEvent::FINISH, $response); $events->trigger($event(SystemEvent::FINISH)); }
/** * 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 testInvokeOnSuccess() { $view = new View(); $listener = new InjectModelListener(); $listener->setView($view); $event = new SystemEvent(); $model = new ViewModel(); $event->setResult(SystemEvent::DISPATCH, $model); $listener($event); $layout = $view->getLayout(); $iterator = $layout->getIterator(); $array = iterator_to_array($iterator); $this->assertContains($model, $array); }
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()); }
public function testInvokeOnSuccess() { $server = new Server(); $request = $server->getRequest(); $server->setRequest($request->withAttribute('action', 'foo')); $model = new ViewModel(); $model->setModule(__NAMESPACE__); $event = new SystemEvent(); $event->setResult(SystemEvent::DISPATCH, $model); $event->setContext(new FakeController()); $listener = new InjectTemplateListener(); $listener->setServer($server); $listener($event); $this->assertSame('fake/foo', $model->getTemplate()); }
/** * Creates the report about debugging dump. * * @param \Es\System\SystemEvent $event The system event */ public function __invoke(SystemEvent $event) { $system = $this->getSystem(); if ($system->isDevMode()) { $result = $event->getResult(SystemEvent::FINISH); if ($result instanceof ResponseInterface) { $contentType = $result->getHeaderLine('Content-Type'); if (0 === strpos($contentType, 'text/html')) { $model = $this->getModel(); $view = $this->getView(); $model['dumps'] = Debug::getDumpInstances(); $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)); } } } }
/** * Triggers the DispatchEvent. * * @param \Es\System\SystemEvent $event The system event * * @throws \RuntimeException If the ServerRequest not contain the * "controller" attribute */ public function onDispatch(SystemEvent $event) { $server = $this->getServer(); $request = $server->getRequest(); $controllerName = $request->getAttribute('controller'); if (!$controllerName) { throw new RuntimeException('Unable to dispatch the system event, the server request not ' . 'contains the "controller" attribute.'); } $actionName = $request->getAttribute('action', 'index'); $events = $this->getEvents(); $controllers = $this->getControllers(); $controller = $controllers->get($controllerName); $event->setContext($controller); $dispatchEvent = new DispatchEvent($controller, $controllerName, $actionName, $event->getParams()); $events->trigger($dispatchEvent); $result = $dispatchEvent->getResult(); $target = SystemEvent::DISPATCH; if ($result instanceof ResponseInterface) { $target = SystemEvent::FINISH; } $event->setResult($target, $result); }
public function testInvokeOnSuccess() { $modules = new Modules(); $module = $this->getMock(AbstractModule::CLASS); $modules->set(__NAMESPACE__, $module); $view = new View(); $listener = new InjectModuleListener(); $listener->setModules($modules); $listener->setView($view); $event = new SystemEvent(); $model = new ViewModel(); $controller = new FakeController(); $event->setContext($controller); $event->setResult(SystemEvent::DISPATCH, $model); $listener($event); $layout = $view->getLayout(); $this->assertSame(__NAMESPACE__, $layout->getModule()); $this->assertSame(__NAMESPACE__, $model->getModule()); }
public function __invoke(SystemEvent $event) { $event->setResult(SystemEvent::INIT, true); }