diff --git a/composer.json b/composer.json index a4f0fe4..922f916 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ }, "require-dev": { "phpspec/phpspec": "^2.5", - "henrikbjorn/phpspec-code-coverage" : "^1.0" + "henrikbjorn/phpspec-code-coverage" : "^1.0", + "squizlabs/php_codesniffer": "^2.8" }, "autoload": { "psr-4": { @@ -33,7 +34,8 @@ }, "scripts": { "test": "vendor/bin/phpspec run", - "test-ci": "vendor/bin/phpspec run -c phpspec.ci.yml" + "test-ci": "vendor/bin/phpspec run -c phpspec.ci.yml", + "phpcs": "vendor/bin/phpcs src/* spec/*" }, "extra": { "branch-alias": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..ba41dfe --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,8 @@ + + + PSR-2 without CamelCapsMethodName for tests + + + */spec/* + + diff --git a/spec/CachePluginSpec.php b/spec/CachePluginSpec.php index 97afd55..d1cb8cd 100644 --- a/spec/CachePluginSpec.php +++ b/spec/CachePluginSpec.php @@ -14,7 +14,7 @@ class CachePluginSpec extends ObjectBehavior { - function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory) + public function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory) { $this->beConstructedWith($pool, $streamFactory, [ 'default_ttl' => 60, @@ -22,18 +22,23 @@ function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory) ]); } - function it_is_initializable(CacheItemPoolInterface $pool) + public function it_is_initializable(CacheItemPoolInterface $pool) { $this->shouldHaveType('Http\Client\Common\Plugin\CachePlugin'); } - function it_is_a_plugin() + public function it_is_a_plugin() { $this->shouldImplement('Http\Client\Common\Plugin'); } - function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream) - { + public function it_caches_responses( + CacheItemPoolInterface $pool, + CacheItemInterface $item, + RequestInterface $request, + ResponseInterface $response, + StreamInterface $stream + ) { $httpBody = 'body'; $stream->__toString()->willReturn($httpBody); $stream->isSeekable()->willReturn(true); @@ -43,9 +48,9 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i $request->getUri()->willReturn('/'); $response->getStatusCode()->willReturn(200); $response->getBody()->willReturn($stream); - $response->getHeader('Cache-Control')->willReturn(array())->shouldBeCalled(); - $response->getHeader('Expires')->willReturn(array())->shouldBeCalled(); - $response->getHeader('ETag')->willReturn(array())->shouldBeCalled(); + $response->getHeader('Cache-Control')->willReturn([])->shouldBeCalled(); + $response->getHeader('Expires')->willReturn([])->shouldBeCalled(); + $response->getHeader('ETag')->willReturn([])->shouldBeCalled(); $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); @@ -64,16 +69,21 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i return new FulfilledPromise($response->getWrappedObject()); }; - $this->handleRequest($request, $next, function () {}); + $this->handleRequest($request, $next, function () { + }); } - function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response) - { + public function it_doesnt_store_failed_responses( + CacheItemPoolInterface $pool, + CacheItemInterface $item, + RequestInterface $request, + ResponseInterface $response + ) { $request->getMethod()->willReturn('GET'); $request->getUri()->willReturn('/'); $response->getStatusCode()->willReturn(400); - $response->getHeader('Cache-Control')->willReturn(array()); - $response->getHeader('Expires')->willReturn(array()); + $response->getHeader('Cache-Control')->willReturn([]); + $response->getHeader('Expires')->willReturn([]); $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); @@ -82,11 +92,16 @@ function it_doesnt_store_failed_responses(CacheItemPoolInterface $pool, CacheIte return new FulfilledPromise($response->getWrappedObject()); }; - $this->handleRequest($request, $next, function () {}); + $this->handleRequest($request, $next, function () { + }); } - function it_doesnt_store_post_requests(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response) - { + public function it_doesnt_store_post_requests( + CacheItemPoolInterface $pool, + CacheItemInterface $item, + RequestInterface $request, + ResponseInterface $response + ) { $request->getMethod()->willReturn('POST'); $request->getUri()->willReturn('/'); @@ -94,12 +109,17 @@ function it_doesnt_store_post_requests(CacheItemPoolInterface $pool, CacheItemIn return new FulfilledPromise($response->getWrappedObject()); }; - $this->handleRequest($request, $next, function () {}); + $this->handleRequest($request, $next, function () { + }); } - - function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream) - { + public function it_calculate_age_from_response( + CacheItemPoolInterface $pool, + CacheItemInterface $item, + RequestInterface $request, + ResponseInterface $response, + StreamInterface $stream + ) { $httpBody = 'body'; $stream->__toString()->willReturn($httpBody); $stream->isSeekable()->willReturn(true); @@ -109,10 +129,10 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI $request->getUri()->willReturn('/'); $response->getStatusCode()->willReturn(200); $response->getBody()->willReturn($stream); - $response->getHeader('Cache-Control')->willReturn(array('max-age=40')); - $response->getHeader('Age')->willReturn(array('15')); - $response->getHeader('Expires')->willReturn(array()); - $response->getHeader('ETag')->willReturn(array()); + $response->getHeader('Cache-Control')->willReturn(['max-age=40']); + $response->getHeader('Age')->willReturn(['15']); + $response->getHeader('Expires')->willReturn([]); + $response->getHeader('ETag')->willReturn([]); $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); @@ -132,11 +152,17 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI return new FulfilledPromise($response->getWrappedObject()); }; - $this->handleRequest($request, $next, function () {}); + $this->handleRequest($request, $next, function () { + }); } - function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream) - { + public function it_saves_etag( + CacheItemPoolInterface $pool, + CacheItemInterface $item, + RequestInterface $request, + ResponseInterface $response, + StreamInterface $stream + ) { $httpBody = 'body'; $stream->__toString()->willReturn($httpBody); $stream->isSeekable()->willReturn(true); @@ -146,9 +172,9 @@ function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, R $request->getUri()->willReturn('/'); $response->getStatusCode()->willReturn(200); $response->getBody()->willReturn($stream); - $response->getHeader('Cache-Control')->willReturn(array()); - $response->getHeader('Expires')->willReturn(array()); - $response->getHeader('ETag')->willReturn(array('foo_etag')); + $response->getHeader('Cache-Control')->willReturn([]); + $response->getHeader('Expires')->willReturn([]); + $response->getHeader('ETag')->willReturn(['foo_etag']); $pool->getItem('d20f64acc6e70b6079845f2fe357732929550ae1')->shouldBeCalled()->willReturn($item); $item->isHit()->willReturn(false); @@ -167,17 +193,26 @@ function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, R return new FulfilledPromise($response->getWrappedObject()); }; - $this->handleRequest($request, $next, function () {}); + $this->handleRequest($request, $next, function () { + }); } - function it_adds_etag_and_modfied_since_to_request(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream) - { + public function it_adds_etag_and_modfied_since_to_request( + CacheItemPoolInterface $pool, + CacheItemInterface $item, + RequestInterface $request, + ResponseInterface $response, + StreamInterface $stream + ) { $httpBody = 'body'; $request->getMethod()->willReturn('GET'); $request->getUri()->willReturn('/'); - $request->withHeader('If-Modified-Since', 'Thursday, 01-Jan-70 01:18:31 GMT')->shouldBeCalled()->willReturn($request); + $request + ->withHeader('If-Modified-Since', 'Thursday, 01-Jan-70 01:18:31 GMT') + ->shouldBeCalled() + ->willReturn($request); $request->withHeader('If-None-Match', 'foo_etag')->shouldBeCalled()->willReturn($request); $response->getStatusCode()->willReturn(304); @@ -196,11 +231,18 @@ function it_adds_etag_and_modfied_since_to_request(CacheItemPoolInterface $pool, return new FulfilledPromise($response->getWrappedObject()); }; - $this->handleRequest($request, $next, function () {}); + $this->handleRequest($request, $next, function () { + }); } - function it_servces_a_cached_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream, StreamFactory $streamFactory) - { + public function it_servces_a_cached_response( + CacheItemPoolInterface $pool, + CacheItemInterface $item, + RequestInterface $request, + ResponseInterface $response, + StreamInterface $stream, + StreamFactory $streamFactory + ) { $httpBody = 'body'; $request->getMethod()->willReturn('GET'); @@ -224,11 +266,18 @@ function it_servces_a_cached_response(CacheItemPoolInterface $pool, CacheItemInt return new FulfilledPromise($response->getWrappedObject()); }; - $this->handleRequest($request, $next, function () {}); + $this->handleRequest($request, $next, function () { + }); } - function it_serves_and_resaved_expired_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream, StreamFactory $streamFactory) - { + public function it_serves_and_resaved_expired_response( + CacheItemPoolInterface $pool, + CacheItemInterface $item, + RequestInterface $request, + ResponseInterface $response, + StreamInterface $stream, + StreamFactory $streamFactory + ) { $httpBody = 'body'; $request->getMethod()->willReturn('GET'); @@ -238,8 +287,8 @@ function it_serves_and_resaved_expired_response(CacheItemPoolInterface $pool, Ca $request->withHeader(Argument::any(), Argument::any())->willReturn($request); $response->getStatusCode()->willReturn(304); - $response->getHeader('Cache-Control')->willReturn(array()); - $response->getHeader('Expires')->willReturn(array())->shouldBeCalled(); + $response->getHeader('Cache-Control')->willReturn([]); + $response->getHeader('Expires')->willReturn([])->shouldBeCalled(); // Make sure we add back the body $response->withBody($stream)->willReturn($response)->shouldBeCalled(); @@ -270,7 +319,8 @@ function it_serves_and_resaved_expired_response(CacheItemPoolInterface $pool, Ca return new FulfilledPromise($response->getWrappedObject()); }; - $this->handleRequest($request, $next, function () {}); + $this->handleRequest($request, $next, function () { + }); } @@ -283,7 +333,7 @@ function it_serves_and_resaved_expired_response(CacheItemPoolInterface $pool, Ca */ private function getCacheItemMatcher(array $expectedData) { - return Argument::that(function(array $actualData) use ($expectedData) { + return Argument::that(function (array $actualData) use ($expectedData) { foreach ($expectedData as $key => $value) { if (!isset($actualData[$key])) { return false; diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 5053aad..e612b98 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -185,7 +185,8 @@ protected function isCacheable(ResponseInterface $response) if (!$this->config['respect_cache_headers']) { return true; } - if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { + if ($this->getCacheControlDirective($response, 'no-store') || + $this->getCacheControlDirective($response, 'private')) { return false; } @@ -205,7 +206,6 @@ private function getCacheControlDirective(ResponseInterface $response, $name) $headers = $response->getHeader('Cache-Control'); foreach ($headers as $header) { if (preg_match(sprintf('|%s=?([0-9]+)?|i', $name), $header, $matches)) { - // return the value for $name if it exists if (isset($matches[1])) { return $matches[1];