Merge pull request #11660 from JMAConsulting/CRM-21754
[civicrm-core.git] / tests / phpunit / CRM / Utils / Cache / SqlGroupTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_Cache_SqlGroupTest
5 * @group headless
6 */
7 class CRM_Utils_Cache_SqlGroupTest extends CiviUnitTestCase {
8 public function setUp() {
9 parent::setUp();
10 }
11
12 public function tearDown() {
13 parent::tearDown();
14 }
15
16 /**
17 * Add and remove two items from the same cache instance.
18 */
19 public function testSameInstance() {
20 $a = new CRM_Utils_Cache_SqlGroup(array(
21 'group' => 'testSameInstance',
22 ));
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));
28
29 $barValue = 45.78;
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);
33
34 $a->delete('foo');
35 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
36
37 $a->flush();
38 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
39 }
40
41 /**
42 * Add item to one cache instance then read with another.
43 */
44 public function testTwoInstance() {
45 $a = new CRM_Utils_Cache_SqlGroup(array(
46 'group' => 'testTwoInstance',
47 ));
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);
53
54 $b = new CRM_Utils_Cache_SqlGroup(array(
55 'group' => 'testTwoInstance',
56 'prefetch' => FALSE,
57 ));
58 $this->assertEquals($b->get('foo'), array('whiz' => 'bang', 'bar' => 3));
59 }
60
61 /**
62 * Add item to one cache instance then read (with or without prefetch) from another
63 */
64 public function testPrefetch() {
65 // 1. put data in cache
66 $a = new CRM_Utils_Cache_SqlGroup(array(
67 'group' => 'testPrefetch',
68 'prefetch' => FALSE,
69 ));
70 $fooValue = array('whiz' => 'bang', 'bar' => 4);
71 $a->set('foo', $fooValue);
72 $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 4));
73
74 // 2. see what happens when prefetch is TRUE
75 $b = new CRM_Utils_Cache_SqlGroup(array(
76 'group' => 'testPrefetch',
77 'prefetch' => TRUE,
78 ));
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
81
82 // 3. see what happens when prefetch is FALSE
83 $c = new CRM_Utils_Cache_SqlGroup(array(
84 'group' => 'testPrefetch',
85 'prefetch' => FALSE,
86 ));
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
90 }
91
92 }