3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\Util;
24 use Friendica\Database\DBA;
25 use Mockery\MockInterface;
29 public static $connected = true;
33 * Trait to mock the DBA connection status
38 * @var MockInterface The mocking interface of Friendica\Database\DBA
42 private function checkMock()
44 if (!isset($this->dbaMock)) {
45 $this->dbaMock = \Mockery::namedMock(DBA::class, DBAStub::class);
50 * Mocking DBA::connect()
52 * @param bool $return True, if the connect was successful, otherwise false
53 * @param null|int $times How often the method will get used
55 public function mockConnect($return = true, $times = null)
60 ->shouldReceive('connect')
66 * Mocking DBA::connected()
68 * @param bool $return True, if the DB is connected, otherwise false
69 * @param null|int $times How often the method will get used
71 public function mockConnected($return = true, $times = null)
76 ->shouldReceive('connected')
82 * Mocking DBA::fetchFirst()
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
88 public function mockFetchFirst(string $arg, bool $return = true, int $times = null)
93 ->shouldReceive('fetchFirst')
100 * Mocking each DBA::fetch() call of an statement
102 * @param array $stmt The result statement (array)
103 * @param null|int $times How often the method will get used
105 public function mockFetchLoop($stmt = [], $times = null)
109 foreach ($stmt as $item) {
111 ->shouldReceive('fetch')
116 // The last mock call of a fetch (=> breaking the loop)
118 ->shouldReceive('fetch')
124 * Mocking DBA::close()
126 * @param array $return The return per fetch
127 * @param null|int $times How often the method will get used
129 public function mockDbaClose($return = [], $times = null)
134 ->shouldReceive('close')
136 ->andReturn($return);
140 * Mocking DBA::select()
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
148 public function mockSelect(string $tableName, array $select = [], array $where = [], $return = null, int $times = null)
153 ->shouldReceive('select')
154 ->with($tableName, $select, $where)
156 ->andReturn($return);
160 * Mocking DBA::delete()
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
167 public function mockDBADelete(string $tableName, array $where = [], bool $return = true, int $times = null)
172 ->shouldReceive('delete')
173 ->with($tableName, $where)
175 ->andReturn($return);
179 * Mocking DBA::update()
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
188 public function mockDBAUpdate(string $expTableName, array $expFields, array $expCondition, array $expOld_fields = [], bool $return = true, int $times = null)
192 $closure = function ($tableName, $fields, $condition, $old_fields = []) use ($expTableName, $expFields, $expCondition, $expOld_fields) {
194 $tableName == $expTableName &&
195 $fields == $expFields &&
196 $condition == $expCondition &&
197 $old_fields == $expOld_fields;
201 ->shouldReceive('update')
204 ->andReturn($return);
208 * Mocking DBA::insert()
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
216 public function mockDBAInsert(string $expTableName, array $expParam, bool $expOnDuplUpdate = false, bool $return = true, int $times = null)
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;
228 ->shouldReceive('insert')
231 ->andReturn($return);
235 * Mocking DBA::selectFirst()
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
243 public function mockSelectFirst(string $expTableName, array $expSelect = [], array $expWhere = [], array $return = [], int $times = null)
247 $closure = function ($tableName, $select = [], $where = []) use ($expTableName, $expSelect, $expWhere) {
248 return $tableName === $expTableName
249 && $select === $expSelect
250 && $where === $expWhere;
254 ->shouldReceive('selectFirst')
257 ->andReturn($return);
261 * Mocking DBA::isResult()
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
267 public function mockIsResult($record, $return = true, $times = null)
272 ->shouldReceive('isResult')
275 ->andReturn($return);
279 * Mocking DBA::isResult()
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
285 public function mockToArray($record = null, $return = [], $times = null)
290 ->shouldReceive('toArray')
293 ->andReturn($return);
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
303 public function mockP($sql = null, $return = null, $times = null)
311 ->andReturn($return);
317 ->andReturn($return);
322 * Mocking DBA::lock()
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
328 public function mockDbaLock(string $table, bool $return = true, int $times = null)
333 ->shouldReceive('lock')
336 ->andReturn($return);
340 * Mocking DBA::unlock()
342 * @param bool $return True, if the lock is set successful
343 * @param null|int $times How often the method will get used
345 public function mockDbaUnlock( $return = true, $times = null)
350 ->shouldReceive('unlock')
352 ->andReturn($return);