Update ArrayDecoratorTest.php
[civicrm-core.git] / tests / phpunit / E2E / Cache / ArrayDecoratorTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 */
26
27 /**
28 * Verify that CRM_Utils_Cache_ArrayDecorator complies with PSR-16.
29 *
30 * @group e2e
31 */
32 class E2E_Cache_ArrayDecoratorTest extends E2E_Cache_CacheTestCase {
33
34 /**
35 * @var CRM_Utils_Cache_Interface
36 */
37 protected $a;
38
39 public function createSimpleCache() {
40 return new CRM_Utils_Cache_ArrayDecorator(
41 $this->a = CRM_Utils_Cache::create([
42 'name' => 'e2e array-dec test',
43 'type' => ['ArrayCache'],
44 ])
45 );
46 }
47
48 public function testDoubleLifeWithDelete() {
49 $this->assertFalse($this->a->has('foo'));
50 $this->assertEquals('dfl-1', $this->a->get('foo', 'dfl-1'));
51
52 $this->cache->set('foo', 100);
53
54 $this->assertTrue($this->a->has('foo'));
55 $this->assertEquals(100, $this->a->get('foo', 'dfl-1')[1]);
56
57 $this->cache->set('foo', 200);
58
59 $this->assertTrue($this->a->has('foo'));
60 $this->assertEquals(200, $this->a->get('foo', 'dfl-1')[1]);
61
62 $this->cache->delete('foo');
63
64 $this->assertFalse($this->a->has('foo'));
65 $this->assertEquals('dfl-1', $this->a->get('foo', 'dfl-1'));
66 }
67
68 public function testDoubleLifeWithClear() {
69 $this->assertFalse($this->a->has('foo'));
70 $this->assertEquals('dfl-1', $this->a->get('foo', 'dfl-1'));
71
72 $this->cache->set('foo', 100);
73
74 $this->assertTrue($this->a->has('foo'));
75 $this->assertEquals(100, $this->a->get('foo', 'dfl-1')[1]);
76
77 $this->cache->clear();
78
79 $this->assertFalse($this->a->has('foo'));
80 $this->assertEquals('dfl-1', $this->a->get('foo', 'dfl-1'));
81 }
82
83 public function testSetTtl() {
84 // This test has exhibited some flakiness. It is overridden to
85 // dump more detailed information about failures; however, it should be
86 // substantively the same.
87 if (isset($this->skippedTests[__FUNCTION__])) {
88 $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
89 }
90 $result = $this->cache->set('key1', 'value', 1);
91 $this->assertTrue($result, 'set() must return true if success');
92 $this->assertEquals('value', $this->cache->get('key1'));
93 sleep(2);
94 $this->assertNull($this->cache->get('key1'), 'Value must expire after ttl.');
95
96 $this->cache->set('key2', 'value', new \DateInterval('PT1S'));
97 $key2Value = $this->cache->get('key2');
98 if ($key2Value !== 'value') {
99 // dump out contents of cache.
100 var_dump($this->cache);
101 print_r(date('u') . 'Current UNIX timestamp');
102 }
103 $this->assertEquals('value', $key2Value);
104 sleep(2);
105 $this->assertNull($this->cache->get('key2'), 'Value must expire after ttl.');
106 }
107
108 }