1
|
|
|
<?php |
2
|
|
|
namespace Opine\Cache; |
3
|
|
|
|
4
|
|
|
use PHPUnit_Framework_TestCase; |
5
|
|
|
use Opine\Cache\Service as Cache; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class CacheTest extends PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
const ROOT = __DIR__.'/../public'; |
11
|
|
|
|
12
|
|
|
public function testSet() |
13
|
|
|
{ |
14
|
|
|
$cache = new Cache(self::ROOT); |
15
|
|
|
$this->assertTrue($cache->set('phpunit-test', 'A', 30, 0)); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testGet() |
19
|
|
|
{ |
20
|
|
|
$cache = new Cache(self::ROOT); |
21
|
|
|
$this->assertTrue('A' === $cache->get('phpunit-test', 0)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testDelete() |
25
|
|
|
{ |
26
|
|
|
$cache = new Cache(self::ROOT); |
27
|
|
|
$this->assertTrue($cache->delete('phpunit-test', 0)); |
28
|
|
|
$this->assertFalse($cache->get('phpunit-test', 0)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testGetSet() |
32
|
|
|
{ |
33
|
|
|
$cache = new Cache(self::ROOT); |
34
|
|
|
$this->assertTrue('B' === $cache->getSetGet('phpunit-test', function () { |
35
|
|
|
return 'B'; |
36
|
|
|
}, 30, 0)); |
37
|
|
|
$this->assertTrue('B' === $cache->get('phpunit-test', 0)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testGetSetGetBatchNoCallback() |
41
|
|
|
{ |
42
|
|
|
$cache = new Cache(self::ROOT); |
43
|
|
|
$items = [ |
44
|
|
|
'phpunit-test' => 'C', |
45
|
|
|
'phpunit-test2' => 'D', |
46
|
|
|
]; |
47
|
|
|
$caught = false; |
48
|
|
|
try { |
49
|
|
|
$cache->getSetGetBatch($items, 30, 0); |
50
|
|
|
} catch (Exception $e) { |
51
|
|
|
$caught = true; |
52
|
|
|
} |
53
|
|
|
$this->assertTrue($caught); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetSetGetBatch() |
57
|
|
|
{ |
58
|
|
|
$cache = new Cache(self::ROOT); |
59
|
|
|
$items = [ |
60
|
|
|
'phpunit-test' => function () { return 'C'; }, |
61
|
|
|
'phpunit-test2' => function () { return 'D'; } |
62
|
|
|
]; |
63
|
|
|
$this->assertTrue($cache->getSetGetBatch($items, 30, 0)); |
64
|
|
|
$this->assertTrue('B' === $items['phpunit-test']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testGetBatch() |
68
|
|
|
{ |
69
|
|
|
$cache = new Cache(self::ROOT); |
70
|
|
|
$items = [ |
71
|
|
|
'phpunit-test' => function () { return 'C'; }, |
72
|
|
|
'phpunit-test2' => function () { return 'D'; } |
73
|
|
|
]; |
74
|
|
|
$this->assertTrue($cache->getBatch($items, 0)); |
75
|
|
|
$this->assertTrue('B' === $items['phpunit-test']); |
76
|
|
|
$this->assertTrue('D' === $items['phpunit-test2']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testDeleteBatch() |
80
|
|
|
{ |
81
|
|
|
$cache = new Cache(self::ROOT); |
82
|
|
|
$items = ['phpunit-test', 'phpunit-test2']; |
83
|
|
|
$this->assertTrue($cache->deleteBatch($items)); |
84
|
|
|
$this->assertFalse($cache->get('phpunit-test')); |
85
|
|
|
$this->assertFalse($cache->get('phpunit-test2')); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|