(NFC) (dev/core#878) Simplify copyright header (tests/*)
[civicrm-core.git] / tests / phpunit / E2E / Cache / TwoInstancesTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * If you make two instances of a cache driver, do they coexist as you would expect?
14 *
15 * @group e2e
16 */
17 class E2E_Cache_TwoInstancesTest extends CiviEndToEndTestCase {
18
19 /**
20 * @var Psr\SimpleCache\CacheInterface;
21 */
22 protected $a;
23
24 /**
25 * @var Psr\SimpleCache\CacheInterface;
26 */
27 protected $b;
28
29 protected function setUp() {
30 parent::setUp();
31 $this->a = $this->b = NULL;
32 }
33
34 protected function tearDown() {
35 parent::tearDown();
36 if ($this->a) {
37 $this->a->clear();
38 }
39 if ($this->b) {
40 $this->b->clear();
41 }
42 }
43
44 /**
45 * Get a list of cache-creation specs.
46 */
47 public function getSingleGenerators() {
48 $exs = [];
49 $exs[] = [
50 ['type' => ['SqlGroup'], 'name' => 'TwoInstancesTest_SameSQL'],
51 ];
52 $exs[] = [
53 ['type' => ['*memory*'], 'name' => 'TwoInstancesTest_SameMem'],
54 ];
55 return $exs;
56 }
57
58 /**
59 * Add item to one cache instance then read with another.
60 *
61 * @param array $cacheDef
62 * Cache definition. See CRM_Utils_Cache::create().
63 * @dataProvider getSingleGenerators
64 */
65 public function testSingle_reload($cacheDef) {
66 if (!E2E_Cache_ConfiguredMemoryTest::isMemorySupported() && $cacheDef['type'] === ['*memory*']) {
67 $this->markTestSkipped('This environment is not configured to use a memory-backed cache service.');
68 }
69
70 $a = $this->a = CRM_Utils_Cache::create($cacheDef);
71 $a->set('foo', 1234);
72 $this->assertEquals(1234, $a->get('foo'));
73
74 $b = $this->b = CRM_Utils_Cache::create($cacheDef + ['prefetch' => TRUE]);
75 $this->assertEquals(1234, $b->get('foo'));
76
77 $b = $this->b = CRM_Utils_Cache::create($cacheDef + ['prefetch' => FALSE]);
78 $this->assertEquals(1234, $b->get('foo'));
79 }
80
81 /**
82 * Get a list of distinct cache-creation specs.
83 */
84 public function getTwoGenerators() {
85 $exs = [];
86 $exs[] = [
87 ['type' => ['SqlGroup'], 'name' => 'testTwo_a'],
88 ['type' => ['SqlGroup'], 'name' => 'testTwo_b'],
89 ];
90 $exs[] = [
91 ['type' => ['*memory*'], 'name' => 'testTwo_a'],
92 ['type' => ['*memory*'], 'name' => 'testTwo_b'],
93 ];
94 $exs[] = [
95 ['type' => ['*memory*'], 'name' => 'testTwo_drv'],
96 ['type' => ['SqlGroup'], 'name' => 'testTwo_drv'],
97 ];
98 return $exs;
99 }
100
101 /**
102 * Add items to the two caches. Then clear the first.
103 *
104 * @param array $cacheA
105 * Cache definition. See CRM_Utils_Cache::create().
106 * @param array $cacheB
107 * Cache definition. See CRM_Utils_Cache::create().
108 * @dataProvider getTwoGenerators
109 */
110 public function testDiff_clearA($cacheA, $cacheB) {
111 list($a, $b) = $this->createTwoCaches($cacheA, $cacheB);
112 $a->set('foo', 1234);
113 $b->set('foo', 5678);
114 $this->assertEquals(1234, $a->get('foo'), 'Check value A after initial setup');
115 $this->assertEquals(5678, $b->get('foo'), 'Check value B after initial setup');
116
117 $a->clear();
118 $this->assertEquals(NULL, $a->get('foo'), 'Check value A after clearing A');
119 $this->assertEquals(5678, $b->get('foo'), 'Check value B after clearing A');
120 }
121
122 /**
123 * Add items to the two caches. Then clear the second.
124 *
125 * @param array $cacheA
126 * Cache definition. See CRM_Utils_Cache::create().
127 * @param array $cacheB
128 * Cache definition. See CRM_Utils_Cache::create().
129 * @dataProvider getTwoGenerators
130 */
131 public function testDiff_clearB($cacheA, $cacheB) {
132 list($a, $b) = $this->createTwoCaches($cacheA, $cacheB);
133 $a->set('foo', 1234);
134 $b->set('foo', 5678);
135 $this->assertEquals(1234, $a->get('foo'), 'Check value A after initial setup');
136 $this->assertEquals(5678, $b->get('foo'), 'Check value B after initial setup');
137
138 $b->clear();
139 $this->assertEquals(1234, $a->get('foo'), 'Check value A after clearing B');
140 $this->assertEquals(NULL, $b->get('foo'), 'Check value B after clearing B');
141 }
142
143 /**
144 * Add items to the two caches. Then reload both caches and read from each.
145 *
146 * @param array $cacheA
147 * Cache definition. See CRM_Utils_Cache::create().
148 * @param array $cacheB
149 * Cache definition. See CRM_Utils_Cache::create().
150 * @dataProvider getTwoGenerators
151 */
152 public function testDiff_reload($cacheA, $cacheB) {
153 list($a, $b) = $this->createTwoCaches($cacheA, $cacheB);
154 $a->set('foo', 1234);
155 $b->set('foo', 5678);
156 $this->assertEquals(1234, $a->get('foo'), 'Check value A after initial setup');
157 $this->assertEquals(5678, $b->get('foo'), 'Check value B after initial setup');
158
159 list($a, $b) = $this->createTwoCaches($cacheA, $cacheB);
160 $this->assertEquals(1234, $a->get('foo'), 'Check value A after initial setup');
161 $this->assertEquals(5678, $b->get('foo'), 'Check value B after initial setup');
162 }
163
164 /**
165 * @param $cacheA
166 * @param $cacheB
167 * @return array
168 */
169 protected function createTwoCaches($cacheA, $cacheB) {
170 if (!E2E_Cache_ConfiguredMemoryTest::isMemorySupported() && $cacheA['type'] === ['*memory*']) {
171 $this->markTestSkipped('This environment is not configured to use a memory-backed cache service.');
172 }
173 if (!E2E_Cache_ConfiguredMemoryTest::isMemorySupported() && $cacheB['type'] === ['*memory*']) {
174 $this->markTestSkipped('This environment is not configured to use a memory-backed cache service.');
175 }
176
177 $a = $this->a = CRM_Utils_Cache::create($cacheA);
178 $b = $this->b = CRM_Utils_Cache::create($cacheB);
179 return array($a, $b);
180 }
181
182 }