2018-10-31 10:16:15 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-10-31 10:16:15 +01:00
|
|
|
|
|
|
|
namespace Friendica\Test\Util;
|
|
|
|
|
2019-02-24 13:40:54 +01:00
|
|
|
use Friendica\Database\DBA;
|
2018-10-31 10:24:07 +01:00
|
|
|
use Mockery\MockInterface;
|
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
class DBAStub
|
|
|
|
{
|
|
|
|
public static $connected = true;
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:16:15 +01:00
|
|
|
/**
|
|
|
|
* Trait to mock the DBA connection status
|
|
|
|
*/
|
|
|
|
trait DBAMockTrait
|
|
|
|
{
|
2018-10-31 10:24:07 +01:00
|
|
|
/**
|
|
|
|
* @var MockInterface The mocking interface of Friendica\Database\DBA
|
|
|
|
*/
|
2018-10-31 10:16:15 +01:00
|
|
|
private $dbaMock;
|
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
private function checkMock()
|
|
|
|
{
|
|
|
|
if (!isset($this->dbaMock)) {
|
2019-02-24 13:40:54 +01:00
|
|
|
$this->dbaMock = \Mockery::namedMock(DBA::class, DBAStub::class);
|
2019-01-30 20:26:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:24:07 +01:00
|
|
|
/**
|
|
|
|
* Mocking DBA::connect()
|
|
|
|
*
|
|
|
|
* @param bool $return True, if the connect was successful, otherwise false
|
|
|
|
* @param null|int $times How often the method will get used
|
|
|
|
*/
|
2018-10-31 10:16:15 +01:00
|
|
|
public function mockConnect($return = true, $times = null)
|
|
|
|
{
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->checkMock();
|
2018-10-31 10:16:15 +01:00
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('connect')
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:24:07 +01:00
|
|
|
/**
|
|
|
|
* Mocking DBA::connected()
|
|
|
|
*
|
|
|
|
* @param bool $return True, if the DB is connected, otherwise false
|
|
|
|
* @param null|int $times How often the method will get used
|
|
|
|
*/
|
2018-10-31 10:16:15 +01:00
|
|
|
public function mockConnected($return = true, $times = null)
|
|
|
|
{
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->checkMock();
|
2018-10-31 10:16:15 +01:00
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('connected')
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
2018-11-01 13:44:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::fetchFirst()
|
|
|
|
*
|
2020-10-18 20:31:57 +02:00
|
|
|
* @param string $arg The argument of fetchFirst
|
|
|
|
* @param bool $return True, if the DB is connected, otherwise false
|
|
|
|
* @param null|int $times How often the method will get used
|
2018-11-01 13:44:47 +01:00
|
|
|
*/
|
2020-10-18 20:31:57 +02:00
|
|
|
public function mockFetchFirst(string $arg, bool $return = true, int $times = null)
|
2018-11-01 13:44:47 +01:00
|
|
|
{
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->checkMock();
|
2018-11-01 13:44:47 +01:00
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('fetchFirst')
|
|
|
|
->with($arg)
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
2018-11-08 00:22:15 +01:00
|
|
|
|
2019-01-30 20:26:17 +01:00
|
|
|
/**
|
|
|
|
* Mocking each DBA::fetch() call of an statement
|
|
|
|
*
|
|
|
|
* @param array $stmt The result statement (array)
|
|
|
|
* @param null|int $times How often the method will get used
|
|
|
|
*/
|
|
|
|
public function mockFetchLoop($stmt = [], $times = null)
|
|
|
|
{
|
|
|
|
$this->checkMock();
|
|
|
|
|
|
|
|
foreach ($stmt as $item) {
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('fetch')
|
|
|
|
->times($times)
|
|
|
|
->andReturn($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last mock call of a fetch (=> breaking the loop)
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('fetch')
|
|
|
|
->times($times)
|
|
|
|
->andReturn(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::close()
|
|
|
|
*
|
|
|
|
* @param array $return The return per fetch
|
|
|
|
* @param null|int $times How often the method will get used
|
|
|
|
*/
|
|
|
|
public function mockDbaClose($return = [], $times = null)
|
|
|
|
{
|
|
|
|
$this->checkMock();
|
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('close')
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
2018-11-08 00:22:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::select()
|
|
|
|
*
|
2020-10-18 20:31:57 +02:00
|
|
|
* @param string $tableName The name of the table
|
|
|
|
* @param array $select The Select Array (Default is [])
|
|
|
|
* @param array $where The Where Array (Default is [])
|
|
|
|
* @param object $return The array to return (Default is [])
|
|
|
|
* @param null|int $times How often the method will get used
|
2018-11-08 00:22:15 +01:00
|
|
|
*/
|
2020-10-18 20:31:57 +02:00
|
|
|
public function mockSelect(string $tableName, array $select = [], array $where = [], $return = null, int $times = null)
|
2018-11-08 00:22:15 +01:00
|
|
|
{
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->checkMock();
|
2018-11-08 00:22:15 +01:00
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('select')
|
|
|
|
->with($tableName, $select, $where)
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-30 20:26:17 +01:00
|
|
|
* Mocking DBA::delete()
|
2018-11-08 00:22:15 +01:00
|
|
|
*
|
2020-10-18 20:31:57 +02:00
|
|
|
* @param string $tableName The name of the table
|
|
|
|
* @param array $where The Where Array (Default is [])
|
|
|
|
* @param bool $return The array to return (Default is true)
|
|
|
|
* @param null|int $times How often the method will get used
|
2019-01-30 20:26:17 +01:00
|
|
|
*/
|
2020-10-18 20:31:57 +02:00
|
|
|
public function mockDBADelete(string $tableName, array $where = [], bool $return = true, int $times = null)
|
2019-01-30 20:26:17 +01:00
|
|
|
{
|
|
|
|
$this->checkMock();
|
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('delete')
|
|
|
|
->with($tableName, $where)
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::update()
|
|
|
|
*
|
2020-10-18 20:31:57 +02:00
|
|
|
* @param string $expTableName The name of the table
|
|
|
|
* @param array $expFields The Fields Array
|
|
|
|
* @param array $expCondition The Condition Array
|
|
|
|
* @param array $expOld_fields The Old Fieldnames (Default is [])
|
|
|
|
* @param bool $return true if the update was successful
|
|
|
|
* @param null|int $times How often the method will get used
|
2019-01-30 20:26:17 +01:00
|
|
|
*/
|
2020-10-18 20:31:57 +02:00
|
|
|
public function mockDBAUpdate(string $expTableName, array $expFields, array $expCondition, array $expOld_fields = [], bool $return = true, int $times = null)
|
2019-01-30 20:26:17 +01:00
|
|
|
{
|
|
|
|
$this->checkMock();
|
|
|
|
|
|
|
|
$closure = function ($tableName, $fields, $condition, $old_fields = []) use ($expTableName, $expFields, $expCondition, $expOld_fields) {
|
|
|
|
return
|
|
|
|
$tableName == $expTableName &&
|
|
|
|
$fields == $expFields &&
|
|
|
|
$condition == $expCondition &&
|
|
|
|
$old_fields == $expOld_fields;
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('update')
|
|
|
|
->withArgs($closure)
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::insert()
|
|
|
|
*
|
2020-10-18 20:31:57 +02:00
|
|
|
* @param string $expTableName The name of the table
|
|
|
|
* @param array $expParam The Parameters Array
|
|
|
|
* @param bool $expOnDuplUpdate Update on a duplicated entry
|
|
|
|
* @param bool $return True if the insert was successful
|
|
|
|
* @param null|int $times How often the method will get used
|
2019-01-30 20:26:17 +01:00
|
|
|
*/
|
2020-10-18 20:31:57 +02:00
|
|
|
public function mockDBAInsert(string $expTableName, array $expParam, bool $expOnDuplUpdate = false, bool $return = true, int $times = null)
|
2019-01-30 20:26:17 +01:00
|
|
|
{
|
|
|
|
$this->checkMock();
|
|
|
|
|
|
|
|
$closure = function ($tableName, $param, $on_duplicate_update = false) use ($expTableName, $expParam, $expOnDuplUpdate) {
|
|
|
|
return $tableName == $expTableName
|
|
|
|
&& $param == $expParam
|
|
|
|
&& $on_duplicate_update == $expOnDuplUpdate;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('insert')
|
|
|
|
->withArgs($closure)
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::selectFirst()
|
|
|
|
*
|
2020-10-18 20:31:57 +02:00
|
|
|
* @param string $expTableName The name of the table
|
|
|
|
* @param array $expSelect The Select Array (Default is [])
|
|
|
|
* @param array $expWhere The Where Array (Default is [])
|
|
|
|
* @param array $return The array to return (Default is [])
|
|
|
|
* @param null|int $times How often the method will get used
|
2018-11-08 00:22:15 +01:00
|
|
|
*/
|
2020-10-18 20:31:57 +02:00
|
|
|
public function mockSelectFirst(string $expTableName, array $expSelect = [], array $expWhere = [], array $return = [], int $times = null)
|
2018-11-08 00:22:15 +01:00
|
|
|
{
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->checkMock();
|
|
|
|
|
|
|
|
$closure = function ($tableName, $select = [], $where = []) use ($expTableName, $expSelect, $expWhere) {
|
|
|
|
return $tableName === $expTableName
|
|
|
|
&& $select === $expSelect
|
|
|
|
&& $where === $expWhere;
|
|
|
|
};
|
2018-11-08 00:22:15 +01:00
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('selectFirst')
|
2019-01-30 20:26:17 +01:00
|
|
|
->withArgs($closure)
|
2018-11-08 00:22:15 +01:00
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::isResult()
|
|
|
|
*
|
|
|
|
* @param object $record The record to test
|
|
|
|
* @param bool $return True, if the DB is connected, otherwise false
|
|
|
|
* @param null|int $times How often the method will get used
|
|
|
|
*/
|
|
|
|
public function mockIsResult($record, $return = true, $times = null)
|
|
|
|
{
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->checkMock();
|
2018-11-08 00:22:15 +01:00
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('isResult')
|
|
|
|
->with($record)
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::isResult()
|
|
|
|
*
|
|
|
|
* @param object $record The record to test
|
|
|
|
* @param array $return The array to return
|
|
|
|
* @param null|int $times How often the method will get used
|
|
|
|
*/
|
|
|
|
public function mockToArray($record = null, $return = [], $times = null)
|
|
|
|
{
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->checkMock();
|
2018-11-08 00:22:15 +01:00
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('toArray')
|
|
|
|
->with($record)
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::p()
|
|
|
|
*
|
|
|
|
* @param string $sql The SQL statement
|
|
|
|
* @param object $return The object to return
|
|
|
|
* @param null|int $times How often the method will get used
|
|
|
|
*/
|
|
|
|
public function mockP($sql = null, $return = null, $times = null)
|
|
|
|
{
|
2019-01-30 20:26:17 +01:00
|
|
|
$this->checkMock();
|
2018-11-08 00:22:15 +01:00
|
|
|
|
|
|
|
if (!isset($sql)) {
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('p')
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
} else {
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('p')
|
|
|
|
->with($sql)
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 20:26:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::lock()
|
|
|
|
*
|
2020-10-18 20:31:57 +02:00
|
|
|
* @param string $table The table to lock
|
|
|
|
* @param bool $return True, if the lock is set successful
|
|
|
|
* @param null|int $times How often the method will get used
|
2019-01-30 20:26:17 +01:00
|
|
|
*/
|
2020-10-18 20:31:57 +02:00
|
|
|
public function mockDbaLock(string $table, bool $return = true, int $times = null)
|
2019-01-30 20:26:17 +01:00
|
|
|
{
|
|
|
|
$this->checkMock();
|
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('lock')
|
|
|
|
->with($table)
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocking DBA::unlock()
|
|
|
|
*
|
|
|
|
* @param bool $return True, if the lock is set successful
|
|
|
|
* @param null|int $times How often the method will get used
|
|
|
|
*/
|
|
|
|
public function mockDbaUnlock( $return = true, $times = null)
|
|
|
|
{
|
|
|
|
$this->checkMock();
|
|
|
|
|
|
|
|
$this->dbaMock
|
|
|
|
->shouldReceive('unlock')
|
|
|
|
->times($times)
|
|
|
|
->andReturn($return);
|
|
|
|
}
|
2018-10-31 10:16:15 +01:00
|
|
|
}
|