Refactoring identities to Model\User::identities

This commit is contained in:
Philipp Holzer 2018-11-08 00:22:15 +01:00
commit 8ad721988b
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
4 changed files with 337 additions and 44 deletions

View file

@ -69,4 +69,117 @@ trait DBAMockTrait
->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($tableName, $select = [], $where = [], $return = null, $times = null)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->dbaMock
->shouldReceive('select')
->with($tableName, $select, $where)
->times($times)
->andReturn($return);
}
/**
* Mocking DBA::selectFirst()
*
* @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 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)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$this->dbaMock
->shouldReceive('selectFirst')
->with($tableName, $select, $where)
->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)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$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)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
$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)
{
if (!isset($this->dbaMock)) {
$this->dbaMock = \Mockery::mock('alias:Friendica\Database\DBA');
}
if (!isset($sql)) {
$this->dbaMock
->shouldReceive('p')
->times($times)
->andReturn($return);
} else {
$this->dbaMock
->shouldReceive('p')
->with($sql)
->times($times)
->andReturn($return);
}
}
}

View file

@ -0,0 +1,148 @@
<?php
namespace Friendica\Test\Object;
use Friendica\Model\User;
use Friendica\Test\MockedTest;
use Friendica\Test\Util\DBAMockTrait;
class UserTest extends MockedTest
{
use DBAMockTrait;
private $parent;
private $child;
private $manage;
protected function setUp()
{
parent::setUp();
$this->parent = [
'uid' => 1,
'username' => 'maxmuster',
'nickname' => 'Max Muster'
];
$this->child = [
'uid' => 2,
'username' => 'johndoe',
'nickname' => 'John Doe'
];
$this->manage = [
'uid' => 3,
'username' => 'janesmith',
'nickname' => 'Jane Smith'
];
}
public function testIdentitiesEmpty()
{
$this->mockSelectFirst('user',
['uid', 'nickname', 'username', 'parent-uid'],
['uid' => $this->parent['uid']],
$this->parent,
1
);
$this->mockIsResult($this->parent, false, 1);
$record = User::identities($this->parent['uid']);
$this->assertEquals([], $record);
}
public function testIdentitiesAsParent()
{
$parentSelect = $this->parent;
$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);
// Select one child
$this->mockSelect('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);
// Select the manage
$this->mockP(null, 'objectTwo', 1);
$this->mockIsResult('objectTwo', true, 1);
$this->mockToArray('objectTwo', [ $this->manage ], 1);
$record = User::identities($this->parent['uid']);
$this->assertEquals([
$this->parent,
$this->child,
$this->manage
], $record);
}
public function testIdentitiesAsChild()
{
$childSelect = $this->child;
$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);
// Select the parent
$this->mockSelect('user',
['uid', 'username', 'nickname'],
[
'uid' => $this->parent['uid'],
'account_removed' => false
],
'objectReturn',
1
);
$this->mockIsResult('objectReturn', true, 1);
$this->mockToArray('objectReturn', [ $this->parent ], 1);
// Select the childs (user & manage)
$this->mockSelect('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);
// Select the manage
$this->mockP(null, 'objectTwo', 1);
$this->mockIsResult('objectTwo', false, 1);
$record = User::identities($this->child['uid']);
$this->assertEquals([
$this->parent,
$this->child,
$this->manage
], $record);
}
}