Beispiel #1
0
 public function testCacheHit()
 {
     $request = new Request(new Url('http://localhost.com/foo/bar'), 'GET');
     $response = new Response();
     $response->setBody(new StringStream());
     $filters = array();
     $filters[] = function ($request, $response, $filterChain) {
         $response->getBody()->write('foobar');
         $filterChain->handle($request, $response);
     };
     $filterChain = new FilterChain($filters);
     $filterChain->handle($request, $response);
     $cache = new Cache(new CacheHandler\Memory());
     $item = $cache->getItem(md5('/foo/bar'));
     $item->set(array('headers' => array('Last-Modified' => 'Sat, 27 Dec 2014 15:54:49 GMT', 'Content-Type' => 'text/plain'), 'body' => 'foobar'));
     $cache->save($item);
     $filter = new StaticCache($cache);
     $filter->handle($request, $response, $filterChain);
     $result = $cache->getItem(md5('/foo/bar'))->get();
     $this->assertArrayHasKey('headers', $result);
     $this->assertArrayHasKey('Content-Type', $result['headers']);
     $this->assertEquals('text/plain', $result['headers']['Content-Type']);
     $this->assertArrayHasKey('Last-Modified', $result['headers']);
     $this->assertEquals('Sat, 27 Dec 2014 15:54:49 GMT', $result['headers']['Last-Modified']);
     $this->assertArrayHasKey('body', $result);
     $this->assertEquals('foobar', $result['body']);
 }
Beispiel #2
0
 public function testGroup()
 {
     $request = new Request(new Url('http://localhost'), 'GET');
     $response = new Response();
     $subFilters[] = new DummyFilter(3);
     $subFilters[] = new DummyFilter(4);
     $filters[] = new DummyFilter(1);
     $filters[] = new DummyFilter(2);
     $filters[] = new Group($subFilters);
     $filters[] = new DummyFilter(5);
     $filters[] = new DummyFilter(6);
     $filterChain = new FilterChain($filters);
     $filterChain->handle($request, $response);
     $this->assertEquals(array(1, 2, 3, 4, 5, 6), DummyFilter::$calls);
 }
Beispiel #3
0
 public function testExecuteControllerMethod()
 {
     $controller = $this->getMock('PSX\\ControllerInterface', array('onLoad', 'onDelete', 'onGet', 'onHead', 'onOptions', 'onPost', 'onPut', 'onTrace', 'processResponse', 'doFoo'));
     $controller->expects($this->once())->method('onLoad');
     $controller->expects($this->once())->method('onGet');
     $controller->expects($this->once())->method('doFoo');
     $controller->expects($this->once())->method('processResponse');
     $context = new Context();
     $context->set(Context::KEY_METHOD, 'doFoo');
     $request = new Request(new Url('http://localhost'), 'GET');
     $response = new Response();
     $filters = array();
     $filters[] = new ControllerExecutor($controller, $context);
     $filterChain = new FilterChain($filters);
     $filterChain->handle($request, $response);
 }
Beispiel #4
0
 public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain)
 {
     $filterChain = new FilterChain($this->filters, $filterChain);
     $filterChain->handle($request, $response);
 }
Beispiel #5
0
 public function testDecryptCookieDataChanged()
 {
     $request = new Request(new Url('http://localhost'), 'GET', array('Cookie' => 'psx_cookie=eyJmb28iOiJkYXRhIn0=.2IwNbitA6b1VccgR1pGVIRzro4AwYN1IqNXvNHYebog='));
     $response = new Response();
     $filters = array();
     $filters[] = new CookieSigner('secret_key');
     $filters[] = function ($request, $response, $filterChain) {
         $this->assertEquals(array('foo' => 'data'), $request->getAttribute(CookieSigner::COOKIE_NAME));
         $request->setAttribute(CookieSigner::COOKIE_NAME, array('foo' => 'foo'));
         $filterChain->handle($request, $response);
     };
     $filterChain = new FilterChain($filters);
     $filterChain->handle($request, $response);
     $this->assertEquals('psx_cookie=eyJmb28iOiJmb28ifQ==.lUT+fW1P+JlRv1+v0MtN7mQ9cx/OK5Jevt3wn/HXsj0=', $response->getHeader('Set-Cookie'));
     $this->assertEquals(array('foo' => 'foo'), $request->getAttribute(CookieSigner::COOKIE_NAME));
 }
Beispiel #6
0
 /**
  * Executes an specific controller direct without any routing
  *
  * @param mixed $controller
  * @param \PSX\Http\RequestInterface $request
  * @param \PSX\Http\ResponseInterface $response
  * @return void
  */
 public function executeController($controller, RequestInterface $request, ResponseInterface $response)
 {
     if ($controller instanceof ApplicationStackInterface) {
         $this->eventDispatcher->dispatch(Event::CONTROLLER_EXECUTE, new ControllerExecuteEvent($controller, $request, $response));
         $filterChain = new FilterChain($controller->getApplicationStack());
         $filterChain->setLogger($this->logger);
         $filterChain->handle($request, $response);
         $this->eventDispatcher->dispatch(Event::CONTROLLER_PROCESSED, new ControllerProcessedEvent($controller, $request, $response));
     } else {
         throw new UnexpectedValueException('Controller must be an instance of PSX\\ApplicationStackInterface');
     }
 }