Completed
Pull Request — master (#221)
by Andreas
03:46
created

testMongoCursorExceptionsDoNotBubbleUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 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\MongoDBCache;
8
use MongoClient;
9
use MongoCollection;
10
use MongoConnectionException;
11
12
/**
13
 * @requires extension mongodb
14
 */
15
class MongoDBCacheTest extends CacheTest
16
{
17
    /**
18
     * @var MongoCollection
19
     */
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
            $this->collection->drop();
38
        }
39
    }
40
41
    public function testGetStats() : void
42
    {
43
        $cache = $this->_getCacheDriver();
44
        $stats = $cache->getStats();
45
46
        $this->assertNull($stats[Cache::STATS_HITS]);
47
        $this->assertNull($stats[Cache::STATS_MISSES]);
48
        $this->assertGreaterThan(0, $stats[Cache::STATS_UPTIME]);
49
        $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
50
        $this->assertNull($stats[Cache::STATS_MEMORY_AVAILABLE]);
51
    }
52
53
    /**
54
     * @group 108
55
     */
56
    public function testMongoCursorExceptionsDoNotBubbleUp() : void
57
    {
58
        /* @var $collection \MongoCollection|\PHPUnit_Framework_MockObject_MockObject */
59
        $collection = $this
60
            ->getMockBuilder(\MongoCollection::class)
61
            ->disableOriginalConstructor()
62
            ->getMock();
63
64
        $collection->expects(self::once())->method('update')->willThrowException(new \MongoCursorException());
65
66
        $cache = new MongoDBCache($collection);
67
68
        self::assertFalse($cache->save('foo', 'bar'));
69
    }
70
71
    public function testLifetime() : void
72
    {
73
        $cache = $this->_getCacheDriver();
74
        $cache->save('expire', 'value', 1);
75
        $this->assertCount(1, $this->collection->getIndexInfo());
76
        $this->assertTrue($cache->contains('expire'), 'Data should not be expired yet');
77
        sleep(2);
78
        $this->assertFalse($cache->contains('expire'), 'Data should be expired');
79
        $this->assertCount(2, $this->collection->getIndexInfo());
80
    }
81
82
    protected function _getCacheDriver() : CacheProvider
83
    {
84
        return new MongoDBCache($this->collection);
85
    }
86
}
87