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
|
|
|
|