Passed
Pull Request — master (#251)
by Gabriel
10:21
created

LegacyMongoDBCacheTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 72
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\Common\Cache;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\CacheProvider;
7
use Doctrine\Common\Cache\LegacyMongoDBCache;
8
use Doctrine\Common\Cache\MongoDBCache;
9
use MongoClient;
10
use MongoCollection;
11
use MongoConnectionException;
12
use function sleep;
13
14
/**
15
 * @requires extension mongodb
16
 */
17
class LegacyMongoDBCacheTest extends CacheTest
18
{
19
    /** @var MongoCollection */
20
    private $collection;
21
22
    protected function setUp() : void
23
    {
24
        try {
25
            $mongo = new MongoClient();
26
            $mongo->listDBs();
27
        } catch (MongoConnectionException $e) {
28
            $this->markTestSkipped('Cannot connect to MongoDB because of: ' . $e);
29
        }
30
31
        $this->collection = $mongo->selectCollection('doctrine_common_cache', 'test');
32
    }
33
34
    protected function tearDown() : void
35
    {
36
        if (! ($this->collection instanceof MongoCollection)) {
37
            return;
38
        }
39
40
        $this->collection->drop();
41
    }
42
43
    public function testGetStats() : void
44
    {
45
        $cache = $this->_getCacheDriver();
46
        $stats = $cache->getStats();
47
48
        self::assertNull($stats[Cache::STATS_HITS]);
49
        self::assertNull($stats[Cache::STATS_MISSES]);
50
        self::assertGreaterThan(0, $stats[Cache::STATS_UPTIME]);
51
        self::assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
52
        self::assertNull($stats[Cache::STATS_MEMORY_AVAILABLE]);
53
    }
54
55
    /**
56
     * @group 108
57
     */
58
    public function testMongoCursorExceptionsDoNotBubbleUp() : void
59
    {
60
        /* @var $collection \MongoCollection|\PHPUnit_Framework_MockObject_MockObject */
61
        $collection = $this
62
            ->getMockBuilder(\MongoCollection::class)
63
            ->disableOriginalConstructor()
64
            ->getMock();
65
66
        $collection->expects(self::once())->method('update')->willThrowException(new \MongoCursorException());
67
68
        $cache = new LegacyMongoDBCache($collection);
69
70
        self::assertFalse($cache->save('foo', 'bar'));
71
    }
72
73
    public function testLifetime() : void
74
    {
75
        $cache = $this->_getCacheDriver();
76
        $cache->save('expire', 'value', 1);
77
        self::assertCount(1, $this->collection->getIndexInfo());
78
        self::assertTrue($cache->contains('expire'), 'Data should not be expired yet');
79
        sleep(2);
80
        self::assertFalse($cache->contains('expire'), 'Data should be expired');
81
        self::assertCount(2, $this->collection->getIndexInfo());
82
    }
83
84
    protected function _getCacheDriver() : CacheProvider
85
    {
86
        return new MongoDBCache($this->collection);
87
    }
88
}
89