]> git.mxchange.org Git - friendica.git/blob - tests/Util/DbaCacheMockTrait.php
Merge pull request #7465 from nupplaphil/task/dice_cache_lock
[friendica.git] / tests / Util / DbaCacheMockTrait.php
1 <?php
2
3 namespace Friendica\Test\Util;
4
5 trait DbaCacheMockTrait
6 {
7         /**
8          * @var
9          */
10         protected $dba;
11
12         public function __construct()
13         {
14         }
15
16         protected function mockDelete($key, $return = true, $times = null)
17         {
18                 $this->mockDBADelete('cache', ['k' => $key], $return, $times);
19         }
20
21         protected function mockGet($key, $return = null, $time = null, $times = null)
22         {
23                 if ($time === null) {
24                         $time = time();
25                 }
26
27                 $value = @serialize($return);
28
29                 $this->mockSelectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, $time], ['v' => $value], $times);
30                 $this->mockIsResult(['v' => $value], isset($return), $times);
31         }
32
33         protected function mockSet($key, $value, $ttl = Cache::FIVE_MINUTES, $time = null, $return = true, $times = null)
34         {
35                 if ($time === null) {
36                         $time = time();
37                 }
38
39                 if ($ttl > 0) {
40                         $this->mockUtc('now + ' . $ttl . 'seconds', $time + $ttl, $times);
41                         $fields = [
42                                 'v' => serialize($value),
43                                 'expires' => $time + $ttl,
44                                 'updated' => $time
45                         ];
46                 } else {
47                         $fields = [
48                                 'v' => serialize($value),
49                                 'expires' => -1,
50                                 'updated' => $time
51                         ];
52                 }
53
54                 $this->mockDBAUpdate('cache', $fields, ['k' => $key], true, $return, $times);
55         }
56
57         protected function mockClear($outdated = true, $return = true, $times = null)
58         {
59                 if ($outdated) {
60                         $this->mockDBADelete('cache', ['`expires` < NOW()'], $return, $times);
61                 } else {
62                         $this->mockDBADelete('cache', ['`k` IS NOT NULL '], $return, $times);
63                 }
64         }
65
66         protected function mockGetAllKeys($prefix = null, $return = [], $time = null, $times = null)
67         {
68                 if ($time === null) {
69                         $time = time();
70                 }
71
72                 if (empty($prefix)) {
73                         $where = ['`expires` >= ?', $time];
74                 } else {
75                         $where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', $time, $prefix];
76                 }
77
78                 $this->mockSelect('cache', ['k'], $where, $return, $times);
79                 $this->mockFetchLoop($return, $times);
80                 $this->mockDbaClose(true, $times);
81         }
82 }