Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
4 | ||
aba1cd8b EM |
5 | /** |
6 | * Class CRM_Utils_Cache_SqlGroupTest | |
7 | */ | |
6a488035 TO |
8 | class CRM_Utils_Cache_SqlGroupTest extends CiviUnitTestCase { |
9 | function setUp() { | |
10 | parent::setUp(); | |
11 | } | |
12 | ||
13 | function tearDown() { | |
14 | parent::tearDown(); | |
15 | } | |
16 | ||
17 | /** | |
18 | * Add and remove two items from the same cache instance | |
19 | */ | |
20 | function testSameInstance() { | |
21 | $a = new CRM_Utils_Cache_SqlGroup(array( | |
22 | 'group' => 'testSameInstance', | |
23 | )); | |
24 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"'); | |
25 | $fooValue = array('whiz' => 'bang', 'bar' => 2); | |
26 | $a->set('foo', $fooValue); | |
27 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"'); | |
28 | $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 2)); | |
29 | ||
30 | $barValue = 45.78; | |
31 | $a->set('bar', $barValue); | |
32 | $this->assertDBQuery(2, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"'); | |
33 | $this->assertEquals($a->get('bar'), 45.78); | |
97d5a31f | 34 | |
6a488035 TO |
35 | $a->delete('foo'); |
36 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"'); | |
37 | ||
38 | $a->flush(); | |
39 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"'); | |
40 | } | |
41 | ||
42 | /** | |
43 | * Add item to one cache instance then read with another | |
44 | */ | |
45 | function testTwoInstance() { | |
46 | $a = new CRM_Utils_Cache_SqlGroup(array( | |
47 | 'group' => 'testTwoInstance', | |
48 | )); | |
49 | $fooValue = array('whiz' => 'bang', 'bar' => 3); | |
50 | $a->set('foo', $fooValue); | |
51 | $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 3)); | |
52 | ||
53 | $b = new CRM_Utils_Cache_SqlGroup(array( | |
54 | 'group' => 'testTwoInstance', | |
55 | 'prefetch' => FALSE, | |
56 | )); | |
57 | $this->assertEquals($b->get('foo'), array('whiz' => 'bang', 'bar' => 3)); | |
58 | } | |
59 | ||
60 | /** | |
20b015e1 | 61 | * Add item to one cache instance then read (with or without prefetch) from another |
6a488035 TO |
62 | */ |
63 | function testPrefetch() { | |
20b015e1 | 64 | // 1. put data in cache |
6a488035 TO |
65 | $a = new CRM_Utils_Cache_SqlGroup(array( |
66 | 'group' => 'testPrefetch', | |
20b015e1 | 67 | 'prefetch' => FALSE, |
6a488035 TO |
68 | )); |
69 | $fooValue = array('whiz' => 'bang', 'bar' => 4); | |
70 | $a->set('foo', $fooValue); | |
71 | $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 4)); | |
72 | ||
20b015e1 | 73 | // 2. see what happens when prefetch is TRUE |
6a488035 TO |
74 | $b = new CRM_Utils_Cache_SqlGroup(array( |
75 | 'group' => 'testPrefetch', | |
76 | 'prefetch' => TRUE, | |
77 | )); | |
20b015e1 TO |
78 | $this->assertEquals($fooValue, $b->getFromFrontCache('foo')); // should work b/c value was prefetched |
79 | $this->assertEquals($fooValue, $b->get('foo')); // should work b/c value was prefetched | |
80 | ||
81 | // 3. see what happens when prefetch is FALSE | |
82 | $c = new CRM_Utils_Cache_SqlGroup(array( | |
83 | 'group' => 'testPrefetch', | |
84 | 'prefetch' => FALSE, | |
85 | )); | |
86 | $this->assertEquals(NULL, $c->getFromFrontCache('foo')); // should be NULL b/c value was NOT prefetched | |
87 | $this->assertEquals($fooValue, $c->get('foo')); // should work b/c value is fetched on demand | |
88 | $this->assertEquals($fooValue, $c->getFromFrontCache('foo')); // should work b/c value was fetched on demand | |
6a488035 TO |
89 | } |
90 | } |