3 * @copyright Copyright (C) 2010-2021, the Friendica project
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;
25 use Friendica\Core\Cache\IMemoryCache;
27 abstract class MemoryCacheTest extends CacheTest
34 protected function setUp(): void
38 if (!($this->instance instanceof IMemoryCache)) {
39 throw new Exception('MemoryCacheTest unsupported');
45 * @dataProvider dataSimple
47 public function testCompareSet($value1, $value2)
49 self::assertNull($this->instance->get('value1'));
51 $this->instance->add('value1', $value1);
52 $received = $this->instance->get('value1');
53 self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
55 $this->instance->compareSet('value1', $value1, $value2);
56 $received = $this->instance->get('value1');
57 self::assertEquals($value2, $received, 'Value not overwritten by compareSet');
62 * @dataProvider dataSimple
64 public function testNegativeCompareSet($value1, $value2)
66 self::assertNull($this->instance->get('value1'));
68 $this->instance->add('value1', $value1);
69 $received = $this->instance->get('value1');
70 self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
72 $this->instance->compareSet('value1', 'wrong', $value2);
73 $received = $this->instance->get('value1');
74 self::assertNotEquals($value2, $received, 'Value was wrongly overwritten by compareSet');
75 self::assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
80 * @dataProvider dataSimple
82 public function testCompareDelete($data)
84 self::assertNull($this->instance->get('value1'));
86 $this->instance->add('value1', $data);
87 $received = $this->instance->get('value1');
88 self::assertEquals($data, $received, 'Value received from cache not equal to the original');
89 $this->instance->compareDelete('value1', $data);
90 self::assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
95 * @dataProvider dataSimple
97 public function testNegativeCompareDelete($data)
99 self::assertNull($this->instance->get('value1'));
101 $this->instance->add('value1', $data);
102 $received = $this->instance->get('value1');
103 self::assertEquals($data, $received, 'Value received from cache not equal to the original');
104 $this->instance->compareDelete('value1', 'wrong');
105 self::assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
107 $this->instance->compareDelete('value1', $data);
108 self::assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
113 * @dataProvider dataSimple
115 public function testAdd($value1, $value2)
117 self::assertNull($this->instance->get('value1'));
119 $this->instance->add('value1', $value1);
121 $this->instance->add('value1', $value2);
122 $received = $this->instance->get('value1');
123 self::assertNotEquals($value2, $received, 'Value was wrongly overwritten by add');
124 self::assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
126 $this->instance->delete('value1');
127 $this->instance->add('value1', $value2);
128 $received = $this->instance->get('value1');
129 self::assertEquals($value2, $received, 'Value was not overwritten by add');
130 self::assertNotEquals($value1, $received, 'Value was not overwritten by any other value');