4 * Class CRM_Utils_Cache_SqlGroupTest
7 class CRM_Utils_Cache_SqlGroupTest
extends CiviUnitTestCase
{
8 public function setUp() {
12 public function tearDown() {
17 * Add and remove two items from the same cache instance.
19 public function testSameInstance() {
20 $a = new CRM_Utils_Cache_SqlGroup(array(
21 'group' => 'testSameInstance',
23 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
24 $fooValue = array('whiz' => 'bang', 'bar' => 2);
25 $a->set('foo', $fooValue);
26 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
27 $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 2));
30 $a->set('bar', $barValue);
31 $this->assertDBQuery(2, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
32 $this->assertEquals($a->get('bar'), 45.78);
35 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
38 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
42 * Add item to one cache instance then read with another.
44 public function testTwoInstance() {
45 $a = new CRM_Utils_Cache_SqlGroup(array(
46 'group' => 'testTwoInstance',
48 $fooValue = array('whiz' => 'bang', 'bar' => 3);
49 $a->set('foo', $fooValue);
50 $getValue = $a->get('foo');
51 $expectValue = array('whiz' => 'bang', 'bar' => 3);
52 $this->assertEquals($getValue, $expectValue);
54 $b = new CRM_Utils_Cache_SqlGroup(array(
55 'group' => 'testTwoInstance',
58 $this->assertEquals($b->get('foo'), array('whiz' => 'bang', 'bar' => 3));
62 * Add item to one cache instance then read (with or without prefetch) from another
64 public function testPrefetch() {
65 // 1. put data in cache
66 $a = new CRM_Utils_Cache_SqlGroup(array(
67 'group' => 'testPrefetch',
70 $fooValue = array('whiz' => 'bang', 'bar' => 4);
71 $a->set('foo', $fooValue);
72 $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 4));
74 // 2. see what happens when prefetch is TRUE
75 $b = new CRM_Utils_Cache_SqlGroup(array(
76 'group' => 'testPrefetch',
79 $this->assertEquals($fooValue, $b->getFromFrontCache('foo')); // should work b/c value was prefetched
80 $this->assertEquals($fooValue, $b->get('foo')); // should work b/c value was prefetched
82 // 3. see what happens when prefetch is FALSE
83 $c = new CRM_Utils_Cache_SqlGroup(array(
84 'group' => 'testPrefetch',
87 $this->assertEquals(NULL, $c->getFromFrontCache('foo')); // should be NULL b/c value was NOT prefetched
88 $this->assertEquals($fooValue, $c->get('foo')); // should work b/c value is fetched on demand
89 $this->assertEquals($fooValue, $c->getFromFrontCache('foo')); // should work b/c value was fetched on demand