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