/** * @dataProvider getConfigureParams * * @param Response $response * @param Request $request * @param int $lifetime * @param array $private_headers * @param bool $expected_public */ public function testConfigure(Response $response, Request $request, $lifetime, array $private_headers, $expected_public) { $last_modified = new \DateTime(); if ($response->getEtag()) { $etag = $response->getEtag(); } else { $etag = md5('bar'); $this->key_builder->expects($this->once())->method('getEtag')->with($response)->will($this->returnValue($etag)); $etag = '"' . $etag . '"'; } $this->request_stack->expects($this->once())->method('getMasterRequest')->will($this->returnValue($request)); $expires = null; if ($lifetime >= 0) { $expires = clone $last_modified; $expires->modify(sprintf('now +%s seconds', $lifetime)); } $configurator = new ResponseConfigurator($this->key_builder, $this->request_stack, $private_headers); // exec test method $configured_response = $configurator->configure($response, $last_modified, $lifetime); $this->assertEquals($response, $configured_response); $this->assertEquals($last_modified, $response->getLastModified()); $this->assertEquals($etag, $response->getEtag()); $this->assertTrue($response->headers->hasCacheControlDirective('must-revalidate')); $this->assertTrue($response->headers->hasCacheControlDirective($expected_public ? 'public' : 'private')); if ($lifetime >= 0) { $this->assertEquals($lifetime, $response->headers->getCacheControlDirective('max-age')); $this->assertEquals($expires, $response->getExpires()); if ($expected_public) { $this->assertEquals($lifetime, $response->headers->getCacheControlDirective('s-maxage')); } } }
/** * @param LifecycleEventArgs $args * @param bool $remove */ protected function update(LifecycleEventArgs $args, $remove) { $alias = $this->builder->getEntityAlias($args->getEntity()); $this->keeper->set($alias, new \DateTime()); if ($this->track_individually_entity) { $ids = $this->builder->getEntityIdentifier($args->getEntity()); if ($ids !== null) { if ($remove) { $this->keeper->remove($alias . $ids); } else { $this->keeper->set($alias . $ids, new \DateTime()); } } } }
/** * Set ETag. * * Need set Last-Modified before ETag * * @param Response $response * * @return ResponseConfigurator */ protected function setEtag(Response $response) { if (!$response->getEtag()) { $response->setEtag($this->key_builder->getEtag($response)); } return $this; }
public function testGetEtag() { $etag = 'foo'; /** @var $response \PHPUnit_Framework_MockObject_MockObject|Response */ $response = $this->getNoConstructorMock(Response::class); $this->etag_hasher->expects($this->once())->method('hash')->with($response)->will($this->returnValue($etag)); $this->assertEquals($etag, $this->builder->getEtag($response)); }
/** * @dataProvider getTrackMethods * * @param string $method * @param bool $track_individually * @param bool $remove * @param array $ids */ public function testHandleEvent($method, $track_individually, $remove, array $ids) { $alias = 'foo'; $this->keeper->expects($this->at(0))->method('set')->with($alias, $this->isInstanceOf('DateTime')); $this->builder->expects($this->once())->method('getEntityAlias')->with($this->entity)->will($this->returnValue($alias)); if ($track_individually) { $suffix = implode(',', $ids); $this->builder->expects($this->once())->method('getEntityIdentifier')->with($this->entity)->will($this->returnValue($suffix)); if ($suffix) { if ($remove) { $this->keeper->expects($this->once())->method('remove')->with($alias . $suffix); } else { $this->keeper->expects($this->at(1))->method('set')->with($alias . $suffix, $this->isInstanceOf('DateTime')); } } } else { $this->builder->expects($this->never())->method('getEntityIdentifier'); } $listener = new DoctrineListener($this->keeper, $this->builder, $track_individually); call_user_func([$listener, $method], $this->args); }