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