3 namespace Friendica\Test\Util;
5 use Friendica\Core\Cache;
6 use Friendica\Core\Lock\DatabaseLockDriver;
11 use DateTimeFormatMockTrait;
14 * Mocking acquireLock with DBA-backend
15 * @see DatabaseLockDriver::acquireLock()
17 * @param mixed $key The key to lock
18 * @param int $ttl The TimeToLive
20 * @param bool $locked Was the lock already set?
21 * @param null $pid The PID which was set
22 * @param bool $rowExists True, if a row already exists in the lock table
23 * @param null $time The current timestamp
24 * @param null|int $times How often the method will get used
26 public function mockAcquireLock($key, $ttl = Cache::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)
36 $this->mockDbaLock('locks', true, $times);
38 $this->mockUtcNow($time, $times);
39 $result = ['locked' => $locked, 'pid' => $pid];
40 $this->mockSelectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, $time], $result, $times);
41 $this->mockIsResult($result, $rowExists, $times);
45 $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
46 $this->mockDBAUpdate('locks', ['locked' => true, 'pid' => $pid, 'expires' => $time], ['name' => $key], [], true, $times);
49 $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
50 $this->mockDBAInsert('locks', ['name' => $key, 'locked' => true, 'pid' => $pid, 'expires' => $time], false, true, $times);
53 $this->mockDbaUnlock($times);
57 * Mocking isLocked with DBA-backend
58 * @see DatabaseLockDriver::isLocked()
60 * @param mixed $key The key of the lock
61 * @param null|bool $return True, if the key is already locked
62 * @param null $time The current timestamp
63 * @param null|int $times How often the method will get used
65 public function mockIsLocked($key, $return = true, $time = null, $times = null)
71 $this->mockUtcNow($time, $times);
72 $return = ((isset($return)) ? ['locked' => $return] : null);
73 $this->mockSelectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, $time], $return, $times);
74 $this->mockIsResult($return, (isset($return) && $return), $times);
78 * Mocking releaseAll with DBA-backend
79 * @see DatabaseLockDriver::releaseAll()
81 * @param null $pid The PID which was set
82 * @param null|int $times How often the method will get used
84 public function mockReleaseAll($pid = null, $times = null)
90 $this->mockDBADelete('locks', ['pid' => $pid], true, $times);
94 * Mocking ReleaseLock with DBA-backend
95 * @see DatabaseLockDriver::releaseLock()
97 * @param mixed $key The key to release
98 * @param null|int $pid The PID which was set
99 * @param null|int $times How often the method will get used
101 public function mockReleaseLock($key, $pid = null, $times = null)
107 $this->mockDBADelete('locks', ['name' => $key, 'pid' => $pid], true, $times);