Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LEDfan committed Sep 16, 2017
1 parent a6977ba commit 10880a1
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 21 deletions.
3 changes: 2 additions & 1 deletion appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ public function __construct(array $urlParams=array()){
$container->registerService('RefreshRosterCommand', function($c) {
return new RefreshRoster(
$c->query('ServerContainer')->getUserManager(),
$c->query('RosterPush')
$c->query('RosterPush'),
$c->query('PresenceMapper')
);
});

Expand Down
10 changes: 9 additions & 1 deletion lib/command/refreshroster.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\OJSXC\Command;

use OCA\OJSXC\Db\PresenceMapper;
use OCA\OJSXC\RosterPush;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
Expand All @@ -22,13 +23,20 @@ class RefreshRoster extends Command
*/
private $rosterPush;

/**
* @var PresenceMapper
*/
private $presenceMapper;

public function __construct(
IUserManager $userManager,
RosterPush $rosterPush
RosterPush $rosterPush,
PresenceMapper $presenceMapper
) {
parent::__construct();
$this->userManager = $userManager;
$this->rosterPush = $rosterPush;
$this->presenceMapper = $presenceMapper;
}

protected function configure()
Expand Down
41 changes: 41 additions & 0 deletions tests/integration/db/PresenceMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,47 @@ protected function setUp()
$this->setValueOfPrivateProperty($this->mapper, 'connectedUsers', []);
$this->newContentContainer = $this->container->query('NewContentContainer');
$this->setValueOfPrivateProperty($this->newContentContainer, 'stanzas', []);
foreach (\OC::$server->getUserManager()->search('') as $user) {
$user->delete();
}
}

protected function tearDown() {
foreach (\OC::$server->getUserManager()->search('') as $user) {
$user->delete();
}

}

/**
* @before
* TODO explciclty call this function
*/
public function setupContactsStoreAPI() {
foreach (\OC::$server->getUserManager()->search('') as $user) {
$user->delete();
}

$users[] = \OC::$server->getUserManager()->createUser('admin', 'admin');
$users[] = \OC::$server->getUserManager()->createUser('derp', 'derp');
$users[] = \OC::$server->getUserManager()->createUser('derpina', 'derpina');
$users[] = \OC::$server->getUserManager()->createUser('herp', 'herp');
$users[] = \OC::$server->getUserManager()->createUser('foo', 'foo');

\OC_App::loadApp('dav');
$currentUser = \OC::$server->getUserManager()->createUser('autotest', 'autotest');
\OC::$server->getUserSession()->setUser($currentUser);
/** @var \OCA\DAV\CardDAV\SyncService $syncService */
$syncService = \OC::$server->query('CardDAVSyncService');
$syncService->getLocalSystemAddressBook();
$syncService->updateUser($currentUser);

foreach ($users as $user) {
$syncService->updateUser($user);
}

\OC::$server->getDatabaseConnection()->executeQuery("DELETE FROM *PREFIX*ojsxc_stanzas");

}

/**
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class HooksTest extends PHPUnit_Framework_TestCase
*/
private $stanzaMapper;

/**
* @var PHPUnit_Framework_MockObject_MockObject | \OCP\IGroupManager
*/
private $groupManager;

public function setUp()
{
$this->userManager = $this->getMockBuilder('OCP\IUserManager')->setMethods(['listen', 'registerBackend', 'getBackends', 'removeBackend', 'clearBackends', 'get', 'userExists', 'checkPassword', 'search', 'searchDisplayName', 'createUser', 'createUserFromBackend', 'countUsers', 'callForAllUsers', 'countDisabledUsers', 'countSeenUsers', 'callForSeenUsers', 'getByEmail'])->getMock();
Expand All @@ -57,12 +62,15 @@ public function setUp()

$this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->disableOriginalConstructor()->getMock();

$this->groupManager = $this->getMockBuilder('OCP\IGroupManager')->disableOriginalConstructor()->setMethods(['listen', 'isBackendUsed', 'addBackend', 'clearBackends', 'get', 'groupExists', 'createGroup', 'search', 'getUserGroups', 'getUserGroupIds', 'displayNamesInGroup', 'isAdmin', 'isInGroup'])->getMock();

$this->hooks = new Hooks(
$this->userManager,
$this->userSession,
$this->rosterPush,
$this->presenceMapper,
$this->stanzaMapper
$this->stanzaMapper,
$this->groupManager
);
}

Expand Down
20 changes: 14 additions & 6 deletions tests/unit/RosterPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class RosterPushTest extends PHPUnit_Framework_TestCase
*/
private $db;

/**
* @var \PHPUnit_Framework_MockObject_MockObject | IUserProvider
*/
private $userProvider;

public function setUp()
{
$this->userManager = $this->getMockBuilder('OCP\IUserManager')
Expand All @@ -51,12 +56,15 @@ public function setUp()
$this->db = $this->getMockBuilder('OCP\IDbConnection')
->disableOriginalConstructor()->getMock();

$this->userProvider = $this->getMockBuilder('OCA\OJSXC\IUserProvider')->disableOriginalConstructor()->getMock();

$this->rosterPush = new RosterPush(
$this->userManager,
$this->userSession,
'localhost',
$this->iqRosterPushMapper,
$this->db
$this->db,
$this->userProvider
);
}

Expand All @@ -65,7 +73,7 @@ public function testRefreshRoster()

/** @var \PHPUnit_Framework_MockObject_MockObject | RosterPush $rosterPush */
$rosterPush = $this->getMockBuilder('OCA\OJSXC\RosterPush')
->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db])
->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db, $this->userProvider])
->setMethods(['createOrUpdateRosterItem', 'removeRosterItem'])->getMock();

