3 require_once 'CiviTest/CiviUnitTestCase.php';
6 * Class CRM_Utils_Cache_SqlGroupTest
8 class CRM_Utils_Cache_SqlGroupTest
extends CiviUnitTestCase
{
9 public function setUp() {
13 public function tearDown() {
18 * Add and remove two items from the same cache instance.
20 public function testSameInstance() {
21 $a = new CRM_Utils_Cache_SqlGroup(array(
22 'group' => 'testSameInstance',
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));
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);
36 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
39 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
43 * Add item to one cache instance then read with another.
45 public function testTwoInstance() {
46 $a = new CRM_Utils_Cache_SqlGroup(array(
47 'group' => 'testTwoInstance',
49 $fooValue = array('whiz' => 'bang', 'bar' => 3);
50 $a->set('foo', $fooValue);
51 $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 3));
53 $b = new CRM_Utils_Cache_SqlGroup(array(
54 'group' => 'testTwoInstance',
57 $this->assertEquals($b->get('foo'), array('whiz' => 'bang', 'bar' => 3));
61 * Add item to one cache instance then read (with or without prefetch) from another
63 public function testPrefetch() {
64 // 1. put data in cache
65 $a = new CRM_Utils_Cache_SqlGroup(array(
66 'group' => 'testPrefetch',
69 $fooValue = array('whiz' => 'bang', 'bar' => 4);
70 $a->set('foo', $fooValue);
71 $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 4));
73 // 2. see what happens when prefetch is TRUE
74 $b = new CRM_Utils_Cache_SqlGroup(array(
75 'group' => 'testPrefetch',
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
81 // 3. see what happens when prefetch is FALSE
82 $c = new CRM_Utils_Cache_SqlGroup(array(
83 'group' => 'testPrefetch',
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