setRequest() public method

Set request
public setRequest ( Zend\Stdlib\RequestInterface $request ) : MvcEvent
$request Zend\Stdlib\RequestInterface
return MvcEvent
Ejemplo n.º 1
7
 public function __invoke(MvcEvent $event)
 {
     if ($event->getRouteMatch()->getMatchedRouteName() === 'oauth/authorize' || $event->getRouteMatch()->getMatchedRouteName() === 'oauth/code') {
         $auth = $this->authentication;
         if (!$auth->hasIdentity()) {
             //redirect to login form before granting permissions - exception would be client_credentials grant type
             $url = $event->getRouter()->assemble([], array('name' => 'dotuser/login'));
             $host = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'on' ? 'https://' : 'http://';
             $host .= $_SERVER['HTTP_HOST'];
             $url = $host . $url . '?redirect=' . urlencode($event->getRequest()->getUriString());
             $response = $event->getResponse();
             $response->getHeaders()->addHeaderLine('Location', $url);
             $response->setStatusCode(302);
             $response->sendHeaders();
             exit;
         } else {
             $identity = $auth->getIdentity();
             $request = $event->getRequest();
             $client_id = $request->getQuery('client_id');
             //check to see if user already ganted permissions and is not revoked to skip the step and redirecting directly
             if ($event->getRouteMatch()->getMatchedRouteName() === 'oauth/authorize') {
                 if ($this->userRevokeStorage->isAuthorized($client_id, $identity->getUsername())) {
                     $newRequest = new \ZF\ContentNegotiation\Request();
                     $newRequest->setMethod(\Zend\Http\Request::METHOD_POST);
                     $newRequest->getPost()->set('authorized', 'yes');
                     $event->setRequest($newRequest);
                 }
             }
         }
     }
 }
 /**
  * @see \PHPUnit_Framework_TestCase::setUp()
  */
 protected function setUp()
 {
     $this->manager = $this->getMockBuilder(Manager::class)->disableOriginalConstructor()->getMock();
     $this->repository = $this->getMockBuilder(ImageRepository::class)->disableOriginalConstructor()->getMock();
     $this->request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock();
     $this->event = new MvcEvent();
     $this->event->setRequest($this->request);
     $this->listener = new ApplicationListener($this->manager, $this->repository);
 }
Ejemplo n.º 3
0
 public function testOnResponseInAjaxHttpRequestContext()
 {
     $this->moduleOptions->setBrowserTimingEnabled(true);
     $this->client->expects($this->once())->method('disableAutorum');
     $this->client->expects($this->never())->method('getBrowserTimingHeader');
     $this->client->expects($this->never())->method('getBrowserTimingFooter');
     $request = $this->createMock(HttpRequest::class);
     $request->expects($this->once())->method('isXmlHttpRequest')->will($this->returnValue(true));
     $this->event->setRequest($request);
     $this->listener->onResponse($this->event);
 }
 protected function setUp()
 {
     $this->console = $this->getMockOfConsole();
     $this->controller = new IndexController();
     $this->event = new MvcEvent();
     $this->request = new Request();
     $this->response = new Response();
     $this->routeMatch = new RouteMatch(array('controller' => 'index'));
     $this->controller->setConsole($this->console);
     $this->controller->setEvent($this->event);
     $this->event->setRequest($this->request);
     $this->event->setResponse($this->response);
     $this->event->setRouteMatch($this->routeMatch);
 }
