Merge pull request #6634 from tobiasd/20190210-crepairwarning
[friendica.git/.git] / tests / Util / DBAMockTrait.php
1 <?php
2
3 namespace Friendica\Test\Util;
4
5 use Mockery\MockInterface;
6
7 class DBAStub
8 {
9         public static $connected = true;
10 }
11
12 /**
13  * Trait to mock the DBA connection status
14  */
15 trait DBAMockTrait
16 {
17         /**
18          * @var MockInterface The mocking interface of Friendica\Database\DBA
19          */
20         private $dbaMock;
21
22         private function checkMock()
23         {
24                 if (!isset($this->dbaMock)) {
25                         $this->dbaMock = \Mockery::namedMock('Friendica\Database\DBA', 'Friendica\Test\Util\DBAStub');
26                 }
27         }
28
29         /**
30          * Mocking DBA::connect()
31          *
32          * @param bool $return True, if the connect was successful, otherwise false
33          * @param null|int $times How often the method will get used
34          */
35         public function mockConnect($return = true, $times = null)
36         {
37                 $this->checkMock();
38
39                 $this->dbaMock
40                         ->shouldReceive('connect')
41                         ->times($times)
42                         ->andReturn($return);
43         }
44
45         /**
46          * Mocking DBA::connected()
47          *
48          * @param bool $return True, if the DB is connected, otherwise false
49          * @param null|int $times How often the method will get used
50          */
51         public function mockConnected($return = true, $times = null)
52         {
53                 $this->checkMock();
54
55                 $this->dbaMock
56                         ->shouldReceive('connected')
57                         ->times($times)
58                         ->andReturn($return);
59         }
60
61         /**
62          * Mocking DBA::fetchFirst()
63          *
64          * @param string $arg The argument of fetchFirst
65          * @param bool $return True, if the DB is connected, otherwise false
66          * @param null|int $times How often the method will get used
67          */
68         public function mockFetchFirst($arg, $return = true, $times = null)
69         {
70                 $this->checkMock();
71
72                 $this->dbaMock
73                         ->shouldReceive('fetchFirst')
74                         ->with($arg)
75                         ->times($times)
76                         ->andReturn($return);
77         }
78
79         /**
80          * Mocking each DBA::fetch() call of an statement
81          *
82          * @param array $stmt The result statement (array)
83          * @param null|int $times How often the method will get used
84          */
85         public function mockFetchLoop($stmt = [], $times = null)
86         {
87                 $this->checkMock();
88
89                 foreach ($stmt as $item) {
90                         $this->dbaMock
91                                 ->shouldReceive('fetch')
92                                 ->times($times)
93                                 ->andReturn($item);
94                 }
95
96                 // The last mock call of a fetch (=> breaking the loop)
97                 $this->dbaMock
98                         ->shouldReceive('fetch')
99                         ->times($times)
100                         ->andReturn(false);
101         }
102
103         /**
104          * Mocking DBA::close()
105          *
106          * @param array $return The return per fetch
107          * @param null|int $times How often the method will get used
108          */
109         public function mockDbaClose($return = [], $times = null)
110         {
111                 $this->checkMock();
112
113                 $this->dbaMock
114                         ->shouldReceive('close')
115                         ->times($times)
116                         ->andReturn($return);
117         }
118
119         /**
120          * Mocking DBA::select()
121          *
122          * @param string $tableName The name of the table
123          * @param array $select The Select Array (Default is [])
124          * @param array $where The Where Array (Default is [])
125          * @param object $return The array to return (Default is [])
126          * @param null|int $times How often the method will get used
127          */
128         public function mockSelect($tableName, $select = [], $where = [], $return = null, $times = null)
129         {
130                 $this->checkMock();
131
132                 $this->dbaMock
133                         ->shouldReceive('select')
134                         ->with($tableName, $select, $where)
135                         ->times($times)
136                         ->andReturn($return);
137         }
138
139         /**
140          * Mocking DBA::delete()
141          *
142          * @param string $tableName The name of the table
143          * @param array $where The Where Array (Default is [])
144          * @param bool $return The array to return (Default is true)
145          * @param null|int $times How often the method will get used
146          */
147         public function mockDBADelete($tableName, $where = [], $return = true, $times = null)
148         {
149                 $this->checkMock();
150
151                 $this->dbaMock
152                         ->shouldReceive('delete')
153                         ->with($tableName, $where)
154                         ->times($times)
155                         ->andReturn($return);
156         }
157
158         /**
159          * Mocking DBA::update()
160          *
161          * @param string $expTableName The name of the table
162          * @param array $expFields The Fields Array
163          * @param array $expCondition The Condition Array
164          * @param array $expOld_fields The Old Fieldnames (Default is [])
165          * @param bool $return true if the update was successful
166          * @param null|int $times How often the method will get used
167          */
168         public function mockDBAUpdate($expTableName, $expFields, $expCondition, $expOld_fields = [], $return = true, $times = null)
169         {
170                 $this->checkMock();
171
172                 $closure = function ($tableName, $fields, $condition, $old_fields = []) use ($expTableName, $expFields, $expCondition, $expOld_fields) {
173                         return
174                                 $tableName == $expTableName &&
175                                 $fields == $expFields &&
176                                 $condition == $expCondition &&
177                                 $old_fields == $expOld_fields;
178                 };
179
180                 $this->dbaMock
181                         ->shouldReceive('update')
182                         ->withArgs($closure)
183                         ->times($times)
184                         ->andReturn($return);
185         }
186
187         /**
188          * Mocking DBA::insert()
189          *
190          * @param string $expTableName    The name of the table
191          * @param array  $expParam        The Parameters Array
192          * @param bool   $expOnDuplUpdate Update on a duplicated entry
193          * @param bool   $return          True if the insert was successful
194          * @param null|int $times How often the method will get used
195          */
196         public function mockDBAInsert($expTableName, $expParam, $expOnDuplUpdate = false, $return = true, $times = null)
197         {
198                 $this->checkMock();
199
200                 $closure = function ($tableName, $param, $on_duplicate_update = false) use ($expTableName, $expParam, $expOnDuplUpdate) {
201                         return $tableName            == $expTableName
202                                 && $param                == $expParam
203                                 && $on_duplicate_update  == $expOnDuplUpdate;
204
205                 };
206
207                 $this->dbaMock
208                         ->shouldReceive('insert')
209                         ->withArgs($closure)
210                         ->times($times)
211                         ->andReturn($return);
212         }
213
214         /**
215          * Mocking DBA::selectFirst()
216          *
217          * @param string $expTableName The name of the table
218          * @param array $expSelect The Select Array (Default is [])
219          * @param array $expWhere The Where Array (Default is [])
220          * @param array $return The array to return (Default is [])
221          * @param null|int $times How often the method will get used
222          */
223         public function mockSelectFirst($expTableName, $expSelect = [], $expWhere = [], $return = [], $times = null)
224         {
225                 $this->checkMock();
226
227                 $closure = function ($tableName, $select = [], $where = []) use ($expTableName, $expSelect, $expWhere) {
228                         return $tableName === $expTableName
229                                 && $select === $expSelect
230                                 && $where === $expWhere;
231                 };
232
233                 $this->dbaMock
234                         ->shouldReceive('selectFirst')
235                         ->withArgs($closure)
236                         ->times($times)
237                         ->andReturn($return);
238         }
239
240         /**
241          * Mocking DBA::isResult()
242          *
243          * @param object $record The record to test
244          * @param bool $return True, if the DB is connected, otherwise false
245          * @param null|int $times How often the method will get used
246          */
247         public function mockIsResult($record, $return = true, $times = null)
248         {
249                 $this->checkMock();
250
251                 $this->dbaMock
252                         ->shouldReceive('isResult')
253                         ->with($record)
254                         ->times($times)
255                         ->andReturn($return);
256         }
257
258         /**
259          * Mocking DBA::isResult()
260          *
261          * @param object $record The record to test
262          * @param array $return The array to return
263          * @param null|int $times How often the method will get used
264          */
265         public function mockToArray($record = null, $return = [], $times = null)
266         {
267                 $this->checkMock();
268
269                 $this->dbaMock
270                         ->shouldReceive('toArray')
271                         ->with($record)
272                         ->times($times)
273                         ->andReturn($return);
274         }
275
276         /**
277          * Mocking DBA::p()
278          *
279          * @param string $sql The SQL statement
280          * @param object $return The object to return
281          * @param null|int $times How often the method will get used
282          */
283         public function mockP($sql = null, $return = null, $times = null)
284         {
285                 $this->checkMock();
286
287                 if (!isset($sql)) {
288                         $this->dbaMock
289                                 ->shouldReceive('p')
290                                 ->times($times)
291                                 ->andReturn($return);
292                 } else {
293                         $this->dbaMock
294                                 ->shouldReceive('p')
295                                 ->with($sql)
296                                 ->times($times)
297                                 ->andReturn($return);
298                 }
299         }
300
301         /**
302          * Mocking DBA::lock()
303          *
304          * @param string $table The table to lock
305          * @param bool $return True, if the lock is set successful
306          * @param null|int $times How often the method will get used
307          */
308         public function mockDbaLock($table, $return = true, $times = null)
309         {
310                 $this->checkMock();
311
312                 $this->dbaMock
313                         ->shouldReceive('lock')
314                         ->with($table)
315                         ->times($times)
316                         ->andReturn($return);
317         }
318
319         /**
320          * Mocking DBA::unlock()
321          *
322          * @param bool $return True, if the lock is set successful
323          * @param null|int $times How often the method will get used
324          */
325         public function mockDbaUnlock( $return = true, $times = null)
326         {
327                 $this->checkMock();
328
329                 $this->dbaMock
330                         ->shouldReceive('unlock')
331                         ->times($times)
332                         ->andReturn($return);
333         }
334 }