Merge pull request #15878 from civicrm/5.20
[civicrm-core.git] / tests / phpunit / CRM / Utils / Cache / SqlGroupTest.php
CommitLineData
6a488035
TO
1<?php
2
aba1cd8b
EM
3/**
4 * Class CRM_Utils_Cache_SqlGroupTest
acb109b7 5 * @group headless
aba1cd8b 6 */
6a488035 7class CRM_Utils_Cache_SqlGroupTest extends CiviUnitTestCase {
39b959db 8
00be9182 9 public function setUp() {
6a488035
TO
10 parent::setUp();
11 }
12
00be9182 13 public function tearDown() {
6a488035
TO
14 parent::tearDown();
15 }
16
17 /**
eceb18cc 18 * Add and remove two items from the same cache instance.
6a488035 19 */
00be9182 20 public function testSameInstance() {
9099cab3 21 $a = new CRM_Utils_Cache_SqlGroup([
6a488035 22 'group' => 'testSameInstance',
9099cab3 23 ]);
6a488035 24 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
9099cab3 25 $fooValue = ['whiz' => 'bang', 'bar' => 2];
6a488035
TO
26 $a->set('foo', $fooValue);
27 $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_cache WHERE group_name = "testSameInstance"');
9099cab3 28 $this->assertEquals($a->get('foo'), ['whiz' => 'bang', 'bar' => 2]);
6a488035
TO
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);
97d5a31f 34
6a488035
TO
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 /**
eceb18cc 43 * Add item to one cache instance then read with another.
6a488035 44 */
00be9182 45 public function testTwoInstance() {
9099cab3 46 $a = new CRM_Utils_Cache_SqlGroup([
6a488035 47 'group' => 'testTwoInstance',
9099cab3
CW
48 ]);
49 $fooValue = ['whiz' => 'bang', 'bar' => 3];
6a488035 50 $a->set('foo', $fooValue);
be5eea0d 51 $getValue = $a->get('foo');
9099cab3 52 $expectValue = ['whiz' => 'bang', 'bar' => 3];
be5eea0d 53 $this->assertEquals($getValue, $expectValue);
6a488035 54
9099cab3 55 $b = new CRM_Utils_Cache_SqlGroup([
6a488035
TO
56 'group' => 'testTwoInstance',
57 'prefetch' => FALSE,
9099cab3
CW
58 ]);
59 $this->assertEquals($b->get('foo'), ['whiz' => 'bang', 'bar' => 3]);
6a488035
TO
60 }
61
62 /**
20b015e1 63 * Add item to one cache instance then read (with or without prefetch) from another
6a488035 64 */
00be9182 65 public function testPrefetch() {
20b015e1 66 // 1. put data in cache
9099cab3 67 $a = new CRM_Utils_Cache_SqlGroup([
6a488035 68 'group' => 'testPrefetch',
20b015e1 69 'prefetch' => FALSE,
9099cab3
CW
70 ]);
71 $fooValue = ['whiz' => 'bang', 'bar' => 4];
6a488035 72 $a->set('foo', $fooValue);
9099cab3 73 $this->assertEquals($a->get('foo'), ['whiz' => 'bang', 'bar' => 4]);
6a488035 74
20b015e1 75 // 2. see what happens when prefetch is TRUE
9099cab3 76 $b = new CRM_Utils_Cache_SqlGroup([
6a488035
TO
77 'group' => 'testPrefetch',
78 'prefetch' => TRUE,
9099cab3 79 ]);
39b959db
SL
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'));
20b015e1
TO
84
85 // 3. see what happens when prefetch is FALSE
9099cab3 86 $c = new CRM_Utils_Cache_SqlGroup([
20b015e1
TO
87 'group' => 'testPrefetch',
88 'prefetch' => FALSE,
9099cab3 89 ]);
39b959db
SL
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'));
6a488035 96 }
96025800 97
6a488035 98}