Ejemplo n.º 5
0
    /**
     * Dispatch a request
     * 
     * @events dispatch.pre, dispatch.post
     * @param  Request $request 
     * @param  null|Response $response 
     * @param  null|Event $e
     * @return Response|mixed
     */
    public function dispatch(Request $request, Response $response = null, Event $e = null)
    {
        $this->request = $request;
        if (!$response) {
            $response = new HttpResponse();
        }
        $this->response = $response;

        if ($e instanceof Event && !$e instanceof MvcEvent) {
            $eventParams = $e->getParams();
            $e = new MvcEvent();
            $e->setParams($eventParams);
            unset($eventParams);
        }
        if (null === $e) {
            $e = new MvcEvent();
        }
        $e->setRequest($request)
          ->setResponse($response)
          ->setTarget($this);
        $this->event = $e;

        $result = $this->events()->trigger('dispatch', $e, function($test) {
            return ($test instanceof Response);
        });

        if ($result->stopped()) {
            return $result->last();
        }
        return $e->getResult();
    }
 public function testOnRenderErrorCreatesAnApiProblemResponse()
 {
     $response = new Response();
     $request = new Request();
     $request->getHeaders()->addHeaderLine('Accept', 'application/json');
     $event = new MvcEvent();
     $event->setError(Application::ERROR_EXCEPTION);
     $event->setRequest($request);
     $event->setResponse($response);
     $this->listener->onRenderError($event);
     $this->assertTrue($event->propagationIsStopped());
     $this->assertSame($response, $event->getResponse());
     $this->assertEquals(406, $response->getStatusCode());
     $headers = $response->getHeaders();
     $this->assertTrue($headers->has('Content-Type'));
     $this->assertEquals('application/problem+json', $headers->get('content-type')->getFieldValue());
     $content = json_decode($response->getContent(), true);
     $this->assertArrayHasKey('status', $content);
     $this->assertArrayHasKey('title', $content);
     $this->assertArrayHasKey('describedBy', $content);
     $this->assertArrayHasKey('detail', $content);
     $this->assertEquals(406, $content['status']);
     $this->assertEquals('Not Acceptable', $content['title']);
     $this->assertContains('www.w3.org', $content['describedBy']);
     $this->assertContains('accept', $content['detail']);
 }
Ejemplo n.º 7
0
    public function setUp()
    {
        StaticEventManager::resetInstance();

        $mockSharedEventManager = $this->getMock('Zend\EventManager\SharedEventManagerInterface');
        $mockSharedEventManager->expects($this->any())->method('getListeners')->will($this->returnValue(array()));
        $mockEventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
        $mockEventManager->expects($this->any())->method('getSharedManager')->will($this->returnValue($mockSharedEventManager));
        $mockApplication = $this->getMock('Zend\Mvc\ApplicationInterface');
        $mockApplication->expects($this->any())->method('getEventManager')->will($this->returnValue($mockEventManager));

        $event   = new MvcEvent();
        $event->setApplication($mockApplication);
        $event->setRequest(new Request());
        $event->setResponse(new Response());

        $routeMatch = new RouteMatch(array('action' => 'test'));
        $routeMatch->setMatchedRouteName('some-route');
        $event->setRouteMatch($routeMatch);

        $locator = new Locator;
        $locator->add('forward', function () {
            return new ForwardController();
        });

        $this->controller = new SampleController();
        $this->controller->setEvent($event);
        $this->controller->setServiceLocator($locator);

        $this->plugin = $this->controller->plugin('forward');
    }
 public function testDoNothingIfNotHttpRequest()
 {
     $event = new MvcEvent();
     /* @var \Zend\Stdlib\RequestInterface $request */
     $request = $this->getMock(RequestInterface::class);
     $event->setRequest($request);
     $this->assertNull($this->httpMethodOverrideListener->overrideHttpMethod($event));
 }
 public function testCanStartSession()
 {
     $event = new MvcEvent();
     $event->setApplication($this->getApplication());
     $event->setRequest(new Request());
     $sessionListener = new RouteListener();
     $sessionListener->startSession($event);
     $this->assertArrayHasKey('__ZF', $_SESSION);
 }
 public function testNoneHtmlRequest()
 {
     $request = $this->getMock(RequestInterface::class);
     $event = new MvcEvent();
     $event->setRequest($request);
     $clonedRequest = clone $request;
     $this->listener->override($event);
     $this->assertEquals($clonedRequest, $request);
 }
 public function testSet201StatusCodeIfPost()
 {
     $request = new HttpRequest();
     $request->setMethod(HttpRequest::METHOD_POST);
     $this->event->setRequest($request);
     $this->response->setContent('foo');
     $this->resourceResponseListener->finishResponse($this->event);
     $this->assertEquals(201, $this->response->getStatusCode());
 }
 public function setUp()
 {
     Console::overrideIsConsole(false);
     parent::setUp();
     $this->request = new Request();
     $this->request->setHeaders(new Headers());
     $this->routeMatch = new RouteMatch(array('controller' => $this->controllerName));
     $this->event = $this->getApplication()->getMvcEvent();
     $this->event->setRequest($this->request);
     $this->event->setRouteMatch($this->routeMatch);
     $this->event->getRouter()->setRequestUri(new HttpUri('http://localhost'));
     if (null === $this->controller) {
         if (null === $this->controllerName) {
             throw new PHPUnit_Framework_Exception('No controller name was specified in the test');
         }
         $this->controller = $this->getServiceManager()->get('ControllerLoader')->get($this->controllerName);
     }
     $this->controller->setEvent($this->event);
 }
