1 | <?php |
||
12 | * @requires extension memcached |
||
13 | */ |
||
14 | class MemcachedCacheTest extends CacheTest |
||
15 | { |
||
16 | private $memcached; |
||
17 | |||
18 | protected function setUp() : void |
||
19 | { |
||
20 | $this->memcached = new Memcached(); |
||
21 | $this->memcached->setOption(Memcached::OPT_COMPRESSION, false); |
||
22 | $this->memcached->addServer('127.0.0.1', 11211); |
||
23 | |||
24 | if (@fsockopen('127.0.0.1', 11211) !== false) { |
||
25 | return; |
||
26 | } |
||
27 | |||
28 | unset($this->memcached); |
||
29 | $this->markTestSkipped('Cannot connect to Memcached.'); |
||
30 | } |
||
31 | |||
32 | protected function tearDown() : void |
||
33 | { |
||
34 | if (! ($this->memcached instanceof Memcached)) { |
||
35 | return; |
||
36 | } |
||
37 | |||
38 | $this->memcached->flush(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | * |
||
44 | * Memcached does not support " ", null byte and very long keys so we remove them from the tests. |
||
45 | */ |
||
46 | public function provideCacheIds() : array |
||
47 | { |
||
48 | $ids = parent::provideCacheIds(); |
||
49 | unset($ids[21], $ids[22], $ids[24]); |
||
50 | |||
51 | return $ids; |
||
52 | } |
||
53 | |||
54 | public function testGetMemcachedReturnsInstanceOfMemcached() : void |
||
55 | { |
||
56 | self::assertInstanceOf('Memcached', $this->_getCacheDriver()->getMemcached()); |
||
57 | } |
||
58 | |||
59 | public function testContainsWithKeyWithFalseAsValue() |
||
60 | { |
||
61 | $testKey = __METHOD__; |
||
62 | $driver = $this->_getCacheDriver(); |
||
63 | $reflection = new \ReflectionClass($driver); |
||
64 | $method = $reflection->getMethod('getNamespacedId'); |
||
65 | $method->setAccessible(true); |
||
66 | $testKeyNS = $method->invokeArgs($driver, [$testKey]); |
||
67 | $this->memcached->set($testKeyNS, false); |
||
68 | |||
69 | self::assertTrue($driver->contains($testKey), sprintf('Expected key "%s" to be found in cache.', $testKey)); |
||
70 | self::assertFalse($driver->contains($testKey . '1'), 'No set key should not be found.'); |
||
71 | } |
||
72 | |||
73 | public function testContainsWithKeyOnNonReachableCache() |
||
74 | { |
||
75 | $testKey = __METHOD__; |
||
76 | $memcached = new Memcached(); |
||
77 | $memcached->addServer('0.0.0.1', 11211); // fake server is not available |
||
78 | $driver = new MemcachedCache(); |
||
79 | $driver->setMemcached($memcached); |
||
80 | |||
81 | self::assertFalse($driver->contains($testKey), sprintf('Expected key "%s" not to be found in cache.', $testKey)); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritDoc} |
||
86 | */ |
||
87 | protected function _getCacheDriver() : CacheProvider |
||
88 | { |
||
94 |