]> git.mxchange.org Git - friendica.git/blob - tests/Util/DbaCacheMockTrait.php
Merge pull request #8649 from annando/annando/issue8550
[friendica.git] / tests / Util / DbaCacheMockTrait.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\Util;
23
24 use Friendica\Core\Cache\Duration;
25
26 trait DbaCacheMockTrait
27 {
28         /**
29          * @var
30          */
31         protected $dba;
32
33         public function __construct()
34         {
35         }
36
37         protected function mockDelete($key, $return = true, $times = null)
38         {
39                 $this->mockDBADelete('cache', ['k' => $key], $return, $times);
40         }
41
42         protected function mockGet($key, $return = null, $time = null, $times = null)
43         {
44                 if ($time === null) {
45                         $time = time();
46                 }
47
48                 $value = @serialize($return);
49
50                 $this->mockSelectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, $time], ['v' => $value], $times);
51                 $this->mockIsResult(['v' => $value], isset($return), $times);
52         }
53
54         protected function mockSet($key, $value, $ttl = Duration::FIVE_MINUTES, $time = null, $return = true, $times = null)
55         {
56                 if ($time === null) {
57                         $time = time();
58                 }
59
60                 if ($ttl > 0) {
61                         $this->mockUtc('now + ' . $ttl . 'seconds', $time + $ttl, $times);
62                         $fields = [
63                                 'v' => serialize($value),
64                                 'expires' => $time + $ttl,
65                                 'updated' => $time
66                         ];
67                 } else {
68                         $fields = [
69                                 'v' => serialize($value),
70                                 'expires' => -1,
71                                 'updated' => $time
72                         ];
73                 }
74
75                 $this->mockDBAUpdate('cache', $fields, ['k' => $key], true, $return, $times);
76         }
77
78         protected function mockClear($outdated = true, $return = true, $times = null)
79         {
80                 if ($outdated) {
81                         $this->mockDBADelete('cache', ['`expires` < NOW()'], $return, $times);
82                 } else {
83                         $this->mockDBADelete('cache', ['`k` IS NOT NULL '], $return, $times);
84                 }
85         }
86
87         protected function mockGetAllKeys($prefix = null, $return = [], $time = null, $times = null)
88         {
89                 if ($time === null) {
90                         $time = time();
91                 }
92
93                 if (empty($prefix)) {
94                         $where = ['`expires` >= ?', $time];
95                 } else {
96                         $where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', $time, $prefix];
97                 }
98
99                 $this->mockSelect('cache', ['k'], $where, $return, $times);
100                 $this->mockFetchLoop($return, $times);
101                 $this->mockDbaClose(true, $times);
102         }
103 }