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);
 }
 /**
  * @dataProvider invalidResponseDataProvider
  */
 public function testInvokeRaiseExceptionIfEventResultIsNotPsrResponse($response)
 {
     $event = new SystemEvent();
     $event->setResult(SystemEvent::FINISH, $response);
     $listener = new ResponseListener();
     $this->setExpectedException('UnexpectedValueException');
     $listener($event);
 }
 /**
  * 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);
     }
 }
 /**
  * Injects the view model to Layout.
  *
  * @param \Es\System\SystemEvent $event The system event
  */
 public function __invoke(SystemEvent $event)
 {
     $result = $event->getResult(SystemEvent::DISPATCH);
     if ($result instanceof ViewModelInterface) {
         $view = $this->getView();
         $layout = $view->getLayout();
         $layout->addChild($result);
     }
 }
Example #5
0
 /**
  * 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);
 }
 /**
  * Sends a response to client.
  *
  * @param \Es\System\SystemEvent $event The system event
  *
  * @throws \UnexpectedValueException If the final result of a system event
  *                                   is not PSR Response
  */
 public function __invoke(SystemEvent $event)
 {
     $result = $event->getResult(SystemEvent::FINISH);
     if (!$result instanceof ResponseInterface) {
         throw new UnexpectedValueException(sprintf('The system event provided invalid final result; must be ' . 'an instance of "%s", "%s" received.', ResponseInterface::CLASS, is_object($result) ? get_class($result) : gettype($result)));
     }
     $server = $this->getServer();
     $emitter = $server->getEmitter();
     $emitter->emit($result);
 }
 public function testConstructor()
 {
     $name = 'Foo';
     $context = 'Bar';
     $params = ['baz' => 'bat'];
     $event = new SystemEvent($name, $context, $params);
     $this->assertSame($name, $event->getName());
     $this->assertSame($context, $event->getContext());
     $this->assertSame($params, $event->getParams());
 }
 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));
 }
 /**
  * Handles an error in development mode.
  *
  * @param \Es\System\SystemEvent $systemEvent The system event
  * @param \Exception|\Error      $exception   The exception or the error
  */
 public function handleDevelopmentError(SystemEvent $systemEvent, $exception)
 {
     $options = ['exception' => $exception, 'eventName' => $systemEvent->getName()];
     $renderer = $this->getRenderer();
     $result = $renderer->render('error/development', $options);
     $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);
 }
 /**
  * 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());
 }
 /**
  * Injects the template name to a view model.
  *
  * @param \Es\System\SystemEvent $event The system event
  *
  * @throws \RuntimeException         If the module namespace is not specified
  * @throws \UnexpectedValueException If the context of system event is
  *                                   not object
  */
 public function __invoke(SystemEvent $event)
 {
     $result = $event->getResult(SystemEvent::DISPATCH);
     if ($result instanceof ViewModelInterface && !$result->getTemplate()) {
         $module = $result->getModule();
         if (!$module) {
             throw new RuntimeException('Unable to resolve template for View Model. ' . 'The module namespace is not specified.');
         }
         $context = $event->getContext();
         if (!is_object($context)) {
             throw new UnexpectedValueException(sprintf('Invalid context of system event; must be an object, ' . '"%s" received.', gettype($context)));
         }
         $template = $this->resolveTemplate($context, $module);
         $result->setTemplate($template);
     }
 }
 /**
  * Injects the module namespace of the dispatched controller to a view model
  * and to layout.
  *
  * @param \Es\System\SystemEvent $event The system event
  *
  * @throws \UnexpectedValueException If the context of system event is
  *                                   not object
  */
 public function __invoke(SystemEvent $event)
 {
     $context = $event->getContext();
     if (!is_object($context)) {
         throw new UnexpectedValueException(sprintf('Invalid context of system event; must be an object, ' . '"%s" received.', gettype($context)));
     }
     $module = $this->resolveModule($context);
     $view = $this->getView();
     $layout = $view->getLayout();
     if (!$layout->getModule()) {
         $layout->setModule($module);
     }
     $result = $event->getResult(SystemEvent::DISPATCH);
     if ($result instanceof ViewModelInterface && !$result->getModule()) {
         $result->setModule($module);
     }
 }
 public function testHandleDevelopmentErrorWithSpecifiedStatus()
 {
     $renderer = $this->getMock(ErrorRenderer::CLASS);
     $events = $this->getMock(Events::CLASS);
     $strategy = new HtmlErrorStrategy();
     $strategy->setRenderer($renderer);
     $strategy->setEvents($events);
     $event = new SystemEvent('Foo');
     $exception = new FakeException('Foo', 405);
     $content = 'Lorem ipsum dolor sit amet';
     $renderer->expects($this->once())->method('render')->with($this->identicalTo('error/development'), $this->identicalTo(['exception' => $exception, 'eventName' => $event->getName()]))->will($this->returnValue($content));
     $events->expects($this->once())->method('trigger')->with($this->identicalTo($event));
     $strategy->handleDevelopmentError($event, $exception);
     $response = $event->getResult(SystemEvent::FINISH);
     $this->assertInstanceOf(ResponseInterface::CLASS, $response);
     $this->assertSame($content, (string) $response->getBody());
     $this->assertSame('text/html', $response->getHeaderLine('Content-Type'));
     $this->assertSame(405, $response->getStatusCode());
 }
 /**
  * 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());
 }
Example #21
0
 public function __invoke(SystemEvent $event)
 {
     $event->setResult(SystemEvent::INIT, true);
 }