3 namespace Friendica\Test\Util;
5 use Friendica\Database\DBA;
6 use Mockery\MockInterface;
10 public static $connected = true;
14 * Trait to mock the DBA connection status
19 * @var MockInterface The mocking interface of Friendica\Database\DBA
23 private function checkMock()
25 if (!isset($this->dbaMock)) {
26 $this->dbaMock = \Mockery::namedMock(DBA::class, DBAStub::class);
31 * Mocking DBA::connect()
33 * @param bool $return True, if the connect was successful, otherwise false
34 * @param null|int $times How often the method will get used
36 public function mockConnect($return = true, $times = null)
41 ->shouldReceive('connect')
47 * Mocking DBA::connected()
49 * @param bool $return True, if the DB is connected, otherwise false
50 * @param null|int $times How often the method will get used
52 public function mockConnected($return = true, $times = null)
57 ->shouldReceive('connected')
63 * Mocking DBA::fetchFirst()
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
69 public function mockFetchFirst($arg, $return = true, $times = null)
74 ->shouldReceive('fetchFirst')
81 * Mocking each DBA::fetch() call of an statement
83 * @param array $stmt The result statement (array)
84 * @param null|int $times How often the method will get used
86 public function mockFetchLoop($stmt = [], $times = null)
90 foreach ($stmt as $item) {
92 ->shouldReceive('fetch')
97 // The last mock call of a fetch (=> breaking the loop)
99 ->shouldReceive('fetch')
105 * Mocking DBA::close()
107 * @param array $return The return per fetch
108 * @param null|int $times How often the method will get used
110 public function mockDbaClose($return = [], $times = null)
115 ->shouldReceive('close')
117 ->andReturn($return);
121 * Mocking DBA::select()
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
129 public function mockSelect($tableName, $select = [], $where = [], $return = null, $times = null)
134 ->shouldReceive('select')
135 ->with($tableName, $select, $where)
137 ->andReturn($return);
141 * Mocking DBA::delete()
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
148 public function mockDBADelete($tableName, $where = [], $return = true, $times = null)
153 ->shouldReceive('delete')
154 ->with($tableName, $where)
156 ->andReturn($return);
160 * Mocking DBA::update()
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
169 public function mockDBAUpdate($expTableName, $expFields, $expCondition, $expOld_fields = [], $return = true, $times = null)
173 $closure = function ($tableName, $fields, $condition, $old_fields = []) use ($expTableName, $expFields, $expCondition, $expOld_fields) {
175 $tableName == $expTableName &&
176 $fields == $expFields &&
177 $condition == $expCondition &&
178 $old_fields == $expOld_fields;
182 ->shouldReceive('update')
185 ->andReturn($return);
189 * Mocking DBA::insert()
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
197 public function mockDBAInsert($expTableName, $expParam, $expOnDuplUpdate = false, $return = true, $times = null)
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;
209 ->shouldReceive('insert')
212 ->andReturn($return);
216 * Mocking DBA::selectFirst()
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
224 public function mockSelectFirst($expTableName, $expSelect = [], $expWhere = [], $return = [], $times = null)
228 $closure = function ($tableName, $select = [], $where = []) use ($expTableName, $expSelect, $expWhere) {
229 return $tableName === $expTableName
230 && $select === $expSelect
231 && $where === $expWhere;
235 ->shouldReceive('selectFirst')
238 ->andReturn($return);
242 * Mocking DBA::isResult()
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
248 public function mockIsResult($record, $return = true, $times = null)
253 ->shouldReceive('isResult')
256 ->andReturn($return);
260 * Mocking DBA::isResult()
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
266 public function mockToArray($record = null, $return = [], $times = null)
271 ->shouldReceive('toArray')
274 ->andReturn($return);
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
284 public function mockP($sql = null, $return = null, $times = null)
292 ->andReturn($return);
298 ->andReturn($return);
303 * Mocking DBA::lock()
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
309 public function mockDbaLock($table, $return = true, $times = null)
314 ->shouldReceive('lock')
317 ->andReturn($return);
321 * Mocking DBA::unlock()
323 * @param bool $return True, if the lock is set successful
324 * @param null|int $times How often the method will get used
326 public function mockDbaUnlock( $return = true, $times = null)
331 ->shouldReceive('unlock')
333 ->andReturn($return);