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