Ejemplo n.º 13
0
 public function setUp()
 {
     // authentication service
     $this->authentication = new AuthenticationService(new NonPersistent());
     $this->request = $request = new HttpRequest();
     $this->response = $response = new HttpResponse();
     $mvcEvent = new MvcEvent();
     $mvcEvent->setRequest($request)->setResponse($response);
     $this->event = new MvcAuthEvent($mvcEvent, $this->authentication, $this->getMock('ZF\\MvcAuth\\Authorization\\AuthorizationInterface'));
 }
 public function setUp()
 {
     $this->request = new Request();
     $event = new MvcEvent();
     $event->setRequest($this->request);
     $this->event = $event;
     $this->controller = new SampleController();
     $this->controller->setEvent($event);
     $this->plugin = $this->controller->plugin('acceptableViewModelSelector');
 }
 public function testBailsEarlyOnMissingRouteMatch()
 {
     $listener = $this->listener;
     $request = new HttpRequest();
     $response = new HttpResponse();
     $mvcEvent = new MvcEvent();
     $mvcEvent->setRequest($request)->setResponse($response);
     $mvcAuthEvent = new MvcAuthEvent($mvcEvent, $this->authentication, $this->authorization);
     $this->assertNull($listener($mvcAuthEvent));
 }
 public function testThrowExceptionIfUnknownMethod()
 {
     $this->setExpectedException(MethodNotAllowedException::class);
     $request = new HttpRequest();
     $request->setMethod('PATCH');
     $this->mvcEvent->setRequest($request);
     $routeMatch = new RouteMatch([]);
     $this->mvcEvent->setRouteMatch($routeMatch);
     $this->simpleController->onDispatch($this->mvcEvent);
 }
Ejemplo n.º 17
0
 public function setUp()
 {
     $this->request = new Request();
     $event = new MvcEvent();
     $event->setRequest($this->request);
     $event->setResponse(new Response());
     $event->setRouteMatch(new RouteMatch(array('value' => 'rm:1234')));
     $this->controller = new SampleController();
     $this->controller->setEvent($event);
     $this->plugin = $this->controller->plugin('params');
 }
 public function setUp()
 {
     $routeMatch = new RouteMatch(array());
     $request = new HttpRequest();
     $response = new HttpResponse();
     $mvcEvent = new MvcEvent();
     $mvcEvent->setRequest($request)->setResponse($response)->setRouteMatch($routeMatch);
     $this->mvcAuthEvent = $this->createMvcAuthEvent($mvcEvent);
     $this->restControllers = array('ZendCon\\V1\\Rest\\Session\\Controller' => 'session_id');
     $this->listener = new DefaultResourceResolverListener($this->restControllers);
 }