$user1 = $this->getMockBuilder('OCP\IUser')->getMock();
Expand Down Expand Up @@ -140,7 +148,7 @@ public function testRefreshRosterThrowsDuringRemove()

/** @var \PHPUnit_Framework_MockObject_MockObject | RosterPush $rosterPush */
$rosterPush = $this->getMockBuilder('OCA\OJSXC\RosterPush')
->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db])
->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db, $this->userProvider])
->setMethods(['createOrUpdateRosterItem', 'removeRosterItem'])->getMock();

$user1 = $this->getMockBuilder('OCP\IUser')->getMock();
Expand Down Expand Up @@ -218,7 +226,7 @@ public function testRemoveRosterItem()
public function testCreateOrUpdateRosterItem()
{
$user1 = $this->getMockBuilder('OCP\IUser')->getMock();
$user1->expects($this->exactly(5))
$user1->expects($this->exactly(6))
->method('getUID')
->willReturn('user1');
$user2 = $this->getMockBuilder('OCP\IUser')->getMock();
Expand All @@ -230,8 +238,8 @@ public function testCreateOrUpdateRosterItem()
->method('getUID')
->willReturn('user3');

$this->userManager->expects($this->once())
->method('search')
$this->userProvider->expects($this->once())
->method('getAllUsersForUserByUID')
->willReturn([$user1, $user2, $user3]);

$stanza1 = new IQRosterPush();
Expand Down
27 changes: 18 additions & 9 deletions tests/unit/stanzahandlers/IQTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace OCA\OJSXC\StanzaHandlers;

use OCA\OJSXC\Db\IQRoster;
use OCA\OJSXC\IUserProvider;
use OCA\OJSXC\User;
use OCP\IConfig;
use OCP\IUserManager;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;

Expand All @@ -16,7 +19,7 @@ class IQTest extends PHPUnit_Framework_TestCase
private $iq;

/**
* @var PHPUnit_Framework_MockObject_MockObject
* @var PHPUnit_Framework_MockObject_MockObject | IUserManager
*/
private $userManager;

Expand All @@ -35,31 +38,38 @@ class IQTest extends PHPUnit_Framework_TestCase
*/
private $config;


/**
* @var \PHPUnit_Framework_MockObject_MockObject | IUserProvider
*/
private $userProvider;

public function setUp()
{
$this->host = 'localhost';
$this->userId = 'john';
$this->userManager = $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock();
$this->config = $this->getMockBuilder('OCP\IConfig')->disableOriginalConstructor()->getMock();
$this->iq = new IQ($this->userId, $this->host, $this->userManager, $this->config);
$this->userProvider = $this->getMockBuilder('OCA\OJSXC\IUserProvider')->disableOriginalConstructor()->getMock();
$this->iq = new IQ($this->userId, $this->host, $this->userManager, $this->config, $this->userProvider);
}

public function iqRosterProvider()
{
$user1 = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock();
$user1 = $this->getMockBuilder(User::class)->disableOriginalConstructor()->getMock();
$user1->expects($this->any())
->method('getUID')
->will($this->returnValue('john'));
$user1->expects($this->any())
->method('getDisplayName')
->method('getFullName')
->will($this->returnValue('John'));

$user2 = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock();
$user2 = $this->getMockBuilder(User::class)->disableOriginalConstructor()->getMock();
$user2->expects($this->any())
->method('getUID')
->will($this->returnValue('richard'));
$user2->expects($this->any())
->method('getDisplayName')
->method(getFullName)
->will($this->returnValue('Richard'));


Expand Down Expand Up @@ -156,9 +166,8 @@ public function testIqRoster(array $stanza, array $users, $searchCount, $expecte
->with('debug')
->will($this->returnValue(false));

$this->userManager->expects($searchCount)
->method('search')
->with('')
$this->userProvider->expects($searchCount)
->method('getAllUsers')
->will($this->returnValue($users));

$result = $this->iq->handle($stanza);
Expand Down
19 changes: 16 additions & 3 deletions tests/unit/stanzahandlers/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace OCA\OJSXC\StanzaHandlers;

use OCA\OJSXC\Db\Message as MessageEntity;
use OCA\OJSXC\Db\MessageMapper;
use OCA\OJSXC\IUserProvider;
use PHPUnit_Framework_TestCase;
use PHPUnit_Framework_MockObject_MockObject;

Expand All @@ -15,7 +17,7 @@ class MessageTest extends PHPUnit_Framework_TestCase
private $message;

/**
* @var PHPUnit_Framework_MockObject_MockObject
* @var PHPUnit_Framework_MockObject_MockObject | MessageMapper
*/
private $messageMapper;

Expand All @@ -25,16 +27,22 @@ class MessageTest extends PHPUnit_Framework_TestCase
private $userId;

/**
* @var string $host ;
* @var string $host
*/
private $host;

/**
* @var PHPUnit_Framework_MockObject_MockObject | IUserProvider
*/
private $userProvider;

public function setUp()
{
$this->host = 'localhost';
$this->userId = 'john';
$this->messageMapper = $this->getMockBuilder('OCA\OJSXC\Db\MessageMapper')->disableOriginalConstructor()->getMock();
$this->message = new Message($this->userId, $this->host, $this->messageMapper);
$this->userProvider = $this->getMockBuilder('OCA\OJSXC\IUserProvider')->disableOriginalConstructor()->getMock();
$this->message = new Message($this->userId, $this->host, $this->messageMapper, $this->userProvider);
}

public function messageProvider()
Expand Down Expand Up @@ -87,6 +95,11 @@ public function testMessage(array $stanza, $expected)
->method('insert')
->with($expected);

$this->userProvider->expects($this->once())
->method('hasUserByUID')
->with('derp')
->willReturn(true); // TODO test return false

$this->message->handle($stanza);
}
}

0 comments on commit 10880a1

Please sign in to comment.