Completed
Push — master ( e1f38a...73ba45 )
by Marco
10s
created

MemcachedCacheTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 76
rs 10
c 0
b 0
f 0
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
use function fsockopen;
9
use function sprintf;
10
11
/**
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
    {
89
        $driver = new MemcachedCache();
90
        $driver->setMemcached($this->memcached);
91
        return $driver;
92
    }
93
}
94