Skip to content
Open
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"php": ">=5.5.9"
},
"require-dev": {
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0",
"cache/cache": "^1.0",
"symfony/cache": "^3.4.31|^4.3.4|^5.0",
"symfony/phpunit-bridge": "^5.1,<5.3",
Expand Down
41 changes: 32 additions & 9 deletions src/CachePoolTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of php-cache organization.
*
Expand Down Expand Up @@ -56,6 +58,24 @@ public function tearDownService()
}
}

/**
* Accepts either \Psr\Cache\InvalidArgumentException (library-level) or
* \TypeError (PHP-level contract violation) as a valid failure.
*
* @param callable(): void $callable
*/
private function assertCacheExceptionOrTypeError(callable $callable): void
{
try {
$callable();
$this->fail('Expected exception to be thrown.');
} catch (\TypeError $e) {
$this->assertTrue(true);
} catch (\Psr\Cache\InvalidArgumentException $e) {
$this->assertTrue(true);
}
}

/**
* Data provider for invalid keys.
*
Expand Down Expand Up @@ -360,9 +380,9 @@ public function testSaveExpired()

$item = $this->cache->getItem('key');
$item->set('value');
$item->expiresAt(\DateTime::createFromFormat('U', time() + 10));
$item->expiresAt(\DateTime::createFromFormat('U', (string) (time() + 10)));
$this->cache->save($item);
$item->expiresAt(\DateTime::createFromFormat('U', time() - 1));
$item->expiresAt(\DateTime::createFromFormat('U', (string) (time() - 1)));
$this->cache->save($item);
$item = $this->cache->getItem('key');
$this->assertFalse($item->isHit(), 'Cache should not save expired items');
Expand Down Expand Up @@ -421,7 +441,7 @@ public function testDeferredExpired()

$item = $this->cache->getItem('key');
$item->set('4711');
$item->expiresAt(\DateTime::createFromFormat('U', time() - 1));
$item->expiresAt(\DateTime::createFromFormat('U', (string) (time() - 1)));
$this->cache->saveDeferred($item);

$this->assertFalse($this->cache->hasItem('key'), 'Cache should not have expired deferred item');
Expand Down Expand Up @@ -581,8 +601,9 @@ public function testGetItemInvalidKeys($key)
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$this->expectException('Psr\Cache\InvalidArgumentException');
$this->cache->getItem($key);
$this->assertCacheExceptionOrTypeError(function () use ($key) {
$this->cache->getItem($key);
});
}

/**
Expand All @@ -609,8 +630,9 @@ public function testHasItemInvalidKeys($key)
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$this->expectException('Psr\Cache\InvalidArgumentException');
$this->cache->hasItem($key);
$this->assertCacheExceptionOrTypeError(function () use ($key) {
$this->cache->hasItem($key);
});
}

/**
Expand All @@ -623,8 +645,9 @@ public function testDeleteItemInvalidKeys($key)
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}

$this->expectException('Psr\Cache\InvalidArgumentException');
$this->cache->deleteItem($key);
$this->assertCacheExceptionOrTypeError(function () use ($key) {
$this->cache->deleteItem($key);
});
}

/**
Expand Down
Loading