Merge pull request #14320 from sushantpaste/reporthook
[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
9 public function setUp() {
10 parent::setUp();
11 }
12
13 public function tearDown() {
14 parent::tearDown();
15 }
16
17 /**
18 * Add and remove two items from the same cache instance.
19 */
20 public function testSameInstance() {
21 $a = new CRM_Utils_Cache_SqlGroup([
22 'group' => 'testSameInstance',
23 ]);
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]);
29
30 $barValue = 45.78;
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);
34
35 $a->delete('foo');
36 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
37
38 $a->flush();
39 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
40 }
41
42 /**
43 * Add item to one cache instance then read with another.
44 */
45 public function testTwoInstance() {
46 $a = new CRM_Utils_Cache_SqlGroup([
47 'group' => 'testTwoInstance',
48 ]);
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);
54
55 $b = new CRM_Utils_Cache_SqlGroup([
56 'group' => 'testTwoInstance',
57 'prefetch' => FALSE,
58 ]);
59 $this->assertEquals($b->get('foo'), ['whiz' => 'bang', 'bar' => 3]);
60 }
61
62 /**
63 * Add item to one cache instance then read (with or without prefetch) from another
64 */
65 public function testPrefetch() {
66 // 1. put data in cache
67 $a = new CRM_Utils_Cache_SqlGroup([
68 'group' => 'testPrefetch',
69 'prefetch' => FALSE,
70 ]);
71 $fooValue = ['whiz' => 'bang', 'bar' => 4];
72 $a->set('foo', $fooValue);
73 $this->assertEquals($a->get('foo'), ['whiz' => 'bang', 'bar' => 4]);
74
75 // 2. see what happens when prefetch is TRUE
76 $b = new CRM_Utils_Cache_SqlGroup([
77 'group' => 'testPrefetch',
78 'prefetch' => TRUE,
79 ]);
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'));
84
85 // 3. see what happens when prefetch is FALSE
86 $c = new CRM_Utils_Cache_SqlGroup([
87 'group' => 'testPrefetch',
88 'prefetch' => FALSE,
89 ]);
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'));
96 }
97
98 }