3 namespace Friendica\Test\Util;
5 use Mockery\MockInterface;
9 public static $connected = true;
13 * Trait to mock the DBA connection status
18 * @var MockInterface The mocking interface of Friendica\Database\DBA
22 private function checkMock()
24 if (!isset($this->dbaMock)) {
25 $this->dbaMock = \Mockery::namedMock('Friendica\Database\DBA', 'Friendica\Test\Util\DBAStub');
30 * Mocking DBA::connect()
32 * @param bool $return True, if the connect was successful, otherwise false
33 * @param null|int $times How often the method will get used
35 public function mockConnect($return = true, $times = null)
40 ->shouldReceive('connect')
46 * Mocking DBA::connected()
48 * @param bool $return True, if the DB is connected, otherwise false
49 * @param null|int $times How often the method will get used
51 public function mockConnected($return = true, $times = null)
56 ->shouldReceive('connected')
62 * Mocking DBA::fetchFirst()
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
68 public function mockFetchFirst($arg, $return = true, $times = null)
73 ->shouldReceive('fetchFirst')
80 * Mocking each DBA::fetch() call of an statement
82 * @param array $stmt The result statement (array)
83 * @param null|int $times How often the method will get used
85 public function mockFetchLoop($stmt = [], $times = null)
89 foreach ($stmt as $item) {
91 ->shouldReceive('fetch')
96 // The last mock call of a fetch (=> breaking the loop)
98 ->shouldReceive('fetch')
104 * Mocking DBA::close()
106 * @param array $return The return per fetch
107 * @param null|int $times How often the method will get used
109 public function mockDbaClose($return = [], $times = null)
114 ->shouldReceive('close')
116 ->andReturn($return);
120 * Mocking DBA::select()
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
128 public function mockSelect($tableName, $select = [], $where = [], $return = null, $times = null)
133 ->shouldReceive('select')
134 ->with($tableName, $select, $where)
136 ->andReturn($return);
140 * Mocking DBA::delete()
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
147 public function mockDBADelete($tableName, $where = [], $return = true, $times = null)
152 ->shouldReceive('delete')
153 ->with($tableName, $where)
155 ->andReturn($return);
159 * Mocking DBA::update()
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
168 public function mockDBAUpdate($expTableName, $expFields, $expCondition, $expOld_fields = [], $return = true, $times = null)
172 $closure = function ($tableName, $fields, $condition, $old_fields = []) use ($expTableName, $expFields, $expCondition, $expOld_fields) {
174 $tableName == $expTableName &&
175 $fields == $expFields &&
176 $condition == $expCondition &&
177 $old_fields == $expOld_fields;
181 ->shouldReceive('update')
184 ->andReturn($return);
188 * Mocking DBA::insert()
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
196 public function mockDBAInsert($expTableName, $expParam, $expOnDuplUpdate = false, $return = true, $times = null)
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;
208 ->shouldReceive('insert')
211 ->andReturn($return);
215 * Mocking DBA::selectFirst()
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
223 public function mockSelectFirst($expTableName, $expSelect = [], $expWhere = [], $return = [], $times = null)
227 $closure = function ($tableName, $select = [], $where = []) use ($expTableName, $expSelect, $expWhere) {
228 return $tableName === $expTableName
229 && $select === $expSelect
230 && $where === $expWhere;
234 ->shouldReceive('selectFirst')
237 ->andReturn($return);
241 * Mocking DBA::isResult()
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
247 public function mockIsResult($record, $return = true, $times = null)
252 ->shouldReceive('isResult')
255 ->andReturn($return);
259 * Mocking DBA::isResult()
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
265 public function mockToArray($record = null, $return = [], $times = null)
270 ->shouldReceive('toArray')
273 ->andReturn($return);
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
283 public function mockP($sql = null, $return = null, $times = null)
291 ->andReturn($return);
297 ->andReturn($return);
302 * Mocking DBA::lock()
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
308 public function mockDbaLock($table, $return = true, $times = null)
313 ->shouldReceive('lock')
316 ->andReturn($return);
320 * Mocking DBA::unlock()
322 * @param bool $return True, if the lock is set successful
323 * @param null|int $times How often the method will get used
325 public function mockDbaUnlock( $return = true, $times = null)
330 ->shouldReceive('unlock')
332 ->andReturn($return);