Skip to content

Commit

Permalink
Remove abandoned dependency micheh/psr7-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Sep 4, 2021
1 parent 3b8a77b commit a4ab90f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 93 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"imbo/imbo-mysql-adapters": "dev-main",
"imbo/imbo-postgresql-adapters": "dev-main",
"imbo/imbo-sqlite-adapters": "dev-main",
"micheh/psr7-cache": "^0.5",
"phpunit/phpunit": "^9.2"
},
"suggest": {
Expand Down
55 changes: 1 addition & 54 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 26 additions & 22 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Psr\Http\Message\RequestInterface;
use Behat\Gherkin\Node\TableNode;
use Assert\Assertion;
use Micheh\Cache\CacheUtil;
use GuzzleHttp\Psr7\Header;
use MongoDB\Client as MongoClient;
use RuntimeException;
use InvalidArgumentException;
Expand All @@ -22,6 +22,7 @@
use Imagick;
use ImagickException;
use Imbo\Constraint\MultidimensionalArrayIsEqual;
use Psr\Http\Message\ResponseInterface;

class FeatureContext extends ApiContext {
/**
Expand All @@ -34,11 +35,6 @@ class FeatureContext extends ApiContext {
const MIDDLEWARE_HISTORY = 'imbo-behat-history';
const MIDDLEWARE_BEHAT_CONTEXT_CLASS = 'imbo-behat-context-class';

/**
* @var CacheUtil
*/
private $cacheUtil;

/**
* The public key used by the client
*
Expand Down Expand Up @@ -132,21 +128,6 @@ class FeatureContext extends ApiContext {
*/
static private $storageTestConfig;

/**
* Class constructor
*
* @param CacheUtil $cacheUtil
*/
public function __construct(CacheUtil $cacheUtil = null) {
// @codeCoverageIgnoreStart
if ($cacheUtil === null) {
$cacheUtil = new CacheUtil();
}
// @codeCoverageIgnoreEnd

$this->cacheUtil = $cacheUtil;
}

/**
* Set up adapters for the suite
*
Expand Down Expand Up @@ -1351,6 +1332,29 @@ public function assertPublicKeyDoesNotExist($publicKey) {
return $this;
}

/**
* Check if the response is cacheable
*
* @param ResponseInterface $response
* @return bool
*/
private function isCacheable(ResponseInterface $response): bool {
if (!in_array($response->getStatusCode(), [200, 203, 204, 206, 300, 301, 404, 405, 410, 414, 501])) {
return false;
}

if (!$response->hasHeader('Cache-Control')) {
return true;
}

$cacheControl = array_fill_keys(
Header::normalize($response->getHeaderLine('Cache-Control')),
true,
);

return !array_key_exists('no-store', $cacheControl) && !array_key_exists('private', $cacheControl);
}

/**
* Check whether or not the response can be cached
*
Expand All @@ -1368,7 +1372,7 @@ public function assertCacheability($cacheable = true) {

Assertion::same(
$cacheable,
$this->cacheUtil->isCacheable($this->response),
$this->isCacheable($this->response),
$cacheable ?
'Response was supposed to be cacheble, but it\'s not.' :
'Response was not supposed to be cacheable, but it is.'
Expand Down
32 changes: 16 additions & 16 deletions tests/Behat/FeatureContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use Imbo\Behat\FeatureContext;
use Imbo\BehatApiExtension\Exception\AssertionFailedException;
use Imbo\BehatApiExtension\ArrayContainsComparator;
use Micheh\Cache\CacheUtil;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
Expand All @@ -24,13 +23,13 @@
use RuntimeException;
use InvalidArgumentException;
use PHPUnit\Framework\ExpectationFailedException;
use Psr\Http\Message\ResponseInterface;

/**
* @coversDefaultClass Imbo\Behat\FeatureContext
*/
class FeatureContextTest extends TestCase {
private $context;
private $cacheUtil;
private $client;
private $history;
private $mockHandler;
Expand All @@ -49,9 +48,8 @@ public function setUp() : void {
'handler' => $this->handlerStack,
'base_uri' => $this->baseUri,
]);
$this->cacheUtil = $this->createMock(CacheUtil::class);

$this->context = new FeatureContext($this->cacheUtil);
$this->context = new FeatureContext();
$this->context->setClient($this->client, $this->baseUri);
}

Expand Down Expand Up @@ -2133,23 +2131,31 @@ public function testCanAssertThatPublicKeyDoesNotExist() : void {
public function getCacheabilityData() : array {
return [
'cacheable, expect cacheable' => [
'cacheable' => true,
'response' => new Response(200),
'expected' => true,
],
'not cacheable, expect not cacheable' => [
'cacheable' => false,
'response' => new Response(400),
'expected' => false,
],
'cacheable, expect not cacheable' => [
'cacheable' => true,
'response' => new Response(200),
'expected' => false,
'exceptionMessage' => 'Response was not supposed to be cacheable, but it is.',
],
'not cacheable, expect cacheable' => [
'cacheable' => false,
'cacheable' => new Response(400),
'expected' => true,
'exceptionMessage' => 'Response was supposed to be cacheble, but it\'s not.',
],
'not cacheable (no-store)' => [
'response' => new Response(200, ['cache-control' => 'no-store']),
'expected' => false,
],
'not cacheable (private)' => [
'response' => new Response(200, ['cache-control' => 'private']),
'expected' => false,
],
];
}

Expand All @@ -2158,14 +2164,8 @@ public function getCacheabilityData() : array {
* @covers ::__construct
* @covers ::assertCacheability
*/
public function testCanAssertResponseCacheability(bool $actual, bool $expected, string $exceptionMessage = null) : void {
$this->cacheUtil
->expects($this->once())
->method('isCacheable')
->with($this->isInstanceOf(Response::class))
->willReturn($actual);

$this->mockHandler->append(new Response(200));
public function testCanAssertResponseCacheability(ResponseInterface $response, bool $expected, string $exceptionMessage = null) : void {
$this->mockHandler->append($response);
$this->context->requestPath('/path');

if ($exceptionMessage) {
Expand Down

0 comments on commit a4ab90f

Please sign in to comment.