3 namespace Friendica\Test\Util;
5 use Friendica\Core\Cache;
6 use Friendica\Core\Lock\DatabaseLock;
11 use DateTimeFormatMockTrait;
14 * Mocking acquireLock with DBA-backend
16 * @param mixed $key The key to lock
17 * @param int $ttl The TimeToLive
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
25 *@see DatabaseLock::acquireLock()
28 public function mockAcquireLock($key, $ttl = Cache::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)
38 $this->mockDbaLock('locks', true, $times);
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);
47 $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
48 $this->mockDBAUpdate('locks', ['locked' => true, 'pid' => $pid, 'expires' => $time], ['name' => $key], [], true, $times);
51 $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
52 $this->mockDBAInsert('locks', ['name' => $key, 'locked' => true, 'pid' => $pid, 'expires' => $time], false, true, $times);
55 $this->mockDbaUnlock($times);
59 * Mocking isLocked with DBA-backend
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
66 *@see DatabaseLock::isLocked()
69 public function mockIsLocked($key, $return = true, $time = null, $times = null)
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);
82 * Mocking releaseAll with DBA-backend
84 * @param null $pid The PID which was set
85 * @param null|int $times How often the method will get used
87 *@see DatabaseLock::releaseAll()
90 public function mockReleaseAll($pid = null, $times = null)
96 $this->mockDBADelete('locks', ['pid' => $pid], true, $times);
100 * Mocking ReleaseLock with DBA-backend
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
106 *@see DatabaseLock::releaseLock()
109 public function mockReleaseLock($key, $pid = null, $times = null)
115 $this->mockDBADelete('locks', ['name' => $key, 'pid' => $pid], true, $times);