1
0
Fork 0

Refactoring DBA-mocking tests

- Reducing DB-dependencies
- Creating DB-cache mocks
- Creating DB-lock mocks
- Switching to mocked dependencies for Cache/Lock/App
This commit is contained in:
Philipp Holzer 2019-01-30 20:26:17 +01:00
commit 433d6abe8c
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
21 changed files with 848 additions and 193 deletions

View file

@ -4,6 +4,11 @@ namespace Friendica\Test\Util;
use Mockery\MockInterface;
class DBAStub
{
public static $connected = true;
}
/**
* Trait to mock the DBA connection status
*/
@ -14,6 +19,13 @@ trait DBAMockTrait
*/
private $dbaMock;
private function checkMock()
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::namedMock('Friendica\Database\DBA', 'Friendica\Test\Util\DBAStub');
}
}
/**
* Mocking DBA::connect()
*
@ -22,9 +34,7 @@ trait DBAMockTrait
*/
public function mockConnect($return = true, $times = null)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->checkMock();
$this->dbaMock
->shouldReceive('connect')
@ -40,9 +50,7 @@ trait DBAMockTrait
*/
public function mockConnected($return = true, $times = null)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->checkMock();
$this->dbaMock
->shouldReceive('connected')
@ -59,9 +67,7 @@ trait DBAMockTrait
*/
public function mockFetchFirst($arg, $return = true, $times = null)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->checkMock();
$this->dbaMock
->shouldReceive('fetchFirst')
@ -70,6 +76,45 @@ trait DBAMockTrait
->andReturn($return);
}
/**
* 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);
}
/**
* Mocking DBA::select()
@ -82,9 +127,7 @@ trait DBAMockTrait
*/
public function mockSelect($tableName, $select = [], $where = [], $return = null, $times = null)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->checkMock();
$this->dbaMock
->shouldReceive('select')
@ -94,23 +137,102 @@ trait DBAMockTrait
}
/**
* Mocking DBA::selectFirst()
* Mocking DBA::delete()
*
* @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 bool $return The array to return (Default is true)
* @param null|int $times How often the method will get used
*/
public function mockDBADelete($tableName, $where = [], $return = true, $times = null)
{
$this->checkMock();
$this->dbaMock
->shouldReceive('delete')
->with($tableName, $where)
->times($times)
->andReturn($return);
}
/**
* Mocking DBA::update()
*
* @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
*/
public function mockDBAUpdate($expTableName, $expFields, $expCondition, $expOld_fields = [], $return = true, $times = null)
{
$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()
*
* @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
*/
public function mockDBAInsert($expTableName, $expParam, $expOnDuplUpdate = false, $return = true, $times = null)
{
$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()
*
* @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
*/
public function mockSelectFirst($tableName, $select = [], $where = [], $return = [], $times = null)
public function mockSelectFirst($expTableName, $expSelect = [], $expWhere = [], $return = [], $times = null)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->checkMock();
$closure = function ($tableName, $select = [], $where = []) use ($expTableName, $expSelect, $expWhere) {
return $tableName === $expTableName
&& $select === $expSelect
&& $where === $expWhere;
};
$this->dbaMock
->shouldReceive('selectFirst')
->with($tableName, $select, $where)
->withArgs($closure)
->times($times)
->andReturn($return);
}
@ -124,9 +246,7 @@ trait DBAMockTrait
*/
public function mockIsResult($record, $return = true, $times = null)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->checkMock();
$this->dbaMock
->shouldReceive('isResult')
@ -144,9 +264,7 @@ trait DBAMockTrait
*/
public function mockToArray($record = null, $return = [], $times = null)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->checkMock();
$this->dbaMock
->shouldReceive('toArray')
@ -155,7 +273,6 @@ trait DBAMockTrait
->andReturn($return);
}
/**
* Mocking DBA::p()
*
@ -165,9 +282,7 @@ trait DBAMockTrait
*/
public function mockP($sql = null, $return = null, $times = null)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->checkMock();
if (!isset($sql)) {
$this->dbaMock
@ -182,4 +297,38 @@ trait DBAMockTrait
->andReturn($return);
}
}
/**
* Mocking DBA::lock()
*
* @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
*/
public function mockDbaLock($table, $return = true, $times = null)
{
$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);
}
}

View file

@ -0,0 +1,38 @@
<?php
namespace Friendica\Test\Util;
use Mockery\MockInterface;
trait DateTimeFormatMockTrait
{
/**
* @var MockInterface The mocking interface of Friendica\Database\DBA
*/
private $dtfMock;
public function mockUtcNow($time, $times = null)
{
if (!isset($this->dtfMock)) {
$this->dtfMock = \Mockery::mock('alias:Friendica\Util\DateTimeFormat');
}
$this->dtfMock
->shouldReceive('utcNow')
->andReturn($time)
->times($times);
}
public function mockUtc($input, $time, $times = null)
{
if (!isset($this->dtfMock)) {
$this->dtfMock = \Mockery::mock('alias:Friendica\Util\DateTimeFormat');
}
$this->dtfMock
->shouldReceive('utc')
->with($input)
->andReturn($time)
->times($times);
}
}

