Skip to content

Commit

Permalink
Add and fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Dec 3, 2020
1 parent 63aeea7 commit 4640547
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions tests/lib/Accounts/AccountsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OC\Accounts\AccountManager;
use OCP\Accounts\IAccountManager;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
Expand All @@ -43,6 +44,9 @@ class AccountsManagerTest extends TestCase {
/** @var \OCP\IDBConnection */
private $connection;

/** @var IConfig|MockObject */
private $config;

/** @var EventDispatcherInterface|MockObject */
private $eventDispatcher;

Expand All @@ -59,6 +63,7 @@ protected function setUp(): void {
parent::setUp();
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->connection = \OC::$server->getDatabaseConnection();
$this->config = $this->createMock(IConfig::class);
$this->jobList = $this->createMock(IJobList::class);
$this->logger = $this->createMock(LoggerInterface::class);
}
Expand All @@ -77,7 +82,13 @@ protected function tearDown(): void {
*/
public function getInstance($mockedMethods = null) {
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)
->getMock();
}
Expand Down Expand Up @@ -187,9 +198,9 @@ public function dataTestGetUser() {

public function testUpdateExistingUser() {
$user = $this->getMockBuilder(IUser::class)->getMock();
$user->expects($this->once())->method('getUID')->willReturn('uid');
$oldData = ['key' => 'value'];
$newData = ['newKey' => 'newValue'];
$user->expects($this->atLeastOnce())->method('getUID')->willReturn('uid');
$oldData = ['key' => ['value' => 'value']];
$newData = ['newKey' => ['value' => 'newValue']];

$accountManager = $this->getInstance();
$this->addDummyValuesToTable('uid', $oldData);
Expand All @@ -201,10 +212,10 @@ public function testUpdateExistingUser() {
public function testInsertNewUser() {
$user = $this->getMockBuilder(IUser::class)->getMock();
$uid = 'uid';
$data = ['key' => 'value'];
$data = ['key' => ['value' => 'value']];

$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->invokePrivate($accountManager, 'insertNewUser', [$user, $data]);

Expand Down Expand Up @@ -293,4 +304,32 @@ public function testGetAccount() {
->willReturn($data);
$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]));
}
}
}

0 comments on commit 4640547

Please sign in to comment.