|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of php-cache organization. |
| 5 | + * |
| 6 | + * (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
| 7 | + * |
| 8 | + * This source file is subject to the MIT license that is bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Cache\Namespaced\Tests; |
| 13 | + |
| 14 | +use Cache\Adapter\Memcached\MemcachedCachePool; |
| 15 | +use Cache\Hierarchy\HierarchicalPoolInterface; |
| 16 | +use Cache\Namespaced\NamespacedCachePool; |
| 17 | +use Memcached; |
| 18 | +use Psr\Cache\CacheItemPoolInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Tobias Nyholm <[email protected]> |
| 22 | + */ |
| 23 | +class IntegrationTest extends \PHPUnit_Framework_TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @type CacheItemPoolInterface|HierarchicalPoolInterface |
| 27 | + */ |
| 28 | + private $cache; |
| 29 | + |
| 30 | + protected function setUp() |
| 31 | + { |
| 32 | + $cache = new Memcached(); |
| 33 | + $cache->addServer('localhost', 11211); |
| 34 | + |
| 35 | + $this->cache = new MemcachedCachePool($cache); |
| 36 | + } |
| 37 | + |
| 38 | + protected function tearDown() |
| 39 | + { |
| 40 | + if ($this->cache !== null) { |
| 41 | + $this->cache->clear(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public function testGetItem() |
| 46 | + { |
| 47 | + $namespace = 'ns'; |
| 48 | + $nsPool = new NamespacedCachePool($this->cache, $namespace); |
| 49 | + |
| 50 | + $item = $nsPool->getItem('key'); |
| 51 | + $this->assertEquals("|$namespace|key", $item->getKey()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testGetItems() |
| 55 | + { |
| 56 | + $namespace = 'ns'; |
| 57 | + $nsPool = new NamespacedCachePool($this->cache, $namespace); |
| 58 | + |
| 59 | + $items = $nsPool->getItems(['key0', 'key1']); |
| 60 | + |
| 61 | + $str = "|$namespace|key0"; |
| 62 | + $this->assertTrue(isset($items[$str])); |
| 63 | + $this->assertEquals($str, $items[$str]->getKey()); |
| 64 | + |
| 65 | + $str = "|$namespace|key1"; |
| 66 | + $this->assertTrue(isset($items[$str])); |
| 67 | + $this->assertEquals($str, $items[$str]->getKey()); |
| 68 | + } |
| 69 | + |
| 70 | + public function testSave() |
| 71 | + { |
| 72 | + $namespace = 'ns'; |
| 73 | + $nsPool = new NamespacedCachePool($this->cache, $namespace); |
| 74 | + |
| 75 | + $item = $nsPool->getItem('key'); |
| 76 | + $item->set('foo'); |
| 77 | + $nsPool->save($item); |
| 78 | + |
| 79 | + $this->assertTrue($nsPool->hasItem('key')); |
| 80 | + $this->assertFalse($this->cache->hasItem('key')); |
| 81 | + } |
| 82 | + |
| 83 | + public function testSaveDeferred() |
| 84 | + { |
| 85 | + $namespace = 'ns'; |
| 86 | + $nsPool = new NamespacedCachePool($this->cache, $namespace); |
| 87 | + |
| 88 | + $item = $nsPool->getItem('key'); |
| 89 | + $item->set('foo'); |
| 90 | + $nsPool->saveDeferred($item); |
| 91 | + |
| 92 | + $this->assertTrue($nsPool->hasItem('key')); |
| 93 | + $this->assertFalse($this->cache->hasItem('key')); |
| 94 | + |
| 95 | + $nsPool->commit(); |
| 96 | + $this->assertTrue($nsPool->hasItem('key')); |
| 97 | + $this->assertFalse($this->cache->hasItem('key')); |
| 98 | + } |
| 99 | +} |
0 commit comments