| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- <?php
- namespace Sabre\Cache;
- use Psr\SimpleCache\CacheInterface;
- /**
- * Abstract PSR-16 tester.
- *
- * Because all cache implementations should mostly behave the same way, they
- * can all extend this test.
- */
- abstract class AbstractCacheTest extends \PHPUnit\Framework\TestCase {
- abstract function getCache() : CacheInterface;
- function testSetGet() {
- $cache = $this->getCache();
- $cache->set('foo', 'bar');
- $this->assertEquals('bar', $cache->get('foo'));
- }
- /**
- * @depends testSetGet
- */
- function testDelete() {
- $cache = $this->getCache();
- $cache->set('foo', 'bar');
- $this->assertEquals('bar', $cache->get('foo'));
- $cache->delete('foo');
- $this->assertNull($cache->get('foo'));
- }
- /**
- * @expectedException \Psr\SimpleCache\InvalidArgumentException
- */
- function testGetInvalidArg() {
- $cache = $this->getCache();
- $cache->get(null);
- }
- /**
- * @depends testDelete
- */
- function testGetNotFound() {
- $cache = $this->getCache();
- $this->assertNull($cache->get('notfound'));
- }
- /**
- * @depends testDelete
- */
- function testGetNotFoundDefault() {
- $cache = $this->getCache();
- $default = 'chickpeas';
- $this->assertEquals(
- $default,
- $cache->get('notfound', $default)
- );
- }
- /**
- * @depends testSetGet
- * @slow
- */
- function testSetExpire() {
- $cache = $this->getCache();
- $cache->set('foo', 'bar', 1);
- $this->assertEquals('bar', $cache->get('foo'));
- // Wait 2 seconds so the cache expires
- usleep(2000000);
- $this->assertNull($cache->get('foo'));
- }
- /**
- * @depends testSetGet
- * @slow
- */
- function testSetExpireDateInterval() {
- $cache = $this->getCache();
- $cache->set('foo', 'bar', new \DateInterval('PT1S'));
- $this->assertEquals('bar', $cache->get('foo'));
- // Wait 2 seconds so the cache expires
- usleep(2000000);
- $this->assertNull($cache->get('foo'));
- }
- /**
- * @expectedException \Psr\SimpleCache\InvalidArgumentException
- */
- function testSetInvalidArg() {
- $cache = $this->getCache();
- $cache->set(null, 'bar');
- }
- /**
- * @expectedException \Psr\SimpleCache\InvalidArgumentException
- */
- function testDeleteInvalidArg() {
- $cache = $this->getCache();
- $cache->delete(null);
- }
- /**
- * @depends testSetGet
- */
- function testClearCache() {
- $cache = $this->getCache();
- $cache->set('foo', 'bar');
- $cache->clear();
- $this->assertNull($cache->get('foo'));
- }
- /**
- * @depends testSetGet
- */
- function testHas() {
- $cache = $this->getCache();
- $cache->set('foo', 'bar');
- $this->assertTrue($cache->has('foo'));
- }
- /**
- * @depends testHas
- */
- function testHasNot() {
- $cache = $this->getCache();
- $this->assertFalse($cache->has('not-found'));
- }
- /**
- * @expectedException \Psr\SimpleCache\InvalidArgumentException
- */
- function testHasInvalidArg() {
- $cache = $this->getCache();
- $cache->has(null);
- }
- /**
- * @depends testSetGet
- */
- function testSetGetMultiple() {
- $values = [
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- ];
- $cache = $this->getCache();
- $cache->setMultiple($values);
- $result = $cache->getMultiple(array_keys($values));
- foreach ($result as $key => $value) {
- $this->assertTrue(isset($values[$key]));
- $this->assertEquals($values[$key], $value);
- unset($values[$key]);
- }
- // The list of values should now be empty
- $this->assertEquals([], $values);
- }
- /**
- * @depends testSetGet
- */
- function testSetGetMultipleGenerator() {
- $values = [
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- ];
- $gen = function() use ($values) {
- foreach ($values as $key => $value) {
- yield $key => $value;
- }
- };
- $cache = $this->getCache();
- $cache->setMultiple($gen());
- $result = $cache->getMultiple(array_keys($values));
- foreach ($result as $key => $value) {
- $this->assertTrue(isset($values[$key]));
- $this->assertEquals($values[$key], $value);
- unset($values[$key]);
- }
- // The list of values should now be empty
- $this->assertEquals([], $values);
- }
- /**
- * @depends testSetGet
- */
- function testSetGetMultipleGenerator2() {
- $values = [
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- ];
- $gen = function() use ($values) {
- foreach ($values as $key => $value) {
- yield $key;
- }
- };
- $cache = $this->getCache();
- $cache->setMultiple($values);
- $result = $cache->getMultiple($gen());
- foreach ($result as $key => $value) {
- $this->assertTrue(isset($values[$key]));
- $this->assertEquals($values[$key], $value);
- unset($values[$key]);
- }
- // The list of values should now be empty
- $this->assertEquals([], $values);
- }
- /**
- * @depends testSetGetMultiple
- * @depends testSetExpire
- * @slow
- */
- function testSetMultipleExpireDateIntervalNotExpired() {
- $values = [
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- ];
- $cache = $this->getCache();
- $cache->setMultiple($values, new \DateInterval('PT5S'));
- $result = $cache->getMultiple(array_keys($values));
- $count = 0;
- foreach ($result as $key => $value) {
- $count++;
- $this->assertTrue(isset($values[$key]));
- $this->assertEquals($values[$key], $value);
- unset($values[$key]);
- }
- $this->assertEquals(3, $count);
- // The list of values should now be empty
- $this->assertEquals([], $values);
- }
- /**
- * @slow
- */
- function testSetMultipleExpireDateIntervalExpired() {
- $values = [
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- ];
- $cache = $this->getCache();
- $cache->setMultiple($values, new \DateInterval('PT1S'));
- // Wait 2 seconds so the cache expires
- sleep(2);
- $result = $cache->getMultiple(array_keys($values), 'not-found');
- $count = 0;
- $expected = [
- 'key1' => 'not-found',
- 'key2' => 'not-found',
- 'key3' => 'not-found',
- ];
- foreach ($result as $key => $value) {
- $count++;
- $this->assertTrue(isset($expected[$key]));
- $this->assertEquals($expected[$key], $value);
- unset($expected[$key]);
- }
- $this->assertEquals(3, $count);
- // The list of values should now be empty
- $this->assertEquals([], $expected);
- }
- /**
- * @slow
- */
- function testSetMultipleExpireDateIntervalInt() {
- $values = [
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- ];
- $cache = $this->getCache();
- $cache->setMultiple($values, 1);
- // Wait 2 seconds so the cache expires
- sleep(2);
- $result = $cache->getMultiple(array_keys($values), 'not-found');
- $count = 0;
- $expected = [
- 'key1' => 'not-found',
- 'key2' => 'not-found',
- 'key3' => 'not-found',
- ];
- foreach ($result as $key => $value) {
- $count++;
- $this->assertTrue(isset($expected[$key]));
- $this->assertEquals($expected[$key], $value);
- unset($expected[$key]);
- }
- $this->assertEquals(3, $count);
- // The list of values should now be empty
- $this->assertEquals([], $expected);
- }
- /**
- * @expectedException \Psr\SimpleCache\InvalidArgumentException
- */
- function testSetMultipleInvalidArg() {
- $cache = $this->getCache();
- $cache->setMultiple(null);
- }
- /**
- * @expectedException \Psr\SimpleCache\InvalidArgumentException
- */
- function testGetMultipleInvalidArg() {
- $cache = $this->getCache();
- $result = $cache->getMultiple(null);
- // If $result was a generator, the generator will only error once the
- // first value is requested.
- //
- // This extra line is just a precaution for that
- if ($result instanceof \Traversable) $result->current();
- }
- /**
- * @depends testSetGetMultiple
- */
- function testDeleteMultipleDefaultGet() {
- $values = [
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- ];
- $cache = $this->getCache();
- $cache->setMultiple($values);
- $cache->deleteMultiple(['key1', 'key3']);
- $result = $cache->getMultiple(array_keys($values), 'tea');
- $expected = [
- 'key1' => 'tea',
- 'key2' => 'value2',
- 'key3' => 'tea',
- ];
- foreach ($result as $key => $value) {
- $this->assertTrue(isset($expected[$key]));
- $this->assertEquals($expected[$key], $value);
- unset($expected[$key]);
- }
- // The list of values should now be empty
- $this->assertEquals([], $expected);
- }
- /**
- * @depends testSetGetMultiple
- */
- function testDeleteMultipleGenerator() {
- $values = [
- 'key1' => 'value1',
- 'key2' => 'value2',
- 'key3' => 'value3',
- ];
- $cache = $this->getCache();
- $cache->setMultiple($values);
- $gen = function() {
- yield 'key1';
- yield 'key3';
- };
- $cache->deleteMultiple($gen());
- $result = $cache->getMultiple(array_keys($values), 'tea');
- $expected = [
- 'key1' => 'tea',
- 'key2' => 'value2',
- 'key3' => 'tea',
- ];
- foreach ($result as $key => $value) {
- $this->assertTrue(isset($expected[$key]));
- $this->assertEquals($expected[$key], $value);
- unset($expected[$key]);
- }
- // The list of values should now be empty
- $this->assertEquals([], $expected);
- }
- /**
- * @expectedException \Psr\SimpleCache\InvalidArgumentException
- */
- function testDeleteMultipleInvalidArg() {
- $cache = $this->getCache();
- $cache->deleteMultiple(null);
- }
- }
|