Add and fix unit tests

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-12-03 11:51:42 +01:00
parent 46b073d7ce
commit f0ca76aefb
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
1 changed files with 45 additions and 6 deletions

View File

@ -25,6 +25,7 @@ use OC\Accounts\Account;
use OC\Accounts\AccountManager; use OC\Accounts\AccountManager;
use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountManager;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IUser; use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -43,6 +44,9 @@ class AccountsManagerTest extends TestCase {
/** @var \OCP\IDBConnection */ /** @var \OCP\IDBConnection */
private $connection; private $connection;
/** @var IConfig|MockObject */
private $config;
/** @var EventDispatcherInterface|MockObject */ /** @var EventDispatcherInterface|MockObject */
private $eventDispatcher; private $eventDispatcher;
@ -59,6 +63,7 @@ class AccountsManagerTest extends TestCase {
parent::setUp(); parent::setUp();
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->connection = \OC::$server->getDatabaseConnection(); $this->connection = \OC::$server->getDatabaseConnection();
$this->config = $this->createMock(IConfig::class);
$this->jobList = $this->createMock(IJobList::class); $this->jobList = $this->createMock(IJobList::class);
$this->logger = $this->createMock(LoggerInterface::class); $this->logger = $this->createMock(LoggerInterface::class);
} }
@ -77,7 +82,13 @@ class AccountsManagerTest extends TestCase {
*/ */
public function getInstance($mockedMethods = null) { public function getInstance($mockedMethods = null) {
return $this->getMockBuilder(AccountManager::class) return $this->getMockBuilder(AccountManager::class)
->setConstructorArgs([$this->connection, $this->eventDispatcher, $this->jobList, $this->logger]) ->setConstructorArgs([
$this->connection,
$this->config,
$this->eventDispatcher,
$this->jobList,
$this->logger,
])
->setMethods($mockedMethods) ->setMethods($mockedMethods)
->getMock(); ->getMock();
} }
@ -187,9 +198,9 @@ class AccountsManagerTest extends TestCase {
public function testUpdateExistingUser() { public function testUpdateExistingUser() {
$user = $this->getMockBuilder(IUser::class)->getMock(); $user = $this->getMockBuilder(IUser::class)->getMock();
$user->expects($this->once())->method('getUID')->willReturn('uid'); $user->expects($this->atLeastOnce())->method('getUID')->willReturn('uid');
$oldData = ['key' => 'value']; $oldData = ['key' => ['value' => 'value']];
$newData = ['newKey' => 'newValue']; $newData = ['newKey' => ['value' => 'newValue']];
$accountManager = $this->getInstance(); $accountManager = $this->getInstance();
$this->addDummyValuesToTable('uid', $oldData); $this->addDummyValuesToTable('uid', $oldData);
@ -201,10 +212,10 @@ class AccountsManagerTest extends TestCase {
public function testInsertNewUser() { public function testInsertNewUser() {
$user = $this->getMockBuilder(IUser::class)->getMock(); $user = $this->getMockBuilder(IUser::class)->getMock();
$uid = 'uid'; $uid = 'uid';
$data = ['key' => 'value']; $data = ['key' => ['value' => 'value']];
$accountManager = $this->getInstance(); $accountManager = $this->getInstance();
$user->expects($this->once())->method('getUID')->willReturn($uid); $user->expects($this->atLeastOnce())->method('getUID')->willReturn($uid);
$this->assertNull($this->getDataFromTable($uid)); $this->assertNull($this->getDataFromTable($uid));
$this->invokePrivate($accountManager, 'insertNewUser', [$user, $data]); $this->invokePrivate($accountManager, 'insertNewUser', [$user, $data]);
@ -293,4 +304,32 @@ class AccountsManagerTest extends TestCase {
->willReturn($data); ->willReturn($data);
$this->assertEquals($expected, $accountManager->getAccount($user)); $this->assertEquals($expected, $accountManager->getAccount($user));
} }
public function dataParsePhoneNumber(): array {
return [
['0711 / 25 24 28-90', 'DE', '+4971125242890'],
['0711 / 25 24 28-90', '', null],
['+49 711 / 25 24 28-90', '', '+4971125242890'],
];
}
/**
* @dataProvider dataParsePhoneNumber
* @param string $phoneInput
* @param string $defaultRegion
* @param string|null $phoneNumber
*/
public function testParsePhoneNumber(string $phoneInput, string $defaultRegion, ?string $phoneNumber): void {
$this->config->method('getSystemValueString')
->willReturn($defaultRegion);
$instance = $this->getInstance();
if ($phoneNumber === null) {
$this->expectException(\InvalidArgumentException::class);
self::invokePrivate($instance, 'parsePhoneNumber', [$phoneInput]);
} else {
self::assertEquals($phoneNumber, self::invokePrivate($instance, 'parsePhoneNumber', [$phoneInput]));
}
}
} }