Ejemplo n.º 19
0
 /**
  * @param array  $regexpes
  * @param string $uriPath
  * @param bool   $expectedResult
  * @dataProvider shouldCacheProvider
  */
 public function testShouldCache($regexpes, $uriPath, $expectedResult)
 {
     $this->strategy->setRegexpes($regexpes);
     $mvcEvent = new MvcEvent();
     $request = new HttpRequest();
     $uri = new Http();
     $uri->setPath($uriPath);
     $request->setUri($uri);
     $mvcEvent->setRequest($request);
     $this->assertEquals($expectedResult, $this->strategy->shouldCache($mvcEvent));
 }
 public function testFromQueryMethod()
 {
     $_GET = array('test1' => 'value1', 'test2' => 'value2');
     $r = new Request();
     $e = new MvcEvent();
     $e->setRequest($r);
     $helper = new Helper($e);
     $this->assertEquals('value1', $helper->fromQuery('test1'));
     $this->assertEquals($_GET, $helper->fromQuery());
     $this->assertEquals('default', $helper->fromQuery('not_there', 'default'));
 }
Ejemplo n.º 21
0
 public function testCreateService()
 {
     $sm = new ServiceManager(new Config(['services' => ['config' => include __DIR__ . '/../../../../config/module.config.php', 'eventmanager' => new EventManager(), 'router' => new TreeRouteStack(), 'request' => new Request(), 'routematch' => new RouteMatch([])]]));
     $event = new MvcEvent();
     $event->setRequest($sm->get('request'))->setRouter($sm->get('router'))->setRouteMatch($sm->get('routematch'));
     $app = $this->getMockBuilder('Zend\\Mvc\\Application')->disableOriginalConstructor()->getMock();
     $app->method('getMvcEvent')->willReturn($event);
     $sm->setService('application', $app);
     $navigation = $this->factory->createService($sm);
     $this->assertInstanceOf('Zend\\Navigation\\Navigation', $navigation);
 }
 public function testAddAuthorizationToVaryIfNotExists()
 {
     $request = new HttpRequest();
     $request->getHeaders()->addHeaderLine('Authorization', 'abc');
     $mvcEvent = new MvcEvent();
     $mvcEvent->setRequest($request);
     $response = new HttpResponse();
     $mvcEvent->setResponse($response);
     $listener = new AuthorizationVaryListener();
     $listener->alterVaryHeader($mvcEvent);
     $this->assertTrue($response->getHeaders()->has('Vary'));
     $this->assertEquals('Authorization', $response->getHeaders()->get('Vary')->getFieldValue());
 }
 /**
  * @dataProvider httpMethods
  */
 public function testHttpMethodOverrideListenerReturnsProblemResponseForIllegalOverrideValue($method)
 {
     $listener = $this->listener;
     $request = new HttpRequest();
     $request->setMethod('POST');
     $request->getHeaders()->addHeaderLine('X-HTTP-Method-Override', $method);
     $event = new MvcEvent();
     $event->setRequest($request);
     $result = $listener->onRoute($event);
     $this->assertInstanceOf(ApiProblemResponse::class, $result);
     $problem = $result->getApiProblem();
     $this->assertEquals(400, $problem->status);
     $this->assertContains(sprintf('Illegal override method %s in X-HTTP-Method-Override header', $method), $problem->detail);
 }
