]> git.mxchange.org Git - friendica.git/blob - tests/Util/DbaLockMockTrait.php
Merge pull request #6587 from rabuzarus/20190204_-_magicLinks_for_profileAdvanced
[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\DatabaseLockDriver;
7
8 trait DbaLockMockTrait
9 {
10         use DBAMockTrait;
11         use DateTimeFormatMockTrait;
12
13         /**
14          * Mocking acquireLock with DBA-backend
15          * @see DatabaseLockDriver::acquireLock()
16          *
17          * @param mixed    $key       The key to lock
18          * @param int      $ttl       The TimeToLive
19          *
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
25          */
26         public function mockAcquireLock($key, $ttl = Cache::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)
27         {
28                 if ($time === null) {
29                         $time = time();
30                 }
31
32                 if ($pid === null) {
33                         $pid = getmypid();
34                 }
35
36                 $this->mockDbaLock('locks', true, $times);
37
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);
42
43                 if ($rowExists) {
44                         if (!$locked ) {
45                                 $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
46                                 $this->mockDBAUpdate('locks', ['locked' => true, 'pid' => $pid, 'expires' => $time], ['name' => $key], [], true, $times);
47                         }
48                 } else {
49                         $this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
50                         $this->mockDBAInsert('locks', ['name' => $key, 'locked' => true, 'pid' => $pid, 'expires' => $time], false, true, $times);
51                 }
52
53                 $this->mockDbaUnlock($times);
54         }
55
56         /**
57          * Mocking isLocked with DBA-backend
58          * @see DatabaseLockDriver::isLocked()
59          *
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
64          */
65         public function mockIsLocked($key, $return = true, $time = null, $times = null)
66         {
67                 if ($time === null) {
68                         $time = time();
69                 }
70
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);
75         }
76
77         /**
78          * Mocking releaseAll with DBA-backend
79          * @see DatabaseLockDriver::releaseAll()
80          *
81          * @param null     $pid    The PID which was set
82          * @param null|int $times  How often the method will get used
83          */
84         public function mockReleaseAll($pid = null, $times = null)
85         {
86                 if ($pid === null) {
87                         $pid = getmypid();
88                 }
89
90                 $this->mockDBADelete('locks', ['pid' => $pid], true, $times);
91         }
92
93         /**
94          * Mocking ReleaseLock with DBA-backend
95          * @see DatabaseLockDriver::releaseLock()
96          *
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
100          */
101         public function mockReleaseLock($key, $pid = null, $times = null)
102         {
103                 if ($pid === null) {
104                         $pid = getmypid();
105                 }
106
107                 $this->mockDBADelete('locks', ['name' => $key, 'pid' => $pid], true, $times);
108         }
109 }