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