Skip to content

PSR-2 compliance #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
8 changes: 8 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset name="HTTPlug">
<description>PSR-2 without CamelCapsMethodName for tests</description>
<rule ref="PSR2" />
<rule ref="PSR1.Methods.CamelCapsMethodName">
<exclude-pattern>*/spec/*</exclude-pattern>
</rule>
</ruleset>
138 changes: 94 additions & 44 deletions spec/CachePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,31 @@

class CachePluginSpec extends ObjectBehavior
{
function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
public function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
{
$this->beConstructedWith($pool, $streamFactory, [
'default_ttl' => 60,
'cache_lifetime' => 1000
]);
}

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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -82,24 +92,34 @@ 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('/');

$next = function (RequestInterface $request) use ($response) {
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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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();
Expand Down Expand Up @@ -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 () {
});
}


Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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];
Expand Down