4 * Class CRM_Utils_Cache_SqlGroupTest
7 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([
22 'group' => 'testSameInstance',
24 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
25 $fooValue = ['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'), ['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([
47 'group' => 'testTwoInstance',
49 $fooValue = ['whiz' => 'bang', 'bar' => 3];
50 $a->set('foo', $fooValue);
51 $getValue = $a->get('foo');
52 $expectValue = ['whiz' => 'bang', 'bar' => 3];
53 $this->assertEquals($getValue, $expectValue);
55 $b = new CRM_Utils_Cache_SqlGroup([
56 'group' => 'testTwoInstance',
59 $this->assertEquals($b->get('foo'), ['whiz' => 'bang', 'bar' => 3]);
63 * Add item to one cache instance then read (with or without prefetch) from another
65 public function testPrefetch() {
66 // 1. put data in cache
67 $a = new CRM_Utils_Cache_SqlGroup([
68 'group' => 'testPrefetch',
71 $fooValue = ['whiz' => 'bang', 'bar' => 4];
72 $a->set('foo', $fooValue);
73 $this->assertEquals($a->get('foo'), ['whiz' => 'bang', 'bar' => 4]);
75 // 2. see what happens when prefetch is TRUE
76 $b = new CRM_Utils_Cache_SqlGroup([
77 'group' => 'testPrefetch',
80 // should work b/c value was prefetched
81 $this->assertEquals($fooValue, $b->getFromFrontCache('foo'));
82 // should work b/c value was prefetched
83 $this->assertEquals($fooValue, $b->get('foo'));
85 // 3. see what happens when prefetch is FALSE
86 $c = new CRM_Utils_Cache_SqlGroup([
87 'group' => 'testPrefetch',
90 // should be NULL b/c value was NOT prefetched
91 $this->assertEquals(NULL, $c->getFromFrontCache('foo'));
92 // should work b/c value is fetched on demand
93 $this->assertEquals($fooValue, $c->get('foo'));
94 // should work b/c value was fetched on demand
95 $this->assertEquals($fooValue, $c->getFromFrontCache('foo'));