]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Cache/MemoryCacheTest.php
3f7af71f6de5392bf71165d96514dd4c8719f157
[friendica.git] / tests / src / Core / Cache / MemoryCacheTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Cache;
4
5 use Friendica\Core\Cache\IMemoryCacheDriver;
6
7 abstract class MemoryCacheTest extends CacheTest
8 {
9         /**
10          * @var \Friendica\Core\Cache\IMemoryCacheDriver
11          */
12         protected $instance;
13
14         function setUp()
15         {
16                 parent::setUp();
17                 if (!($this->instance instanceof IMemoryCacheDriver)) {
18                         throw new \Exception('MemoryCacheTest unsupported');
19                 }
20         }
21
22         function testCompareSet() {
23                 $this->assertNull($this->instance->get('value1'));
24
25                 $value='foobar';
26                 $this->instance->add('value1', $value);
27                 $received=$this->instance->get('value1');
28                 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
29                 $newValue='ipsum lorum';
30                 $this->instance->compareSet('value1', $value, $newValue);
31                 $received=$this->instance->get('value1');
32                 $this->assertEquals($newValue, $received, 'Value not overwritten by compareSet');
33         }
34
35         function testNegativeCompareSet() {
36                 $this->assertNull($this->instance->get('value1'));
37
38                 $value='foobar';
39                 $this->instance->add('value1', $value);
40                 $received=$this->instance->get('value1');
41                 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
42                 $newValue='ipsum lorum';
43                 $this->instance->compareSet('value1', 'wrong', $newValue);
44                 $received=$this->instance->get('value1');
45                 $this->assertNotEquals($newValue, $received, 'Value was wrongly overwritten by compareSet');
46                 $this->assertEquals($value, $received, 'Value was wrongly overwritten by any other value');
47         }
48
49         function testCompareDelete() {
50                 $this->assertNull($this->instance->get('value1'));
51
52                 $value='foobar';
53                 $this->instance->add('value1', $value);
54                 $received=$this->instance->get('value1');
55                 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
56                 $this->instance->compareDelete('value1', $value);
57                 $this->assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
58         }
59
60         function testNegativeCompareDelete() {
61                 $this->assertNull($this->instance->get('value1'));
62
63                 $value='foobar';
64                 $this->instance->add('value1', $value);
65                 $received=$this->instance->get('value1');
66                 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
67                 $this->instance->compareDelete('value1', 'wrong');
68                 $this->assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
69
70                 $this->instance->compareDelete('value1', $value);
71                 $this->assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
72         }
73
74         function testAdd() {
75                 $this->assertNull($this->instance->get('value1'));
76
77                 $value='foobar';
78                 $this->instance->add('value1', $value);
79
80                 $newValue='ipsum lorum';
81                 $this->instance->add('value1', $newValue);
82                 $received=$this->instance->get('value1');
83                 $this->assertNotEquals($newValue, $received, 'Value was wrongly overwritten by add');
84                 $this->assertEquals($value, $received, 'Value was wrongly overwritten by any other value');
85
86                 $this->instance->delete('value1');
87                 $this->instance->add('value1', $newValue);
88                 $received=$this->instance->get('value1');
89                 $this->assertEquals($newValue, $received, 'Value was not overwritten by add');
90                 $this->assertNotEquals($value, $received, 'Value was not overwritten by any other value');
91         }
92 }