Cleanup tests

- Remove DBAMockTrait
- Remove DBStructureMockTrait
This commit is contained in:
Philipp Holzer 2021-10-18 20:08:40 +02:00
parent 493b428991
commit ef1e2ce77e
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
5 changed files with 82 additions and 532 deletions

View file

@ -1,354 +0,0 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Test\Util;
use Friendica\Database\DBA;
use Mockery\MockInterface;
class DBAStub
{
public static $connected = true;
}
/**
* Trait to mock the DBA connection status
*/
trait DBAMockTrait
{
/**
* @var MockInterface The mocking interface of Friendica\Database\DBA
*/
private $dbaMock;
private function checkMock()
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::namedMock(DBA::class, DBAStub::class);
}
}
/**
* 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
*/
public function mockConnect($return = true, $times = null)
{
$this->checkMock();
$this->dbaMock
->shouldReceive('connect')
->times($times)
->andReturn($return);
}
/**
* 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
*/
public function mockConnected($return = true, $times = null)
{
$this->checkMock();
$this->dbaMock
->shouldReceive('connected')
->times($times)
->andReturn($return);
}
/**
* Mocking DBA::fetchFirst()
*
* @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
*/
public function mockFetchFirst(string $arg, bool $return = true, int $times = null)
{
$this->checkMock();
$this->dbaMock
->shouldReceive('fetchFirst')
->with($arg)
->times($times)
->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()
*
* @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
*/
public function mockSelect(string $tableName, array $select = [], array $where = [], $return = null, int $times = null)
{
$this->checkMock();
$this->dbaMock
->shouldReceive('select')
->with($tableName, $select, $where)
->times($times)
->andReturn($return);
}
/**
* Mocking DBA::delete()
*
* @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
*/
public function mockDBADelete(string $tableName, array $where = [], bool $return = true, int $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(string $expTableName, array $expFields, array $expCondition, array $expOld_fields = [], bool $return = true, int $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(string $expTableName, array $expParam, bool $expOnDuplUpdate = false, bool $return = true, int $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(string $expTableName, array $expSelect = [], array $expWhere = [], array $return = [], int $times = null)
{
$this->checkMock();
$closure = function ($tableName, $select = [], $where = []) use ($expTableName, $expSelect, $expWhere) {
return $tableName === $expTableName
&& $select === $expSelect
&& $where === $expWhere;
};
$this->dbaMock
->shouldReceive('selectFirst')
->withArgs($closure)
->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)
{
$this->checkMock();
$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)
{
$this->checkMock();
$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)
{
$this->checkMock();
if (!isset($sql)) {
$this->dbaMock
->shouldReceive('p')
->times($times)
->andReturn($return);
} else {
$this->dbaMock
->shouldReceive('p')
->with($sql)
->times($times)
->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(string $table, bool $return = true, int $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

@ -1,77 +0,0 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Test\Util;
use Friendica\Database\DBStructure;
use Mockery\MockInterface;
/**
* Trait to mock the DBStructure connection status
*/
trait DBStructureMockTrait
{
/**
* @var MockInterface The mocking interface of Friendica\Database\DBStructure
*/
private $dbStructure;
/**
* Mocking DBStructure::update()
* @see DBStructure::update();
*
* @param array $args The arguments for the update call
* @param bool $return True, if the connect was successful, otherwise false
* @param null|int $times How often the method will get used
*/
public function mockUpdate(array $args = [], bool $return = true, int $times = null)
{
if (!isset($this->dbStructure)) {
$this->dbStructure = \Mockery::mock('alias:' . DBStructure::class);
}
$this->dbStructure
->shouldReceive('update')
->withArgs($args)
->times($times)
->andReturn($return);
}
/**
* Mocking DBStructure::existsTable()
*
* @param string $tableName The name of the table to check
* @param bool $return True, if the connect was successful, otherwise false
* @param null|int $times How often the method will get used
*/
public function mockExistsTable(string $tableName, bool $return = true, int $times = null)
{
if (!isset($this->dbStructure)) {
$this->dbStructure = \Mockery::mock('alias:' . DBStructure::class);
}
$this->dbStructure
->shouldReceive('existsTable')
->with($tableName)
->times($times)
->andReturn($return);
}
}

View file

@ -27,7 +27,6 @@ use Friendica\App\Module;
use Friendica\Core\Config\Cache;
use Friendica\Database\Database;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\DBAMockTrait;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\BasePath;
use Mockery;
@ -36,7 +35,6 @@ use Mockery\MockInterface;
class ModeTest extends MockedTest
{
use VFSTrait;
use DBAMockTrait;
/**
* @var BasePath|MockInterface
@ -53,7 +51,7 @@ class ModeTest extends MockedTest
*/
private $configCacheMock;
protected function setUp() : void
protected function setUp(): void
{
parent::setUp();

View file

@ -30,8 +30,6 @@ use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Test\Util\DBAMockTrait;
use Friendica\Test\Util\DBStructureMockTrait;
use Friendica\Test\Util\RendererMockTrait;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\Logger\VoidLogger;
@ -43,8 +41,6 @@ use org\bovigo\vfs\vfsStreamFile;
class AutomaticInstallationConsoleTest extends ConsoleTest
{
use VFSTrait;
use DBAMockTrait;
use DBStructureMockTrait;
use RendererMockTrait;
/**

View file

@ -21,26 +21,34 @@
namespace Friendica\Test\src\Model;
use Dice\Dice;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Model\User;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\DBAMockTrait;
use Mockery\MockInterface;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class UserTest extends MockedTest
{
use DBAMockTrait;
private $parent;
private $child;
private $manage;
/** @var Database|MockInterface */
private $dbMock;
protected function setUp(): void
{
parent::setUp();
$this->dbMock = \Mockery::mock(Database::class);
$diceMock = \Mockery::mock(Dice::class)->makePartial();
/** @var Dice|MockInterface $diceMock */
$diceMock = $diceMock->addRules(include __DIR__ . '/../../../static/dependencies.config.php');
$diceMock->shouldReceive('create')->withArgs([Database::class])->andReturn($this->dbMock);
DI::init($diceMock);
$this->parent = [
'uid' => 1,
'username' => 'maxmuster',
@ -62,13 +70,9 @@ class UserTest extends MockedTest
public function testIdentitiesEmpty()
{
$this->mockSelectFirst('user',
['uid', 'nickname', 'username', 'parent-uid'],
['uid' => $this->parent['uid']],
$this->parent,
1
);
$this->mockIsResult($this->parent, false, 1);
$this->dbMock->shouldReceive('selectFirst')->with('user',
['uid', 'nickname', 'username', 'parent-uid'],['uid' => $this->parent['uid']], [])->andReturn($this->parent)->once();
$this->dbMock->shouldReceive('isResult')->with($this->parent)->andReturn(false)->once();
$record = User::identities($this->parent['uid']);
@ -81,31 +85,24 @@ class UserTest extends MockedTest
$parentSelect['parent-uid'] = 0;
// Select the user itself (=parent)
$this->mockSelectFirst('user',
['uid', 'nickname', 'username', 'parent-uid'],
['uid' => $this->parent['uid']],
$parentSelect,
1
);
$this->mockIsResult($parentSelect, true, 1);
$this->dbMock->shouldReceive('selectFirst')->with('user',
['uid', 'nickname', 'username', 'parent-uid'],['uid' => $this->parent['uid']], [])->andReturn($parentSelect)->once();
$this->dbMock->shouldReceive('isResult')->with($parentSelect)->andReturn(true)->once();
// Select one child
$this->mockSelect('user',
$this->dbMock->shouldReceive('select')->with('user',
['uid', 'username', 'nickname'],
[
'parent-uid' => $this->parent['uid'],
'account_removed' => false
],
'objectReturn',
1
);
$this->mockIsResult('objectReturn', true, 1);
$this->mockToArray('objectReturn', [ $this->child ], 1);
], [])->andReturn('objectReturn')->once();
$this->dbMock->shouldReceive('isResult')->with('objectReturn')->andReturn(true)->once();
$this->dbMock->shouldReceive('toArray')->with('objectReturn', true, 0)->andReturn([$this->child])->once();
// Select the manage
$this->mockP(null, 'objectTwo', 1);
$this->mockIsResult('objectTwo', true, 1);
$this->mockToArray('objectTwo', [ $this->manage ], 1);
$this->dbMock->shouldReceive('p')->withAnyArgs()->andReturn('objectTwo')->once();
$this->dbMock->shouldReceive('isResult')->with('objectTwo')->andReturn(true)->once();
$this->dbMock->shouldReceive('toArray')->with('objectTwo', true, 0)->andReturn([$this->manage])->once();
$record = User::identities($this->parent['uid']);
@ -122,43 +119,33 @@ class UserTest extends MockedTest
$childSelect['parent-uid'] = $this->parent['uid'];
// Select the user itself (=child)
$this->mockSelectFirst('user',
['uid', 'nickname', 'username', 'parent-uid'],
['uid' => $this->child['uid']],
$childSelect,
1
);
$this->mockIsResult($childSelect, true, 1);
$this->dbMock->shouldReceive('selectFirst')->with('user',
['uid', 'nickname', 'username', 'parent-uid'],['uid' => $this->child['uid']], [])->andReturn($childSelect)->once();
$this->dbMock->shouldReceive('isResult')->with($childSelect)->andReturn(true)->once();
// Select the parent
$this->mockSelect('user',
$this->dbMock->shouldReceive('select')->with('user',
['uid', 'username', 'nickname'],
[
'uid' => $this->parent['uid'],
'account_removed' => false
],
'objectReturn',
1
);
$this->mockIsResult('objectReturn', true, 1);
$this->mockToArray('objectReturn', [ $this->parent ], 1);
], [])->andReturn('objectReturn')->once();
$this->dbMock->shouldReceive('isResult')->with('objectReturn')->andReturn(true)->once();
$this->dbMock->shouldReceive('toArray')->with('objectReturn', true, 0)->andReturn([$this->parent])->once();
// Select the childs (user & manage)
$this->mockSelect('user',
$this->dbMock->shouldReceive('select')->with('user',
['uid', 'username', 'nickname'],
[
'parent-uid' => $this->parent['uid'],
'account_removed' => false
],
'objectReturn',
1
);
$this->mockIsResult('objectReturn', true, 1);
$this->mockToArray('objectReturn', [ $this->child, $this->manage ], 1);
], [])->andReturn('objectReturn')->once();
$this->dbMock->shouldReceive('isResult')->with('objectReturn')->andReturn(true)->once();
$this->dbMock->shouldReceive('toArray')->with('objectReturn', true, 0)->andReturn([$this->child, $this->manage])->once();
// Select the manage
$this->mockP(null, 'objectTwo', 1);
$this->mockIsResult('objectTwo', false, 1);
$this->dbMock->shouldReceive('p')->withAnyArgs()->andReturn('objectTwo')->once();
$this->dbMock->shouldReceive('isResult')->with('objectTwo')->andReturn(false)->once();
$record = User::identities($this->child['uid']);