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