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 MongoDB\Client; |
9
|
|
|
use MongoDB\Collection; |
10
|
|
|
use MongoDB\Driver\Exception\Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @requires extension mongodb |
14
|
|
|
*/ |
15
|
|
|
class ExtMongoDBCacheTest extends CacheTest |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Collection |
19
|
|
|
*/ |
20
|
|
|
private $collection; |
21
|
|
|
|
22
|
|
|
protected function setUp(): void |
23
|
|
|
{ |
24
|
|
|
try { |
25
|
|
|
$mongo = new Client(); |
26
|
|
|
$mongo->listDatabases(); |
27
|
|
|
} catch (Exception $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 Collection) { |
37
|
|
|
$this->collection->drop(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testGetStats(): void |
42
|
|
|
{ |
43
|
|
|
$cache = $this->_getCacheDriver(); |
44
|
|
|
// Run a query to create the collection |
45
|
|
|
$this->collection->find([]); |
46
|
|
|
$stats = $cache->getStats(); |
47
|
|
|
|
48
|
|
|
$this->assertNull($stats[Cache::STATS_HITS]); |
49
|
|
|
$this->assertNull($stats[Cache::STATS_MISSES]); |
50
|
|
|
$this->assertGreaterThan(0, $stats[Cache::STATS_UPTIME]); |
51
|
|
|
$this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]); |
52
|
|
|
$this->assertNull($stats[Cache::STATS_MEMORY_AVAILABLE]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testLifetime() : void |
56
|
|
|
{ |
57
|
|
|
$cache = $this->_getCacheDriver(); |
58
|
|
|
$cache->save('expire', 'value', 1); |
59
|
|
|
$this->assertCount(1, $this->collection->listIndexes()); |
60
|
|
|
$this->assertTrue($cache->contains('expire'), 'Data should not be expired yet'); |
61
|
|
|
sleep(2); |
62
|
|
|
$this->assertFalse($cache->contains('expire'), 'Data should be expired'); |
63
|
|
|
$this->assertCount(2, $this->collection->listIndexes()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function _getCacheDriver(): CacheProvider |
67
|
|
|
{ |
68
|
|
|
return new MongoDBCache($this->collection); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|