Beispiel #1
0
 /**
  * @covers ::setResponseCode
  */
 public function testSetResponseCodeInvalid()
 {
     $this->expectException(\InvalidArgumentException::class);
     $this->expectExceptionMessage('HTTP status \'42\' is not implemented');
     $controllerFactory = new ControllerFactory($this->pdo, $this->configuration, $this->request);
     $carsController = $controllerFactory->create(CarsController::class);
     $reflector = new \ReflectionClass($carsController);
     /**
      * Set response
      */
     $method = $reflector->getMethod('setResponseCode');
     $method->setAccessible(true);
     $method->invoke($carsController, 42);
 }
Beispiel #2
0
 /**
  * @param Request     $request Request object
  * @param Entity|null $user
  *
  * @return Response Reponse object
  *
  * @throws \Exception If unexpected exception is thrown
  */
 public function dispatch(Request $request, Entity $user = null) : Response
 {
     $controllerFactory = new ControllerFactory($this->pdo, $this->configuration, $request, $user);
     /**
      * Figure out what controller to use and what action to call
      */
     try {
         /**
          * @var Router $router
          */
         $router = $this->configuration->get('Router');
         list($controllerName, $action, $arguments) = array_values($router->route($request->getPath()));
         /**
          * Validate and initialize controller
          */
         $controller = $controllerFactory->create($controllerName);
     } catch (RouteNonexistentException $e) {
         /**
          * Log nonexistent route (404)
          */
         if ($this->logger) {
             $this->logger->addWarning('Route not connected', ['path' => $request->getPath()]);
         }
         $controller = $controllerFactory->create(NotFoundController::class);
         $action = 'index';
         $arguments = [];
     }
     /**
      * Set arguments
      */
     $controller->setArguments($arguments);
     /**
      * Validate and set controller action
      */
     try {
         $controller->setAction($action);
     } catch (ControllerActionProtectedInsufficientAuthenticationException $e) {
         /**
          * Log unauthed protected controller action (403)
          */
         if ($this->logger) {
             $this->logger->addWarning('Unauthenticated attempt to access protected action', ['path' => $request->getPath(), 'controller' => $controller->getShortName(), 'action' => $action]);
         }
         if ($this->configuration->exists('User.SignIn.Controller.Class') && $this->configuration->exists('User.SignIn.Controller.Action')) {
             /**
              * Save what controller and action was requested and then redirect to sign in form
              */
             // @todo test that this works
             $request->getCookie()->set('SignIn.onSuccess.path', $request->getPath());
             $controller = $controllerFactory->create($this->configuration->get('User.SignIn.Controller.Class'));
             $controller->setAction($this->configuration->get('User.SignIn.Controller.Action'));
         } else {
             $controller = $controllerFactory->create(NotFoundController::class);
             $controller->setAction('index');
         }
     } catch (ControllerActionPrivateInsufficientAuthenticationException $e) {
         /**
          * Log unauthed private controller action (403)
          */
         if ($this->logger) {
             $this->logger->addWarning('Unauthenticated attempt to access private action', ['path' => $request->getPath(), 'controller' => $controller->getShortName(), 'action' => $action]);
         }
     }
     /**
      * Create response from controller action headers and output
      */
     $response = $controller->callAction();
     /**
      * Performance logging
      */
     if ($this->logger) {
         $time = number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 2);
         // @todo $_SERVER usage
         $this->logger->addDebug("Dispatched request in {$time} ms", ['path' => $request->getPath()]);
     }
     return $response;
 }
Beispiel #3
0
 /**
  * @covers ::create
  */
 public function testCreateInvalidSuperclass()
 {
     $message = 'Controller Zortje\\MVC\\Tests\\Model\\Fixture\\CarEntity is not a subclass of Controller';
     $this->expectException(ControllerInvalidSuperclassException::class);
     $this->expectExceptionMessage($message);
     $controllerFactory = new ControllerFactory($this->pdo, $this->configuration, $this->request);
     $controllerFactory->create(CarEntity::class);
 }