Merge pull request #7895 from cividesk/CRM-18130-master
[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 $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 3));
51
52 $b = new CRM_Utils_Cache_SqlGroup(array(
53 'group' => 'testTwoInstance',
54 'prefetch' => FALSE,
55 ));
56 $this->assertEquals($b->get('foo'), array('whiz' => 'bang', 'bar' => 3));
57 }
58
59 /**
60 * Add item to one cache instance then read (with or without prefetch) from another
61 */
62 public function testPrefetch() {
63 // 1. put data in cache
64 $a = new CRM_Utils_Cache_SqlGroup(array(
65 'group' => 'testPrefetch',
66 'prefetch' => FALSE,
67 ));
68 $fooValue = array('whiz' => 'bang', 'bar' => 4);
69 $a->set('foo', $fooValue);
70 $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 4));
71
72 // 2. see what happens when prefetch is TRUE
73 $b = new CRM_Utils_Cache_SqlGroup(array(
74 'group' => 'testPrefetch',
75 'prefetch' => TRUE,
76 ));
77 $this->assertEquals($fooValue, $b->getFromFrontCache('foo')); // should work b/c value was prefetched
78 $this->assertEquals($fooValue, $b->get('foo')); // should work b/c value was prefetched
79
80 // 3. see what happens when prefetch is FALSE
81 $c = new CRM_Utils_Cache_SqlGroup(array(
82 'group' => 'testPrefetch',
83 'prefetch' => FALSE,
84 ));
85 $this->assertEquals(NULL, $c->getFromFrontCache('foo')); // should be NULL b/c value was NOT prefetched
86 $this->assertEquals($fooValue, $c->get('foo')); // should work b/c value is fetched on demand
87 $this->assertEquals($fooValue, $c->getFromFrontCache('foo')); // should work b/c value was fetched on demand
88 }
89
90 }