]> git.mxchange.org Git - friendica.git/blob - tests/Util/DbaLockMockTrait.php
Merge pull request #1 from friendica/develop
[friendica.git] / tests / Util / DbaLockMockTrait.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 use Friendica\Core\Lock\DatabaseLock;
26
27 trait DbaLockMockTrait
28 {
29         use DBAMockTrait;
30         use DateTimeFormatMockTrait;
31
32         /**
33          * Mocking acquireLock with DBA-backend
34          *
35          * @param mixed    $key       The key to lock
36          * @param int      $ttl       The TimeToLive
37          *
38          * @param bool     $locked    Was the lock already set?
39          * @param null     $pid       The PID which was set
40          * @param bool     $rowExists True, if a row already exists in the lock table
41          * @param null     $time      The current timestamp
42          * @param null|int $times     How often the method will get used
43          *
44          *@see DatabaseLock::acquire()
45          *
46          */
47         public function mockAcquireLock($key, $ttl = Duration::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)
48         {
49                 if ($time === null) {
50                         $time = time();
51                 }
52
53                 if ($pid === null) {
54                         $pid = getmypid();
55                 }
56
57                 $this->mockDbaLock('locks', true, $times);
58
59                 $this->mockUtcNow($time, $times);
60                 $result = ['locked' => $locked, 'pid' => $pid];
61                 $this->mockSelectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, $time], $result, $times);
62                 $this->mockIsResult($result, $rowExists, $times);
63
64                 if ($rowExists) {
65                         if (!$locked ) {
66                                 $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
67                                 $this->mockDBAUpdate('locks', ['locked' => true, 'pid' => $pid, 'expires' => $time], ['name' => $key], [], true, $times);
68                         }
69                 } else {
70                         $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
71                         $this->mockDBAInsert('locks', ['name' => $key, 'locked' => true, 'pid' => $pid, 'expires' => $time], false, true, $times);
72                 }
73
74                 $this->mockDbaUnlock($times);
75         }
76
77         /**
78          * Mocking isLocked with DBA-backend
79          *
80          * @param mixed     $key    The key of the lock
81          * @param null|bool $return True, if the key is already locked
82          * @param null      $time   The current timestamp
83          * @param null|int  $times  How often the method will get used
84          *
85          *@see DatabaseLock::isLocked()
86          *
87          */
88         public function mockIsLocked($key, $return = true, $time = null, $times = null)
89         {
90                 if ($time === null) {
91                         $time = time();
92                 }
93
94                 $this->mockUtcNow($time, $times);
95                 $return = ((isset($return)) ? ['locked' => $return] : null);
96                 $this->mockSelectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, $time], $return, $times);
97                 $this->mockIsResult($return, (isset($return) && $return), $times);
98         }
99
100         /**
101          * Mocking releaseAll with DBA-backend
102          *
103          * @param null     $pid   The PID which was set
104          * @param null|int $times How often the method will get used
105          *
106          *@see DatabaseLock::releaseAll()
107          *
108          */
109         public function mockReleaseAll($pid = null, $times = null)
110         {
111                 if ($pid === null) {
112                         $pid = getmypid();
113                 }
114
115                 $this->mockDBADelete('locks', ['pid' => $pid], true, $times);
116         }
117
118         /**
119          * Mocking ReleaseLock with DBA-backend
120          *
121          * @param mixed    $key    The key to release
122          * @param null|int $pid    The PID which was set
123          * @param null|int $times  How often the method will get used
124          *
125          *@see DatabaseLock::release()
126          *
127          */
128         public function mockReleaseLock($key, $pid = null, $times = null)
129         {
130                 if ($pid === null) {
131                         $pid = getmypid();
132                 }
133
134                 $this->mockDBADelete('locks', ['name' => $key, 'pid' => $pid], true, $times);
135         }
136 }