pax_global_header00006660000000000000000000000064130446411360014514gustar00rootroot0000000000000052 comment=a8d9538a44ed5a70d551f9b87f534c98dfe6b0ee integration-tests-0.16.0/000077500000000000000000000000001304464113600152635ustar00rootroot00000000000000integration-tests-0.16.0/.gitignore000077500000000000000000000000251304464113600172530ustar00rootroot00000000000000composer.lock vendor integration-tests-0.16.0/.travis.yml000066400000000000000000000013071304464113600173750ustar00rootroot00000000000000language: php sudo: false php: - 7.0 env: - SUITE=PHPCache - SUITE=Symfony - SUITE=Stash - SUITE=madewithlove matrix: fast_finish: true allow_failures: - env: SUITE=Stash - env: SUITE=madewithlove services: - redis - memcached cache: directories: - "$HOME/.composer/cache" before_install: - echo "Disable xdebug" && phpenv config-rm xdebug.ini - travis_retry composer self-update install: - echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - composer update --prefer-source script: - phpunit --testsuite $SUITE integration-tests-0.16.0/LICENSE000077500000000000000000000020711304464113600162730ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Aaron Scherer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. integration-tests-0.16.0/README.md000066400000000000000000000034411304464113600165440ustar00rootroot00000000000000# PSR-6 and PSR-16 Integration tests [![Gitter](https://badges.gitter.im/php-cache/cache.svg)](https://gitter.im/php-cache/cache?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) [![Latest Stable Version](https://poser.pugx.org/cache/integration-tests/v/stable)](https://packagist.org/packages/cache/integration-tests) [![Total Downloads](https://poser.pugx.org/cache/integration-tests/downloads)](https://packagist.org/packages/cache/integration-tests) [![Monthly Downloads](https://poser.pugx.org/cache/integration-tests/d/monthly.png)](https://packagist.org/packages/cache/integration-tests) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) This repository contains integration tests to make sure your implementation of a PSR-6 and/or PSR-16 cache follows the rules by PHP-FIG. It is a part of the PHP Cache organisation. To read about us please read the shared documentation at [www.php-cache.com](http://www.php-cache.com). ### Install ```bash composer require --dev cache/integration-tests:dev-master ``` ### Use Create a test that looks like this: ```php class PoolIntegrationTest extends CachePoolTest { public function createCachePool() { return new CachePool(); } } ``` You could also test your tag implementation: ```php class TagIntegrationTest extends TaggableCachePoolTest { public function createCachePool() { return new CachePool(); } } ``` You can also test a PSR-16 implementation: ```php class CacheIntegrationTest extends SimpleCacheTest { public function createSimpleCache() { return new SimpleCache(); } } ``` ### Contribute Contributions are very welcome! Send a pull request or report any issues you find on the [issue tracker](http://issues.php-cache.com). integration-tests-0.16.0/composer.json000077500000000000000000000023231304464113600200100ustar00rootroot00000000000000{ "name": "cache/integration-tests", "type": "library", "description": "Integration tests for PSR-6 and PSR-16 cache implementations", "keywords": [ "cache", "psr6", "psr16", "test" ], "homepage": "https://github.com/php-cache/integration-tests", "license": "MIT", "minimum-stability": "dev", "authors": [ { "name": "Aaron Scherer", "email": "aequasi@gmail.com", "homepage": "https://github.com/aequasi" }, { "name": "Tobias Nyholm", "email": "tobias.nyholm@gmail.com", "homepage": "https://github.com/nyholm" } ], "require": { "php": "^5.4|^7", "psr/cache": "~1.0", "cache/tag-interop": "^1.0" }, "require-dev": { "phpunit/phpunit": "^4.0|^5.0", "cache/cache": "dev-master", "symfony/cache": "^3.1", "madewithlove/illuminate-psr-cache-bridge": "^1.0", "illuminate/cache": "^5.0", "tedivm/stash": "dev-master" }, "autoload": { "psr-4": { "Cache\\IntegrationTests\\": "src/" } } } integration-tests-0.16.0/phpunit.xml.dist000066400000000000000000000022161304464113600204370ustar00rootroot00000000000000 ./vendor/cache/cache/src/Adapter/Redis/Tests/ ./vendor/cache/cache/src/Bridge/SimpleCache/Tests/ ./vendor/symfony/cache/Tests/Adapter/FilesystemAdapterTest.php ./tests/StashTest.php ./vendor/madewithlove/illuminate-psr-cache-bridge/tests/integration/IntegrationTest.php ./ ./Tests ./vendor integration-tests-0.16.0/src/000077500000000000000000000000001304464113600160525ustar00rootroot00000000000000integration-tests-0.16.0/src/CachePoolTest.php000066400000000000000000000663211304464113600212700ustar00rootroot00000000000000, Tobias Nyholm * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace Cache\IntegrationTests; use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; abstract class CachePoolTest extends \PHPUnit_Framework_TestCase { /** * @type array with functionName => reason. */ protected $skippedTests = []; /** * @type CacheItemPoolInterface */ private $cache; /** * @return CacheItemPoolInterface that is used in the tests */ abstract public function createCachePool(); protected function setUp() { $this->cache = $this->createCachePool(); } protected function tearDown() { if ($this->cache !== null) { $this->cache->clear(); } } /** * Data provider for invalid keys. * * @return array */ public static function invalidKeys() { return [ [true], [false], [null], [2], [2.5], ['{str'], ['rand{'], ['rand{str'], ['rand}str'], ['rand(str'], ['rand)str'], ['rand/str'], ['rand\\str'], ['rand@str'], ['rand:str'], [new \stdClass()], [['array']], ]; } public function testBasicUsage() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('4711'); $this->cache->save($item); $item = $this->cache->getItem('key2'); $item->set('4712'); $this->cache->save($item); $fooItem = $this->cache->getItem('key'); $this->assertTrue($fooItem->isHit()); $this->assertEquals('4711', $fooItem->get()); $barItem = $this->cache->getItem('key2'); $this->assertTrue($barItem->isHit()); $this->assertEquals('4712', $barItem->get()); // Remove 'key' and make sure 'key2' is still there $this->cache->deleteItem('key'); $this->assertFalse($this->cache->getItem('key')->isHit()); $this->assertTrue($this->cache->getItem('key2')->isHit()); // Remove everything $this->cache->clear(); $this->assertFalse($this->cache->getItem('key')->isHit()); $this->assertFalse($this->cache->getItem('key2')->isHit()); } public function testItemModifiersReturnsStatic() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $this->assertSame($item, $item->set('4711')); $this->assertSame($item, $item->expiresAfter(2)); $this->assertSame($item, $item->expiresAt(new \DateTime('+2hours'))); } public function testGetItem() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->save($item); // get existing item $item = $this->cache->getItem('key'); $this->assertEquals('value', $item->get(), 'A stored item must be returned from cached.'); $this->assertEquals('key', $item->getKey(), 'Cache key can not change.'); // get non-existent item $item = $this->cache->getItem('key2'); $this->assertFalse($item->isHit()); $this->assertNull($item->get(), "Item's value must be null when isHit is false."); } public function testGetItems() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $keys = ['foo', 'bar', 'baz']; $items = $this->cache->getItems($keys); $count = 0; /** @type CacheItemInterface $item */ foreach ($items as $i => $item) { $item->set($i); $this->cache->save($item); $count++; } $this->assertSame(3, $count); $keys[] = 'biz'; /** @type CacheItemInterface[] $items */ $items = $this->cache->getItems($keys); $count = 0; foreach ($items as $key => $item) { $itemKey = $item->getKey(); $this->assertEquals($itemKey, $key, 'Keys must be preserved when fetching multiple items'); $this->assertEquals($key !== 'biz', $item->isHit()); $this->assertTrue(in_array($key, $keys), 'Cache key can not change.'); // Remove $key for $keys foreach ($keys as $k => $v) { if ($v === $key) { unset($keys[$k]); } } $count++; } $this->assertSame(4, $count); } public function testGetItemsEmpty() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $items = $this->cache->getItems([]); $this->assertTrue( is_array($items) || $items instanceof \Traversable, 'A call to getItems with an empty array must always return an array or \Traversable.' ); $count = 0; foreach ($items as $item) { $count++; } $this->assertSame(0, $count); } public function testHasItem() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->save($item); // has existing item $this->assertTrue($this->cache->hasItem('key')); // has non-existent item $this->assertFalse($this->cache->hasItem('key2')); } public function testClear() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->save($item); $return = $this->cache->clear(); $this->assertTrue($return, 'clear() must return true if cache was cleared. '); $this->assertFalse($this->cache->getItem('key')->isHit(), 'No item should be a hit after the cache is cleared. '); $this->assertFalse($this->cache->hasItem('key2'), 'The cache pool should be empty after it is cleared.'); } public function testClearWithDeferredItems() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->saveDeferred($item); $this->cache->clear(); $this->cache->commit(); $this->assertFalse($this->cache->getItem('key')->isHit(), 'Deferred items must be cleared on clear(). '); } public function testDeleteItem() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->save($item); $this->assertTrue($this->cache->deleteItem('key')); $this->assertFalse($this->cache->getItem('key')->isHit(), 'A deleted item should not be a hit.'); $this->assertFalse($this->cache->hasItem('key'), 'A deleted item should not be a in cache.'); $this->assertTrue($this->cache->deleteItem('key2'), 'Deleting an item that does not exist should return true.'); } public function testDeleteItems() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $items = $this->cache->getItems(['foo', 'bar', 'baz']); /** @type CacheItemInterface $item */ foreach ($items as $idx => $item) { $item->set($idx); $this->cache->save($item); } // All should be a hit but 'biz' $this->assertTrue($this->cache->getItem('foo')->isHit()); $this->assertTrue($this->cache->getItem('bar')->isHit()); $this->assertTrue($this->cache->getItem('baz')->isHit()); $this->assertFalse($this->cache->getItem('biz')->isHit()); $return = $this->cache->deleteItems(['foo', 'bar', 'biz']); $this->assertTrue($return); $this->assertFalse($this->cache->getItem('foo')->isHit()); $this->assertFalse($this->cache->getItem('bar')->isHit()); $this->assertTrue($this->cache->getItem('baz')->isHit()); $this->assertFalse($this->cache->getItem('biz')->isHit()); } public function testSave() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $return = $this->cache->save($item); $this->assertTrue($return, 'save() should return true when items are saved.'); $this->assertEquals('value', $this->cache->getItem('key')->get()); } public function testSaveExpired() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $item->expiresAt(\DateTime::createFromFormat('U', time() + 10)); $this->cache->save($item); $item->expiresAt(\DateTime::createFromFormat('U', time() - 1)); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertFalse($item->isHit(), 'Cache should not save expired items'); } public function testSaveWithoutExpire() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('test_ttl_null'); $item->set('data'); $this->cache->save($item); // Use a new pool instance to ensure that we don't hit any caches $pool = $this->createCachePool(); $item = $pool->getItem('test_ttl_null'); $this->assertTrue($item->isHit(), 'Cache should have retrieved the items'); $this->assertEquals('data', $item->get()); } public function testDeferredSave() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('4711'); $return = $this->cache->saveDeferred($item); $this->assertTrue($return, 'save() should return true when items are saved.'); $item = $this->cache->getItem('key2'); $item->set('4712'); $this->cache->saveDeferred($item); // They are not saved yet but should be a hit $this->assertTrue($this->cache->hasItem('key'), 'Deferred items should be considered as a part of the cache even before they are committed'); $this->assertTrue($this->cache->getItem('key')->isHit(), 'Deferred items should be a hit even before they are committed'); $this->assertTrue($this->cache->getItem('key2')->isHit()); $this->cache->commit(); // They should be a hit after the commit as well $this->assertTrue($this->cache->getItem('key')->isHit()); $this->assertTrue($this->cache->getItem('key2')->isHit()); } public function testDeferredExpired() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('4711'); $item->expiresAt(\DateTime::createFromFormat('U', time() - 1)); $this->cache->saveDeferred($item); $this->assertFalse($this->cache->hasItem('key'), 'Cache should not have expired deferred item'); $this->cache->commit(); $item = $this->cache->getItem('key'); $this->assertFalse($item->isHit(), 'Cache should not save expired items'); } public function testDeleteDeferredItem() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('4711'); $this->cache->saveDeferred($item); $this->assertTrue($this->cache->getItem('key')->isHit()); $this->cache->deleteItem('key'); $this->assertFalse($this->cache->hasItem('key'), 'You must be able to delete a deferred item before committed. '); $this->assertFalse($this->cache->getItem('key')->isHit(), 'You must be able to delete a deferred item before committed. '); $this->cache->commit(); $this->assertFalse($this->cache->hasItem('key'), 'A deleted item should not reappear after commit. '); $this->assertFalse($this->cache->getItem('key')->isHit(), 'A deleted item should not reappear after commit. '); } public function testDeferredSaveWithoutCommit() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $this->prepareDeferredSaveWithoutCommit(); gc_collect_cycles(); $cache = $this->createCachePool(); $this->assertTrue($cache->getItem('key')->isHit(), 'A deferred item should automatically be committed on CachePool::__destruct().'); } private function prepareDeferredSaveWithoutCommit() { $cache = $this->cache; $this->cache = null; $item = $cache->getItem('key'); $item->set('4711'); $cache->saveDeferred($item); } public function testCommit() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->saveDeferred($item); $return = $this->cache->commit(); $this->assertTrue($return, 'commit() should return true on successful commit. '); $this->assertEquals('value', $this->cache->getItem('key')->get()); $return = $this->cache->commit(); $this->assertTrue($return, 'commit() should return true even if no items were deferred. '); } public function testExpiration() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $item->expiresAfter(2); $this->cache->save($item); sleep(3); $item = $this->cache->getItem('key'); $this->assertFalse($item->isHit()); $this->assertNull($item->get(), "Item's value must be null when isHit() is false."); } public function testExpiresAt() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $item->expiresAt(new \DateTime('+2hours')); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue($item->isHit()); } public function testExpiresAtWithNull() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $item->expiresAt(null); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue($item->isHit()); } public function testExpiresAfterWithNull() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $item->expiresAfter(null); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue($item->isHit()); } public function testKeyLength() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $key = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.'; $item = $this->cache->getItem($key); $item->set('value'); $this->assertTrue($this->cache->save($item), 'The implementation does not support a valid cache key'); $this->assertTrue($this->cache->hasItem($key)); } /** * @expectedException \Psr\Cache\InvalidArgumentException * @dataProvider invalidKeys */ public function testGetItemInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $this->cache->getItem($key); } /** * @expectedException \Psr\Cache\InvalidArgumentException * @dataProvider invalidKeys */ public function testGetItemsInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $this->cache->getItems(['key1', $key, 'key2']); } /** * @expectedException \Psr\Cache\InvalidArgumentException * @dataProvider invalidKeys */ public function testHasItemInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $this->cache->hasItem($key); } /** * @expectedException \Psr\Cache\InvalidArgumentException * @dataProvider invalidKeys */ public function testDeleteItemInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $this->cache->deleteItem($key); } /** * @expectedException \Psr\Cache\InvalidArgumentException * @dataProvider invalidKeys */ public function testDeleteItemsInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $this->cache->deleteItems(['key1', $key, 'key2']); } public function testDataTypeString() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('5'); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue('5' === $item->get(), 'Wrong data type. If we store a string we must get an string back.'); $this->assertTrue(is_string($item->get()), 'Wrong data type. If we store a string we must get an string back.'); } public function testDataTypeInteger() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set(5); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue(5 === $item->get(), 'Wrong data type. If we store an int we must get an int back.'); $this->assertTrue(is_int($item->get()), 'Wrong data type. If we store an int we must get an int back.'); } public function testDataTypeNull() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set(null); $this->cache->save($item); $this->assertTrue($this->cache->hasItem('key'), 'Null is a perfectly fine cache value. hasItem() should return true when null are stored. '); $item = $this->cache->getItem('key'); $this->assertTrue(null === $item->get(), 'Wrong data type. If we store null we must get an null back.'); $this->assertTrue(is_null($item->get()), 'Wrong data type. If we store null we must get an null back.'); $this->assertTrue($item->isHit(), 'isHit() should return true when null are stored. '); } public function testDataTypeFloat() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $float = 1.23456789; $item = $this->cache->getItem('key'); $item->set($float); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue(is_float($item->get()), 'Wrong data type. If we store float we must get an float back.'); $this->assertEquals($float, $item->get()); $this->assertTrue($item->isHit(), 'isHit() should return true when float are stored. '); } public function testDataTypeBoolean() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set(true); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue(is_bool($item->get()), 'Wrong data type. If we store boolean we must get an boolean back.'); $this->assertTrue($item->get()); $this->assertTrue($item->isHit(), 'isHit() should return true when true are stored. '); } public function testDataTypeArray() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $array = ['a' => 'foo', 2 => 'bar']; $item = $this->cache->getItem('key'); $item->set($array); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue(is_array($item->get()), 'Wrong data type. If we store array we must get an array back.'); $this->assertEquals($array, $item->get()); $this->assertTrue($item->isHit(), 'isHit() should return true when array are stored. '); } public function testDataTypeObject() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $object = new \stdClass(); $object->a = 'foo'; $item = $this->cache->getItem('key'); $item->set($object); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue(is_object($item->get()), 'Wrong data type. If we store object we must get an object back.'); $this->assertEquals($object, $item->get()); $this->assertTrue($item->isHit(), 'isHit() should return true when object are stored. '); } public function testIsHit() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertTrue($item->isHit()); } public function testIsHitDeferred() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->saveDeferred($item); // Test accessing the value before it is committed $item = $this->cache->getItem('key'); $this->assertTrue($item->isHit()); $this->cache->commit(); $item = $this->cache->getItem('key'); $this->assertTrue($item->isHit()); } public function testSaveDeferredWhenChangingValues() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->saveDeferred($item); $item = $this->cache->getItem('key'); $item->set('new value'); $item = $this->cache->getItem('key'); $this->assertEquals('value', $item->get(), 'Items that is put in the deferred queue should not get their values changed'); $this->cache->commit(); $item = $this->cache->getItem('key'); $this->assertEquals('value', $item->get(), 'Items that is put in the deferred queue should not get their values changed'); } public function testSaveDeferredOverwrite() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $this->cache->saveDeferred($item); $item = $this->cache->getItem('key'); $item->set('new value'); $this->cache->saveDeferred($item); $item = $this->cache->getItem('key'); $this->assertEquals('new value', $item->get()); $this->cache->commit(); $item = $this->cache->getItem('key'); $this->assertEquals('new value', $item->get()); } public function testSavingObject() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set(new \DateTime()); $this->cache->save($item); $item = $this->cache->getItem('key'); $value = $item->get(); $this->assertInstanceOf('DateTime', $value, 'You must be able to store objects in cache.'); } public function testHasItemReturnsFalseWhenDeferredItemIsExpired() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key'); $item->set('value'); $item->expiresAfter(2); $this->cache->saveDeferred($item); sleep(3); $this->assertFalse($this->cache->hasItem('key')); } } integration-tests-0.16.0/src/HierarchicalCachePoolTest.php000066400000000000000000000112741304464113600235640ustar00rootroot00000000000000, Tobias Nyholm * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace Cache\IntegrationTests; use Psr\Cache\CacheItemPoolInterface; /** * @author Tobias Nyholm */ abstract class HierarchicalCachePoolTest extends \PHPUnit_Framework_TestCase { /** * @type array with functionName => reason. */ protected $skippedTests = []; /** * @type CacheItemPoolInterface */ private $cache; /** * @return CacheItemPoolInterface that is used in the tests */ abstract public function createCachePool(); protected function setUp() { $this->cache = $this->createCachePool(); } protected function tearDown() { if ($this->cache !== null) { $this->cache->clear(); } } public function testBasicUsage() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $user = 4711; for ($i = 0; $i < 10; $i++) { $item = $this->cache->getItem(sprintf('|users|%d|followers|%d|likes', $user, $i)); $item->set('Justin Bieber'); $this->cache->save($item); } $this->assertTrue($this->cache->hasItem('|users|4711|followers|4|likes')); $this->cache->deleteItem('|users|4711|followers'); $this->assertFalse($this->cache->hasItem('|users|4711|followers|4|likes')); } public function testChain() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('|aaa|bbb|ccc|ddd'); $item->set('value'); $this->cache->save($item); $item = $this->cache->getItem('|aaa|bbb|ccc|xxx'); $item->set('value'); $this->cache->save($item); $item = $this->cache->getItem('|aaa|bbb|zzz|ddd'); $item->set('value'); $this->cache->save($item); $this->assertTrue($this->cache->hasItem('|aaa|bbb|ccc|ddd')); $this->assertTrue($this->cache->hasItem('|aaa|bbb|ccc|xxx')); $this->assertTrue($this->cache->hasItem('|aaa|bbb|zzz|ddd')); $this->assertFalse($this->cache->hasItem('|aaa|bbb|ccc')); $this->assertFalse($this->cache->hasItem('|aaa|bbb|zzz')); $this->assertFalse($this->cache->hasItem('|aaa|bbb')); $this->assertFalse($this->cache->hasItem('|aaa')); $this->assertFalse($this->cache->hasItem('|')); // This is a different thing $this->cache->deleteItem('|aaa|bbb|cc'); $this->assertTrue($this->cache->hasItem('|aaa|bbb|ccc|ddd')); $this->assertTrue($this->cache->hasItem('|aaa|bbb|ccc|xxx')); $this->assertTrue($this->cache->hasItem('|aaa|bbb|zzz|ddd')); $this->cache->deleteItem('|aaa|bbb|ccc'); $this->assertFalse($this->cache->hasItem('|aaa|bbb|ccc|ddd')); $this->assertFalse($this->cache->hasItem('|aaa|bbb|ccc|xxx')); $this->assertTrue($this->cache->hasItem('|aaa|bbb|zzz|ddd')); $this->cache->deleteItem('|aaa'); $this->assertFalse($this->cache->hasItem('|aaa|bbb|zzz|ddd')); } public function testRemoval() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('foo'); $item->set('value'); $this->cache->save($item); $item = $this->cache->getItem('|aaa|bbb'); $item->set('value'); $this->cache->save($item); $this->cache->deleteItem('|'); $this->assertFalse($this->cache->hasItem('|aaa|bbb'), 'Hierarchy items should be removed when deleting root'); $this->assertTrue($this->cache->hasItem('foo'), 'All cache should not be cleared when deleting root'); } public function testRemovalWhenDeferred() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('|aaa|bbb'); $item->set('value'); $this->cache->saveDeferred($item); $this->cache->deleteItem('|'); $this->assertFalse($this->cache->hasItem('|aaa|bbb'), 'Deferred hierarchy items should be removed'); $this->cache->commit(); $this->assertFalse($this->cache->hasItem('|aaa|bbb'), 'Deferred hierarchy items should be removed'); } } integration-tests-0.16.0/src/SimpleCacheTest.php000066400000000000000000000535361304464113600216140ustar00rootroot00000000000000, Tobias Nyholm * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace Cache\IntegrationTests; use Psr\SimpleCache\CacheInterface; abstract class SimpleCacheTest extends \PHPUnit_Framework_TestCase { /** * @type array with functionName => reason. */ protected $skippedTests = []; /** * @type CacheInterface */ private $cache; /** * @return CacheInterface that is used in the tests */ abstract public function createSimpleCache(); protected function setUp() { $this->cache = $this->createSimpleCache(); } protected function tearDown() { if ($this->cache !== null) { $this->cache->clear(); } } /** * Data provider for invalid keys. * * @return array */ public static function invalidKeys() { return [ [''], [true], [false], [null], [2], [2.5], ['{str'], ['rand{'], ['rand{str'], ['rand}str'], ['rand(str'], ['rand)str'], ['rand/str'], ['rand\\str'], ['rand@str'], ['rand:str'], [new \stdClass()], [['array']], ]; } /** * @return array */ public static function invalidTtl() { return [ [''], [true], [false], ['abc'], [2.5], [' 1'], // can be casted to a int ['12foo'], // can be casted to a int ['025'], // can be interpreted as hex [new \stdClass()], [['array']], ]; } /** * Data provider for valid keys. * * @return array */ public static function validKeys() { return [ ['AbC19_.'], ['1234567890123456789012345678901234567890123456789012345678901234'], ]; } /** * Data provider for valid data to store. * * @return array */ public static function validData() { return [ ['AbC19_.'], [4711], [47.11], [true], [null], [['key' => 'value']], [new \stdClass()], ]; } public function testSet() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $result = $this->cache->set('key', 'value'); $this->assertTrue($result, 'set() must return true if success'); $this->assertEquals('value', $this->cache->get('key')); } public function testSetTtl() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $result = $this->cache->set('key1', 'value', 1); $this->assertTrue($result, 'set() must return true if success'); $this->assertEquals('value', $this->cache->get('key1')); sleep(2); $this->assertNull($this->cache->get('key1'), 'Value must expire after ttl.'); $this->cache->set('key2', 'value', new \DateInterval('PT1S')); $this->assertEquals('value', $this->cache->get('key2')); sleep(2); $this->assertNull($this->cache->get('key2'), 'Value must expire after ttl.'); } public function testSetExpiredTtl() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->set('key0', 'value'); $this->cache->set('key0', 'value', 0); $this->assertNull($this->cache->get('key0')); $this->assertFalse($this->cache->has('key0')); $this->cache->set('key1', 'value', -1); $this->assertNull($this->cache->get('key1')); $this->assertFalse($this->cache->has('key1')); } public function testGet() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->assertNull($this->cache->get('key')); $this->assertEquals('foo', $this->cache->get('key', 'foo')); $this->cache->set('key', 'value'); $this->assertEquals('value', $this->cache->get('key', 'foo')); } public function testDelete() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->assertTrue($this->cache->delete('key'), 'Deleting a value that does not exist should return true'); $this->cache->set('key', 'value'); $this->assertTrue($this->cache->delete('key'), 'Delete must return true on success'); $this->assertNull($this->cache->get('key'), 'Values must be deleted on delete()'); } public function testClear() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->assertTrue($this->cache->clear(), 'Clearing an empty cache should return true'); $this->cache->set('key', 'value'); $this->assertTrue($this->cache->clear(), 'Delete must return true on success'); $this->assertNull($this->cache->get('key'), 'Values must be deleted on clear()'); } public function testSetMultiple() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $result = $this->cache->setMultiple(['key0' => 'value0', 'key1' => 'value1']); $this->assertTrue($result, 'setMultiple() must return true if success'); $this->assertEquals('value0', $this->cache->get('key0')); $this->assertEquals('value1', $this->cache->get('key1')); $result = $this->cache->setMultiple(['0' => 'value0']); $this->assertTrue($result, 'setMultiple() must return true if success'); $this->assertEquals('value0', $this->cache->get('0')); } public function testSetMultipleTtl() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->setMultiple(['key2' => 'value2', 'key3' => 'value3'], 1); $this->assertEquals('value2', $this->cache->get('key2')); $this->assertEquals('value3', $this->cache->get('key3')); sleep(2); $this->assertNull($this->cache->get('key2'), 'Value must expire after ttl.'); $this->assertNull($this->cache->get('key3'), 'Value must expire after ttl.'); $this->cache->setMultiple(['key4' => 'value4'], new \DateInterval('PT1S')); $this->assertEquals('value4', $this->cache->get('key4')); sleep(2); $this->assertNull($this->cache->get('key4'), 'Value must expire after ttl.'); } public function testSetMultipleExpiredTtl() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->setMultiple(['key0' => 'value0', 'key1' => 'value1'], 0); $this->assertNull($this->cache->get('key0')); $this->assertNull($this->cache->get('key1')); } public function testSetMultipleWithGenerator() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $gen = function () { yield 'key0' => 'value0'; yield 'key1' => 'value1'; }; $this->cache->setMultiple($gen()); $this->assertEquals('value0', $this->cache->get('key0')); $this->assertEquals('value1', $this->cache->get('key1')); } public function testGetMultiple() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $result = $this->cache->getMultiple(['key0', 'key1']); $keys = []; foreach ($result as $i => $r) { $keys[] = $i; $this->assertNull($r); } sort($keys); $this->assertSame(['key0', 'key1'], $keys); $this->cache->set('key3', 'value'); $result = $this->cache->getMultiple(['key2', 'key3', 'key4'], 'foo'); $keys = []; foreach ($result as $key => $r) { $keys[] = $key; if ($key === 'key3') { $this->assertEquals('value', $r); } else { $this->assertEquals('foo', $r); } } sort($keys); $this->assertSame(['key2', 'key3', 'key4'], $keys); } public function testGetMultipleWithGenerator() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $gen = function () { yield 1 => 'key0'; yield 1 => 'key1'; }; $this->cache->set('key0', 'value0'); $result = $this->cache->getMultiple($gen()); $keys = []; foreach ($result as $key => $r) { $keys[] = $key; if ($key === 'key0') { $this->assertEquals('value0', $r); } elseif ($key === 'key1') { $this->assertNull($r); } else { $this->assertFalse(true, 'This should not happend'); } } sort($keys); $this->assertSame(['key0', 'key1'], $keys); $this->assertEquals('value0', $this->cache->get('key0')); $this->assertNull($this->cache->get('key1')); } public function testDeleteMultiple() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->assertTrue($this->cache->deleteMultiple([]), 'Deleting a empty array should return true'); $this->assertTrue($this->cache->deleteMultiple(['key']), 'Deleting a value that does not exist should return true'); $this->cache->set('key0', 'value0'); $this->cache->set('key1', 'value1'); $this->assertTrue($this->cache->deleteMultiple(['key0', 'key1']), 'Delete must return true on success'); $this->assertNull($this->cache->get('key0'), 'Values must be deleted on deleteMultiple()'); $this->assertNull($this->cache->get('key1'), 'Values must be deleted on deleteMultiple()'); } public function testDeleteMultipleGenerator() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $gen = function () { yield 1 => 'key0'; yield 1 => 'key1'; }; $this->cache->set('key0', 'value0'); $this->assertTrue($this->cache->deleteMultiple($gen()), 'Deleting a generator should return true'); $this->assertNull($this->cache->get('key0'), 'Values must be deleted on deleteMultiple()'); $this->assertNull($this->cache->get('key1'), 'Values must be deleted on deleteMultiple()'); } public function testHas() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->assertFalse($this->cache->has('key0')); $this->cache->set('key0', 'value0'); $this->assertTrue($this->cache->has('key0')); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException * @dataProvider invalidKeys */ public function testGetInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->get($key); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException * @dataProvider invalidKeys */ public function testGetMultipleInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $result = $this->cache->getMultiple(['key1', $key, 'key2']); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException */ public function testGetMultipleNoIterable() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $result = $this->cache->getMultiple('key'); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException * @dataProvider invalidKeys */ public function testSetInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->set($key, 'foobar'); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException * @dataProvider invalidKeys */ public function testSetMultipleInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } if (is_int($key)) { $this->markTestSkipped('As keys, strings are always casted to ints so they should be accepted'); } $values = function () use ($key) { yield 'key1' => 'foo'; yield $key => 'bar'; yield 'key2' => 'baz'; }; $this->cache->setMultiple($values()); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException */ public function testSetMultipleNoIterable() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->setMultiple('key'); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException * @dataProvider invalidKeys */ public function testHasInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->has($key); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException * @dataProvider invalidKeys */ public function testDeleteInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->delete($key); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException * @dataProvider invalidKeys */ public function testDeleteMultipleInvalidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->deleteMultiple(['key1', $key, 'key2']); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException */ public function testDeleteMultipleNoIterable() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->deleteMultiple('key'); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException * @dataProvider invalidTtl */ public function testSetInvalidTtl($ttl) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->set('key', 'value', $ttl); } /** * @expectedException \Psr\SimpleCache\InvalidArgumentException * @dataProvider invalidTtl */ public function testSetMultipleInvalidTtl($ttl) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->setMultiple(['key' => 'value'], $ttl); } public function testNullOverwrite() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->set('key', 5); $this->cache->set('key', null); $this->assertNull($this->cache->get('key'), 'Setting null to a key must overwrite previous value'); } public function testDataTypeString() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->set('key', '5'); $result = $this->cache->get('key'); $this->assertTrue('5' === $result, 'Wrong data type. If we store a string we must get an string back.'); $this->assertTrue(is_string($result), 'Wrong data type. If we store a string we must get an string back.'); } public function testDataTypeInteger() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->set('key', 5); $result = $this->cache->get('key'); $this->assertTrue(5 === $result, 'Wrong data type. If we store an int we must get an int back.'); $this->assertTrue(is_int($result), 'Wrong data type. If we store an int we must get an int back.'); } public function testDataTypeFloat() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $float = 1.23456789; $this->cache->set('key', $float); $result = $this->cache->get('key'); $this->assertTrue(is_float($result), 'Wrong data type. If we store float we must get an float back.'); $this->assertEquals($float, $result); } public function testDataTypeBoolean() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->set('key', false); $result = $this->cache->get('key'); $this->assertTrue(is_bool($result), 'Wrong data type. If we store boolean we must get an boolean back.'); $this->assertFalse($result); $this->assertTrue($this->cache->has('key'), 'has() should return true when true are stored. '); } public function testDataTypeArray() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $array = ['a' => 'foo', 2 => 'bar']; $this->cache->set('key', $array); $result = $this->cache->get('key'); $this->assertTrue(is_array($result), 'Wrong data type. If we store array we must get an array back.'); $this->assertEquals($array, $result); } public function testDataTypeObject() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $object = new \stdClass(); $object->a = 'foo'; $this->cache->set('key', $object); $result = $this->cache->get('key'); $this->assertTrue(is_object($result), 'Wrong data type. If we store object we must get an object back.'); $this->assertEquals($object, $result); } /** * @dataProvider validKeys */ public function testSetValidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->set($key, 'foobar'); $this->assertEquals('foobar', $this->cache->get($key)); } /** * @dataProvider validKeys */ public function testSetMultipleValidKeys($key) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->setMultiple([$key => 'foobar']); $result = $this->cache->getMultiple([$key]); $keys = []; foreach ($result as $i => $r) { $keys[] = $i; $this->assertEquals($key, $i); $this->assertEquals('foobar', $r); } $this->assertSame([$key], $keys); } /** * @dataProvider validData */ public function testSetValidData($data) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->set('key', $data); $this->assertEquals($data, $this->cache->get('key')); } /** * @dataProvider validData */ public function testSetMultipleValidData($data) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $this->cache->setMultiple(['key' => $data]); $result = $this->cache->getMultiple(['key']); $keys = []; foreach ($result as $i => $r) { $keys[] = $i; $this->assertEquals($data, $r); } $this->assertSame(['key'], $keys); } public function testObjectAsDefaultValue() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $obj = new \stdClass(); $obj->foo = 'value'; $this->assertEquals($obj, $this->cache->get('key', $obj)); } public function testObjectDoesNotChangeInCache() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); } $obj = new \stdClass(); $obj->foo = 'value'; $this->cache->set('key', $obj); $obj->foo = 'changed'; $cacheObject = $this->cache->get('key'); $this->assertEquals('value', $cacheObject->foo, 'Object in cache should not have their values changed.'); } } integration-tests-0.16.0/src/TaggableCachePoolTest.php000066400000000000000000000223701304464113600227130ustar00rootroot00000000000000, Tobias Nyholm * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace Cache\IntegrationTests; use Cache\TagInterop\TaggableCacheItemPoolInterface; /** * @author Tobias Nyholm */ abstract class TaggableCachePoolTest extends \PHPUnit_Framework_TestCase { /** * @type array with functionName => reason. */ protected $skippedTests = []; /** * @type TaggableCacheItemPoolInterface */ private $cache; /** * @return TaggableCacheItemPoolInterface that is used in the tests */ abstract public function createCachePool(); protected function setUp() { $this->cache = $this->createCachePool(); } protected function tearDown() { if ($this->cache !== null) { $this->cache->clear(); } } public function invalidKeys() { return CachePoolTest::invalidKeys(); } public function testMultipleTags() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $this->cache->save($this->cache->getItem('key1')->set('value')->setTags(['tag1', 'tag2'])); $this->cache->save($this->cache->getItem('key2')->set('value')->setTags(['tag1', 'tag3'])); $this->cache->save($this->cache->getItem('key3')->set('value')->setTags(['tag2', 'tag3'])); $this->cache->save($this->cache->getItem('key4')->set('value')->setTags(['tag4', 'tag3'])); $this->cache->invalidateTags(['tag1']); $this->assertFalse($this->cache->hasItem('key1')); $this->assertFalse($this->cache->hasItem('key2')); $this->assertTrue($this->cache->hasItem('key3')); $this->assertTrue($this->cache->hasItem('key4')); $this->cache->invalidateTags(['tag2']); $this->assertFalse($this->cache->hasItem('key1')); $this->assertFalse($this->cache->hasItem('key2')); $this->assertFalse($this->cache->hasItem('key3')); $this->assertTrue($this->cache->hasItem('key4')); } public function testPreviousTag() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key')->set('value'); $tags = $item->getPreviousTags(); $this->assertTrue(is_array($tags)); $this->assertCount(0, $tags); $item->setTags(['tag0']); $this->assertCount(0, $item->getPreviousTags()); $this->cache->save($item); $this->assertCount(0, $item->getPreviousTags()); $item = $this->cache->getItem('key'); $this->assertCount(1, $item->getPreviousTags()); } public function testPreviousTagDeferred() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key')->set('value'); $item->setTags(['tag0']); $this->assertCount(0, $item->getPreviousTags()); $this->cache->saveDeferred($item); $this->assertCount(0, $item->getPreviousTags()); $item = $this->cache->getItem('key'); $this->assertCount(1, $item->getPreviousTags()); } /** * @expectedException \Psr\Cache\InvalidArgumentException */ public function testTagAccessorWithEmptyTag() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key')->set('value'); $item->setTags(['']); $this->cache->save($item); } /** * @expectedException \Psr\Cache\InvalidArgumentException * @dataProvider invalidKeys */ public function testTagAccessorWithInvalidTag($tag) { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key')->set('value'); $item->setTags([$tag]); $this->cache->save($item); } public function testTagAccessorDuplicateTags() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key')->set('value'); $item->setTags(['tag', 'tag', 'tag']); $this->cache->save($item); $item = $this->cache->getItem('key'); $this->assertCount(1, $item->getPreviousTags()); } /** * The tag must be removed whenever we remove an item. If not, when creating a new item * with the same key will get the same tags. */ public function testRemoveTagWhenItemIsRemoved() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key')->set('value'); $item->setTags(['tag1']); // Save the item and then delete it $this->cache->save($item); $this->cache->deleteItem('key'); // Create a new item (same key) (no tags) $item = $this->cache->getItem('key')->set('value'); $this->cache->save($item); // Clear the tag, The new item should not be cleared $this->cache->invalidateTags(['tag1']); $this->assertTrue($this->cache->hasItem('key'), 'Item key should be removed from the tag list when the item is removed'); } public function testClearPool() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key')->set('value'); $item->setTags(['tag1']); $this->cache->save($item); // Clear the pool $this->cache->clear(); // Create a new item (no tags) $item = $this->cache->getItem('key')->set('value'); $this->cache->save($item); $this->cache->invalidateTags(['tag1']); $this->assertTrue($this->cache->hasItem('key'), 'Tags should be removed when the pool was cleared.'); } public function testInvalidateTag() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key')->set('value'); $item->setTags(['tag1', 'tag2']); $this->cache->save($item); $item = $this->cache->getItem('key2')->set('value'); $item->setTags(['tag1']); $this->cache->save($item); $this->cache->invalidateTag('tag2'); $this->assertFalse($this->cache->hasItem('key'), 'Item should be cleared when tag is invalidated'); $this->assertTrue($this->cache->hasItem('key2'), 'Item should be cleared when tag is invalidated'); // Create a new item (no tags) $item = $this->cache->getItem('key')->set('value'); $this->cache->save($item); $this->cache->invalidateTags(['tag2']); $this->assertTrue($this->cache->hasItem('key'), 'Item key list should be removed when clearing the tags'); $this->cache->invalidateTags(['tag1']); $this->assertTrue($this->cache->hasItem('key'), 'Item key list should be removed when clearing the tags'); } public function testInvalidateTags() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $item = $this->cache->getItem('key')->set('value'); $item->setTags(['tag1', 'tag2']); $this->cache->save($item); $item = $this->cache->getItem('key2')->set('value'); $item->setTags(['tag1']); $this->cache->save($item); $this->cache->invalidateTags(['tag1', 'tag2']); $this->assertFalse($this->cache->hasItem('key'), 'Item should be cleared when tag is invalidated'); $this->assertFalse($this->cache->hasItem('key2'), 'Item should be cleared when tag is invalidated'); // Create a new item (no tags) $item = $this->cache->getItem('key')->set('value'); $this->cache->save($item); $this->cache->invalidateTags(['tag1']); $this->assertTrue($this->cache->hasItem('key'), 'Item k list should be removed when clearing the tags'); } /** * When an item is overwritten we need to clear tags for original item. */ public function testTagsAreCleanedOnSave() { if (isset($this->skippedTests[__FUNCTION__])) { $this->markTestSkipped($this->skippedTests[__FUNCTION__]); return; } $pool = $this->cache; $i = $pool->getItem('key')->set('value'); $pool->save($i->setTags(['foo'])); $i = $pool->getItem('key'); $pool->save($i->setTags(['bar'])); $pool->invalidateTags(['foo']); $this->assertTrue($pool->getItem('key')->isHit()); } } integration-tests-0.16.0/tests/000077500000000000000000000000001304464113600164255ustar00rootroot00000000000000integration-tests-0.16.0/tests/StashTest.php000066400000000000000000000013401304464113600210560ustar00rootroot00000000000000, Tobias Nyholm * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ use Cache\IntegrationTests\CachePoolTest as BaseTest; class StashTest extends BaseTest { private $client = null; public function createCachePool() { return new \Stash\Pool($this->getClient()); } private function getClient() { if ($this->client === null) { $this->client = new \Stash\Driver\Redis(['servers' => [['server' => '127.0.0.1', 'port' => '6379']]]); } return $this->client; } }