]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Cache/MemoryCacheTest.php
Merge pull request #9049 from annando/local-followers
[friendica.git] / tests / src / Core / Cache / MemoryCacheTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Core\Cache;
23
24 use Friendica\Core\Cache\IMemoryCache;
25
26 abstract class MemoryCacheTest extends CacheTest
27 {
28         /**
29          * @var \Friendica\Core\Cache\IMemoryCache
30          */
31         protected $instance;
32
33         protected function setUp()
34         {
35                 parent::setUp();
36
37                 if (!($this->instance instanceof IMemoryCache)) {
38                         throw new \Exception('MemoryCacheTest unsupported');
39                 }
40         }
41
42         /**
43          * @small
44          * @dataProvider dataSimple
45          */
46         function testCompareSet($value1, $value2)
47         {
48                 $this->assertNull($this->instance->get('value1'));
49
50                 $this->instance->add('value1', $value1);
51                 $received = $this->instance->get('value1');
52                 $this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
53
54                 $this->instance->compareSet('value1', $value1, $value2);
55                 $received = $this->instance->get('value1');
56                 $this->assertEquals($value2, $received, 'Value not overwritten by compareSet');
57         }
58
59         /**
60          * @small
61          * @dataProvider dataSimple
62          */
63         function testNegativeCompareSet($value1, $value2)
64         {
65                 $this->assertNull($this->instance->get('value1'));
66
67                 $this->instance->add('value1', $value1);
68                 $received = $this->instance->get('value1');
69                 $this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
70
71                 $this->instance->compareSet('value1', 'wrong', $value2);
72                 $received = $this->instance->get('value1');
73                 $this->assertNotEquals($value2, $received, 'Value was wrongly overwritten by compareSet');
74                 $this->assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
75         }
76
77         /**
78          * @small
79          * @dataProvider dataSimple
80          */
81         function testCompareDelete($data)
82         {
83                 $this->assertNull($this->instance->get('value1'));
84
85                 $this->instance->add('value1', $data);
86                 $received = $this->instance->get('value1');
87                 $this->assertEquals($data, $received, 'Value received from cache not equal to the original');
88                 $this->instance->compareDelete('value1', $data);
89                 $this->assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
90         }
91
92         /**
93          * @small
94          * @dataProvider dataSimple
95          */
96         function testNegativeCompareDelete($data)
97         {
98                 $this->assertNull($this->instance->get('value1'));
99
100                 $this->instance->add('value1', $data);
101                 $received = $this->instance->get('value1');
102                 $this->assertEquals($data, $received, 'Value received from cache not equal to the original');
103                 $this->instance->compareDelete('value1', 'wrong');
104                 $this->assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
105
106                 $this->instance->compareDelete('value1', $data);
107                 $this->assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
108         }
109
110         /**
111          * @small
112          * @dataProvider dataSimple
113          */
114         function testAdd($value1, $value2)
115         {
116                 $this->assertNull($this->instance->get('value1'));
117
118                 $this->instance->add('value1', $value1);
119
120                 $this->instance->add('value1', $value2);
121                 $received = $this->instance->get('value1');
122                 $this->assertNotEquals($value2, $received, 'Value was wrongly overwritten by add');
123                 $this->assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
124
125                 $this->instance->delete('value1');
126                 $this->instance->add('value1', $value2);
127                 $received = $this->instance->get('value1');
128                 $this->assertEquals($value2, $received, 'Value was not overwritten by add');
129                 $this->assertNotEquals($value1, $received, 'Value was not overwritten by any other value');
130         }
131 }