Ejemplo n.º 24
0
 public function setUp()
 {
     StaticEventManager::resetInstance();
     $mockSharedEventManager = $this->getMock('Zend\\EventManager\\SharedEventManagerInterface');
     $mockSharedEventManager->expects($this->any())->method('getListeners')->will($this->returnValue(array()));
     $mockEventManager = $this->getMock('Zend\\EventManager\\EventManagerInterface');
     $mockEventManager->expects($this->any())->method('getSharedManager')->will($this->returnValue($mockSharedEventManager));
     $mockApplication = $this->getMock('Zend\\Mvc\\ApplicationInterface');
     $mockApplication->expects($this->any())->method('getEventManager')->will($this->returnValue($mockEventManager));
     $event = new MvcEvent();
     $event->setApplication($mockApplication);
     $event->setRequest(new Request());
     $event->setResponse(new Response());
     $routeMatch = new RouteMatch(array('action' => 'test'));
     $routeMatch->setMatchedRouteName('some-route');
     $event->setRouteMatch($routeMatch);
     $services = new Locator();
     $plugins = $this->plugins = new PluginManager();
     $plugins->setServiceLocator($services);
     $controllers = $this->controllers = new ControllerManager();
     $controllers->setFactory('forward', function () use($plugins) {
         $controller = new ForwardController();
         $controller->setPluginManager($plugins);
         return $controller;
     });
     $controllers->setInvokableClass('ZendTest\\Mvc\\Controller\\TestAsset\\ForwardFq', 'ZendTest\\Mvc\\Controller\\TestAsset\\ForwardFqController');
     $controllers->setServiceLocator($services);
     $controllerLoader = function () use($controllers) {
         return $controllers;
     };
     $services->add('ControllerLoader', $controllerLoader);
     $services->add('ControllerManager', $controllerLoader);
     $services->add('ControllerPluginManager', function () use($plugins) {
         return $plugins;
     });
     $services->add('Zend\\ServiceManager\\ServiceLocatorInterface', function () use($services) {
         return $services;
     });
     $services->add('EventManager', function () use($mockEventManager) {
         return $mockEventManager;
     });
     $services->add('SharedEventManager', function () use($mockSharedEventManager) {
         return $mockSharedEventManager;
     });
     $this->controller = new SampleController();
     $this->controller->setEvent($event);
     $this->controller->setServiceLocator($services);
     $this->controller->setPluginManager($plugins);
     $this->plugin = $this->controller->plugin('forward');
 }
 public function setUp()
 {
     // authentication service
     $this->authentication = new AuthenticationService(new NonPersistent());
     // authorization service
     $this->authorization = $this->getMockBuilder('ZF\\MvcAuth\\Authorization\\AuthorizationInterface')->getMock();
     // event for mvc and mvc-auth
     $this->request = new HttpRequest();
     $this->response = new HttpResponse();
     $mvcEvent = new MvcEvent();
     $mvcEvent->setRequest($this->request)->setResponse($this->response);
     $this->mvcAuthEvent = new MvcAuthEvent($mvcEvent, $this->authentication, $this->authorization);
     $this->listener = new DefaultAuthenticationListener();
 }
Ejemplo n.º 26
0
 /**
  * @param array   $routes
  * @param string  $route
  * @param boolean $expectedResult
  * @param array   $params
  * @param string  $httpMethod
  * @dataProvider shouldCacheProvider
  */
 public function testShouldCache($routes, $route, $expectedResult, $params = array(), $httpMethod = null)
 {
     $this->strategy->setRoutes($routes);
     $routeMatch = new RouteMatch($params);
     $routeMatch->setMatchedRouteName($route);
     $request = new Request();
     if ($httpMethod !== null) {
         $request->setMethod($httpMethod);
     }
     $mvcEvent = new MvcEvent();
     $mvcEvent->setRouteMatch($routeMatch);
     $mvcEvent->setRequest($request);
     $this->assertEquals($expectedResult, $this->strategy->shouldCache($mvcEvent));
 }
 public function testMultipleRegistrationShouldNotResultInMultiplePrefixingOfControllerName()
 {
     $moduleListener = new ModuleRouteListener();
     $this->events->attach($moduleListener);
     $this->router->addRoute('foo', array('type' => 'Literal', 'options' => array('route' => '/foo', 'defaults' => array(ModuleRouteListener::MODULE_NAMESPACE => 'Foo', 'controller' => 'Index'))));
     $this->request->setUri('/foo');
     $event = new MvcEvent();
     $event->setRouter($this->router);
     $event->setRequest($this->request);
     $this->events->trigger('route', $event);
     $matches = $event->getRouteMatch();
     $this->assertInstanceOf('Zend\\Mvc\\Router\\RouteMatch', $matches);
     $this->assertEquals('Foo\\Index', $matches->getParam('controller'));
     $this->assertEquals('Index', $matches->getParam(ModuleRouteListener::ORIGINAL_CONTROLLER));
 }