View file

@ -0,0 +1,76 @@
<?php
namespace Friendica\Test\Util;
trait DbaCacheMockTrait
{
use DBAMockTrait;
use DateTimeFormatMockTrait;
protected function mockDelete($key, $return = true, $times = null)
{
$this->mockDBADelete('cache', ['k' => $key], $return, $times);
}
protected function mockGet($key, $return = null, $time = null, $times = null)
{
if ($time === null) {
$time = time();
}
$value = @serialize($return);
$this->mockSelectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, $time], ['v' => $value], $times);
$this->mockIsResult(['v' => $value], isset($return), $times);
}
protected function mockSet($key, $value, $ttl = Cache::FIVE_MINUTES, $time = null, $return = true, $times = null)
{
if ($time === null) {
$time = time();
}
if ($ttl > 0) {
$this->mockUtc('now + ' . $ttl . 'seconds', $time + $ttl, $times);
$fields = [
'v' => serialize($value),
'expires' => $time + $ttl,
'updated' => $time
];
} else {
$fields = [
'v' => serialize($value),
'expires' => -1,
'updated' => $time
];
}
$this->mockDBAUpdate('cache', $fields, ['k' => $key], true, $return, $times);
}
protected function mockClear($outdated = true, $return = true, $times = null)
{
if ($outdated) {
$this->mockDBADelete('cache', ['`expires` < NOW()'], $return, $times);
} else {
$this->mockDBADelete('cache', ['`k` IS NOT NULL '], $return, $times);
}
}
protected function mockGetAllKeys($prefix = null, $return = [], $time = null, $times = null)
{
if ($time === null) {
$time = time();
}
if (empty($prefix)) {
$where = ['`expires` >= ?', $time];
} else {
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', $time, $prefix];
}
$this->mockSelect('cache', ['k'], $where, $return, $times);
$this->mockFetchLoop($return, $times);
$this->mockDbaClose(true, $times);
}
}

View file

@ -0,0 +1,109 @@
<?php
namespace Friendica\Test\Util;
use Friendica\Core\Cache;
use Friendica\Core\Lock\DatabaseLockDriver;
trait DbaLockMockTrait
{
use DBAMockTrait;
use DateTimeFormatMockTrait;
/**
* Mocking acquireLock with DBA-backend
* @see DatabaseLockDriver::acquireLock()
*
* @param mixed $key The key to lock
* @param int $ttl The TimeToLive
*
* @param bool $locked Was the lock already set?
* @param null $pid The PID which was set
* @param bool $rowExists True, if a row already exists in the lock table
* @param null $time The current timestamp
* @param null|int $times How often the method will get used
*/
public function mockAcquireLock($key, $ttl = Cache::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)
{
if ($time === null) {
$time = time();
}
if ($pid === null) {
$pid = getmypid();
}
$this->mockDbaLock('locks', true, $times);
$this->mockUtcNow($time, $times);
$result = ['locked' => $locked, 'pid' => $pid];
$this->mockSelectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, $time], $result, $times);
$this->mockIsResult($result, $rowExists, $times);
if ($rowExists) {
if (!$locked ) {
$this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
$this->mockDBAUpdate('locks', ['locked' => true, 'pid' => $pid, 'expires' => $time], ['name' => $key], [], true, $times);
}
} else {
$this->mockUtc('now + ' . $ttl . 'seconds', $time, $times);
$this->mockDBAInsert('locks', ['name' => $key, 'locked' => true, 'pid' => $pid, 'expires' => $time], false, true, $times);
}
$this->mockDbaUnlock($times);
}
/**
* Mocking isLocked with DBA-backend
* @see DatabaseLockDriver::isLocked()
*
* @param mixed $key The key of the lock
* @param null|bool $return True, if the key is already locked
* @param null $time The current timestamp
* @param null|int $times How often the method will get used
*/
public function mockIsLocked($key, $return = true, $time = null, $times = null)
{
if ($time === null) {
$time = time();
}
$this->mockUtcNow($time, $times);
$return = ((isset($return)) ? ['locked' => $return] : null);
$this->mockSelectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, $time], $return, $times);
$this->mockIsResult($return, (isset($return) && $return), $times);
}
/**
* Mocking releaseAll with DBA-backend
* @see DatabaseLockDriver::releaseAll()
*
* @param null $pid The PID which was set
* @param null|int $times How often the method will get used
*/
public function mockReleaseAll($pid = null, $times = null)
{
if ($pid === null) {
$pid = getmypid();
}
$this->mockDBADelete('locks', ['pid' => $pid], true, $times);
}
/**
* Mocking ReleaseLock with DBA-backend
* @see DatabaseLockDriver::releaseLock()
*
* @param mixed $key The key to release
* @param null|int $pid The PID which was set
* @param null|int $times How often the method will get used
*/
public function mockReleaseLock($key, $pid = null, $times = null)
{
if ($pid === null) {
$pid = getmypid();
}
$this->mockDBADelete('locks', ['name' => $key, 'pid' => $pid], true, $times);
}
}