3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * If you make two instances of a cache driver, do they coexist as you would expect?
17 class E2E_Cache_TwoInstancesTest
extends CiviEndToEndTestCase
{
20 * @var Psr\SimpleCache\CacheInterface;
25 * @var Psr\SimpleCache\CacheInterface;
29 protected function setUp() {
31 $this->a
= $this->b
= NULL;
34 protected function tearDown() {
45 * Get a list of cache-creation specs.
47 public function getSingleGenerators() {
50 ['type' => ['SqlGroup'], 'name' => 'TwoInstancesTest_SameSQL'],
53 ['type' => ['*memory*'], 'name' => 'TwoInstancesTest_SameMem'],
59 * Add item to one cache instance then read with another.
61 * @param array $cacheDef
62 * Cache definition. See CRM_Utils_Cache::create().
63 * @dataProvider getSingleGenerators
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.');
70 $a = $this->a
= CRM_Utils_Cache
::create($cacheDef);
72 $this->assertEquals(1234, $a->get('foo'));
74 $b = $this->b
= CRM_Utils_Cache
::create($cacheDef +
['prefetch' => TRUE]);
75 $this->assertEquals(1234, $b->get('foo'));
77 $b = $this->b
= CRM_Utils_Cache
::create($cacheDef +
['prefetch' => FALSE]);
78 $this->assertEquals(1234, $b->get('foo'));
82 * Get a list of distinct cache-creation specs.
84 public function getTwoGenerators() {
87 ['type' => ['SqlGroup'], 'name' => 'testTwo_a'],
88 ['type' => ['SqlGroup'], 'name' => 'testTwo_b'],
91 ['type' => ['*memory*'], 'name' => 'testTwo_a'],
92 ['type' => ['*memory*'], 'name' => 'testTwo_b'],
95 ['type' => ['*memory*'], 'name' => 'testTwo_drv'],
96 ['type' => ['SqlGroup'], 'name' => 'testTwo_drv'],
102 * Add items to the two caches. Then clear the first.
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
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');
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');
123 * Add items to the two caches. Then clear the second.
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
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');
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');
144 * Add items to the two caches. Then reload both caches and read from each.
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
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');
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');
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.');
173 if (!E2E_Cache_ConfiguredMemoryTest
::isMemorySupported() && $cacheB['type'] === ['*memory*']) {
174 $this->markTestSkipped('This environment is not configured to use a memory-backed cache service.');
177 $a = $this->a
= CRM_Utils_Cache
::create($cacheA);
178 $b = $this->b
= CRM_Utils_Cache
::create($cacheB);
179 return array($a, $b);