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