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