From bd789d94aa7b4925b212f163b2cb8d8ef0368226 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Sat, 11 Feb 2017 11:01:56 +0700 Subject: [PATCH 1/4] Make PHPCS happy PSR1.Methods.CamelCapsMethodName is ignored for tests. --- phpcs.xml.dist | 8 +++ spec/CachePluginSpec.php | 110 ++++++++++++++++++++++++++++----------- src/CachePlugin.php | 6 +-- 3 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 phpcs.xml.dist 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..bfdbe53 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); @@ -64,11 +69,16 @@ 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); @@ -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); @@ -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); @@ -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'); @@ -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..42cc42c 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -185,8 +185,9 @@ protected function isCacheable(ResponseInterface $response) if (!$this->config['respect_cache_headers']) { return true; } - if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { - return false; + if ($this->getCacheControlDirective($response, 'no-store') || + $this->getCacheControlDirective($response, 'private')) { + return false; } return true; @@ -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]; From 7f5551462a13296b9122e0f1f80ee2ff01a4377f Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Sat, 11 Feb 2017 11:04:49 +0700 Subject: [PATCH 2/4] Use short array syntax See discussion in #24. --- spec/CachePluginSpec.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/spec/CachePluginSpec.php b/spec/CachePluginSpec.php index bfdbe53..d1cb8cd 100644 --- a/spec/CachePluginSpec.php +++ b/spec/CachePluginSpec.php @@ -48,9 +48,9 @@ public function it_caches_responses( $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); @@ -82,8 +82,8 @@ public function it_doesnt_store_failed_responses( $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); @@ -129,10 +129,10 @@ public function it_calculate_age_from_response( $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); @@ -172,9 +172,9 @@ public function it_saves_etag( $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); @@ -287,8 +287,8 @@ public function it_serves_and_resaved_expired_response( $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(); From 7a441578fd6cba7e90a078ccc2b37412f2e35642 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Sat, 11 Feb 2017 11:13:13 +0700 Subject: [PATCH 3/4] Use vendored PHPCS and enable running it via composer $ composer phpcs --- composer.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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": { From b5720e3d4e84785209dcfffa324d7f02c29b94b2 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Sat, 11 Feb 2017 11:22:18 +0700 Subject: [PATCH 4/4] Make StyleCI happy --- src/CachePlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachePlugin.php b/src/CachePlugin.php index 42cc42c..e612b98 100644 --- a/src/CachePlugin.php +++ b/src/CachePlugin.php @@ -187,7 +187,7 @@ protected function isCacheable(ResponseInterface $response) } if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { - return false; + return false; } return true;