Ejemplo n.º 28
0
 public function testRouteMatchIsTransformedToProperControllerClassName()
 {
     $moduleListener = new ModuleRouteListener();
     $this->events->attach($moduleListener);
     $this->router->addRoute('foo', array('type' => 'Literal', 'options' => array('route' => '/foo', 'defaults' => array(ModuleRouteListener::MODULE_NAMESPACE => 'Foo', 'controller' => 'some-index'))));
     $this->request->setUri('/foo');
     $event = new MvcEvent();
     $event->setRouter($this->router);
     $event->setRequest($this->request);
     $this->events->trigger('route', $event);
     $matches = $event->getRouteMatch();
     $this->assertInstanceOf('Zend\\Mvc\\Router\\RouteMatch', $matches);
     $this->assertEquals('Foo\\SomeIndex', $matches->getParam('controller'));
     $this->assertEquals('some-index', $matches->getParam(ModuleRouteListener::ORIGINAL_CONTROLLER));
 }
Ejemplo n.º 29
0
 public function setUp()
 {
     $event = new MvcEvent();
     $event->setRequest(new Request());
     $event->setResponse(new Response());
     $event->setRouteMatch(new RouteMatch(array('action' => 'test')));
     $locator = new Locator();
     $locator->add('forward', function () {
         return new ForwardController();
     });
     $this->controller = new SampleController();
     $this->controller->setEvent($event);
     $this->controller->setServiceLocator($locator);
     $this->plugin = $this->controller->plugin('forward');
 }
Ejemplo n.º 30
0
 public function create(MvcEvent $event)
 {
     $batchRequest = $event->getRequest();
     $router = $this->getRouter();
     $controllerLoader = $this->getControllerLoader();
     $responseModel = [];
     foreach ($event->getParam('data') as $key => $requestData) {
         $request = $this->getSubRequest($batchRequest, $requestData);
         $response = new Response();
         $subEvent = new MvcEvent();
         $subEvent->setRequest($request);
         $subEvent->setResponse($response);
         $match = $router->match($request);
         RouteListener::resolveController($match);
         $contentModel = null;
         if (!isset($match) || $match->getMatchedRouteName() != 'rest') {
             $contentModel = $this->createExceptionContentModel(new Exception\RuntimeException(sprintf('%s uri is not a rest route, so is not supported by batch controller.', $requestData['uri'])), $subEvent);
         } else {
             try {
                 $controller = $controllerLoader->get($match->getParam('controller'));
             } catch (\Zend\ServiceManager\Exception\ServiceNotFoundException $exception) {
                 $contentModel = $this->createExceptionContentModel($exception, $subEvent);
                 $response->setStatusCode(404);
             }
             $subEvent->setRouteMatch($match);
             $controller->setEvent($subEvent);
             if (!isset($contentModel)) {
                 try {
                     $contentModel = $controller->dispatch($request, $response);
                 } catch (\Exception $exception) {
                     $contentModel = $this->createExceptionContentModel($exception, $subEvent);
                 }
             }
         }
         foreach ($response->getHeaders() as $header) {
             $headers[$header->getFieldName()] = $header->getFieldValue();
         }
         $responseModel[$key] = ['status' => $response->getStatusCode(), 'headers' => $headers];
         if ($contentModel instanceof ModelInterface) {
             $responseModel[$key]['content'] = $contentModel->getVariables();
         }
     }
     $result = new Result();
     $result->setSerializedModel($responseModel);
     $event->setResult($result);
     return $result;
 }