Passed
Push — master ( 5c1ad9...58f408 )
by Marco
05:40
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

6 Methods

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