]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Cache/MemoryCacheTest.php
code-standards wrap operators with spaces
[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
30                 $newValue = 'ipsum lorum';
31                 $this->instance->compareSet('value1', $value, $newValue);
32                 $received = $this->instance->get('value1');
33                 $this->assertEquals($newValue, $received, 'Value not overwritten by compareSet');
34         }
35
36         function testNegativeCompareSet() {
37                 $this->assertNull($this->instance->get('value1'));
38
39                 $value = 'foobar';
40                 $this->instance->add('value1', $value);
41                 $received = $this->instance->get('value1');
42                 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
43
44                 $newValue = 'ipsum lorum';
45                 $this->instance->compareSet('value1', 'wrong', $newValue);
46                 $received = $this->instance->get('value1');
47                 $this->assertNotEquals($newValue, $received, 'Value was wrongly overwritten by compareSet');
48                 $this->assertEquals($value, $received, 'Value was wrongly overwritten by any other value');
49         }
50
51         function testCompareDelete() {
52                 $this->assertNull($this->instance->get('value1'));
53
54                 $value = 'foobar';
55                 $this->instance->add('value1', $value);
56                 $received = $this->instance->get('value1');
57                 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
58                 $this->instance->compareDelete('value1', $value);
59                 $this->assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
60         }
61
62         function testNegativeCompareDelete() {
63                 $this->assertNull($this->instance->get('value1'));
64
65                 $value = 'foobar';
66                 $this->instance->add('value1', $value);
67                 $received = $this->instance->get('value1');
68                 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
69                 $this->instance->compareDelete('value1', 'wrong');
70                 $this->assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
71
72                 $this->instance->compareDelete('value1', $value);
73                 $this->assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
74         }
75
76         function testAdd() {
77                 $this->assertNull($this->instance->get('value1'));
78
79                 $value = 'foobar';
80                 $this->instance->add('value1', $value);
81
82                 $newValue = 'ipsum lorum';
83                 $this->instance->add('value1', $newValue);
84                 $received = $this->instance->get('value1');
85                 $this->assertNotEquals($newValue, $received, 'Value was wrongly overwritten by add');
86                 $this->assertEquals($value, $received, 'Value was wrongly overwritten by any other value');
87
88                 $this->instance->delete('value1');
89                 $this->instance->add('value1', $newValue);
90                 $received = $this->instance->get('value1');
91                 $this->assertEquals($newValue, $received, 'Value was not overwritten by add');
92                 $this->assertNotEquals($value, $received, 'Value was not overwritten by any other value');
93         }
94 }