]> git.mxchange.org Git - friendica.git/blob - tests/Util/DbaLockMockTrait.php
Rename *CacheDriver to *Cache because they don't act as driver anymore
[friendica.git] / tests / Util / DbaLockMockTrait.php
1 <?php
2
3 namespace Friendica\Test\Util;
4
5 use Friendica\Core\Cache;
6 use Friendica\Core\Lock\DatabaseLock;
7
8 trait DbaLockMockTrait
9 {
10         use DBAMockTrait;
11         use DateTimeFormatMockTrait;
12
13         /**
14          * Mocking acquireLock with DBA-backend
15          *
16          * @param mixed    $key       The key to lock
17          * @param int      $ttl       The TimeToLive
18          *
19          * @param bool     $locked    Was the lock already set?
20          * @param null     $pid       The PID which was set
21          * @param bool     $rowExists True, if a row already exists in the lock table
22          * @param null     $time      The current timestamp
23          * @param null|int $times     How often the method will get used
24          *
25          *@see DatabaseLock::acquireLock()
26          *
27          */
28         public function mockAcquireLock($key, $ttl = Cache::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)
29         {
30                 if ($time === null) {
31                         $time = time();
32                 }
33
34                 if ($pid === null) {
35                         $pid = getmypid();
36                 }
37
38                 $this->mockDbaLock('locks', true, $times);
39
40                 $this->mockUtcNow($time, $times);
41                 $result = ['locked' => $locked, 'pid' => $pid];
42                 $this->mockSelectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, $time], $result, $times);
43                 $this->mockIsResult($result, $rowExists, $times);
44
45                 if ($rowExists) {
46                         if (!$locked ) {
47                                 $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
48                                 $this->mockDBAUpdate('locks', ['locked' => true, 'pid' => $pid, 'expires' => $time], ['name' => $key], [], true, $times);
49                         }
50                 } else {
51                         $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
52                         $this->mockDBAInsert('locks', ['name' => $key, 'locked' => true, 'pid' => $pid, 'expires' => $time], false, true, $times);
53                 }
54
55                 $this->mockDbaUnlock($times);
56         }
57
58         /**
59          * Mocking isLocked with DBA-backend
60          *
61          * @param mixed     $key    The key of the lock
62          * @param null|bool $return True, if the key is already locked
63          * @param null      $time   The current timestamp
64          * @param null|int  $times  How often the method will get used
65          *
66          *@see DatabaseLock::isLocked()
67          *
68          */
69         public function mockIsLocked($key, $return = true, $time = null, $times = null)
70         {
71                 if ($time === null) {
72                         $time = time();
73                 }
74
75                 $this->mockUtcNow($time, $times);
76                 $return = ((isset($return)) ? ['locked' => $return] : null);
77                 $this->mockSelectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, $time], $return, $times);
78                 $this->mockIsResult($return, (isset($return) && $return), $times);
79         }
80
81         /**
82          * Mocking releaseAll with DBA-backend
83          *
84          * @param null     $pid   The PID which was set
85          * @param null|int $times How often the method will get used
86          *
87          *@see DatabaseLock::releaseAll()
88          *
89          */
90         public function mockReleaseAll($pid = null, $times = null)
91         {
92                 if ($pid === null) {
93                         $pid = getmypid();
94                 }
95
96                 $this->mockDBADelete('locks', ['pid' => $pid], true, $times);
97         }
98
99         /**
100          * Mocking ReleaseLock with DBA-backend
101          *
102          * @param mixed    $key    The key to release
103          * @param null|int $pid    The PID which was set
104          * @param null|int $times  How often the method will get used
105          *
106          *@see DatabaseLock::releaseLock()
107          *
108          */
109         public function mockReleaseLock($key, $pid = null, $times = null)
110         {
111                 if ($pid === null) {
112                         $pid = getmypid();
113                 }
114
115                 $this->mockDBADelete('locks', ['name' => $key, 'pid' => $pid], true, $times);
116         }
117 }