3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Core\Cache;
24 use Friendica\Core\Cache\IMemoryCache;
26 abstract class MemoryCacheTest extends CacheTest
29 * @var \Friendica\Core\Cache\IMemoryCache
33 protected function setUp()
37 if (!($this->instance instanceof IMemoryCache)) {
38 throw new \Exception('MemoryCacheTest unsupported');
44 * @dataProvider dataSimple
46 function testCompareSet($value1, $value2)
48 $this->assertNull($this->instance->get('value1'));
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');
54 $this->instance->compareSet('value1', $value1, $value2);
55 $received = $this->instance->get('value1');
56 $this->assertEquals($value2, $received, 'Value not overwritten by compareSet');
61 * @dataProvider dataSimple
63 function testNegativeCompareSet($value1, $value2)
65 $this->assertNull($this->instance->get('value1'));
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');
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');
79 * @dataProvider dataSimple
81 function testCompareDelete($data)
83 $this->assertNull($this->instance->get('value1'));
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');
94 * @dataProvider dataSimple
96 function testNegativeCompareDelete($data)
98 $this->assertNull($this->instance->get('value1'));
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');
106 $this->instance->compareDelete('value1', $data);
107 $this->assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
112 * @dataProvider dataSimple
114 function testAdd($value1, $value2)
116 $this->assertNull($this->instance->get('value1'));
118 $this->instance->add('value1', $value1);
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');
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');