3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\Util;
24 use Friendica\Core\Cache\Duration;
25 use Friendica\Core\Lock\DatabaseLock;
27 trait DbaLockMockTrait
30 use DateTimeFormatMockTrait;
33 * Mocking acquireLock with DBA-backend
35 * @param mixed $key The key to lock
36 * @param int $ttl The TimeToLive
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
44 *@see DatabaseLock::acquire()
47 public function mockAcquireLock($key, $ttl = Duration::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)
57 $this->mockDbaLock('locks', true, $times);
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);
66 $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
67 $this->mockDBAUpdate('locks', ['locked' => true, 'pid' => $pid, 'expires' => $time], ['name' => $key], [], true, $times);
70 $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
71 $this->mockDBAInsert('locks', ['name' => $key, 'locked' => true, 'pid' => $pid, 'expires' => $time], false, true, $times);
74 $this->mockDbaUnlock($times);
78 * Mocking isLocked with DBA-backend
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
85 *@see DatabaseLock::isLocked()
88 public function mockIsLocked($key, $return = true, $time = null, $times = null)
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);
101 * Mocking releaseAll with DBA-backend
103 * @param null $pid The PID which was set
104 * @param null|int $times How often the method will get used
106 *@see DatabaseLock::releaseAll()
109 public function mockReleaseAll($pid = null, $times = null)
115 $this->mockDBADelete('locks', ['pid' => $pid], true, $times);
119 * Mocking ReleaseLock with DBA-backend
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
125 *@see DatabaseLock::release()
128 public function mockReleaseLock($key, $pid = null, $times = null)
134 $this->mockDBADelete('locks', ['name' => $key, 'pid' => $pid], true, $times);