diff options
Diffstat (limited to 'apps/provisioning_api/tests/Controller')
4 files changed, 96 insertions, 180 deletions
diff --git a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php index 41739b6283f..3d2c1645d25 100644 --- a/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php +++ b/apps/provisioning_api/tests/Controller/AppConfigControllerTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -30,7 +32,6 @@ use function json_encode; * @package OCA\Provisioning_API\Tests */ class AppConfigControllerTest extends TestCase { - private IAppConfig&MockObject $appConfig; private IUserSession&MockObject $userSession; private IL10N&MockObject $l10n; @@ -51,7 +52,7 @@ class AppConfigControllerTest extends TestCase { /** * @param string[] $methods - * @return AppConfigController|\PHPUnit\Framework\MockObject\MockObject + * @return AppConfigController|MockObject */ protected function getInstance(array $methods = []) { $request = $this->createMock(IRequest::class); @@ -79,7 +80,7 @@ class AppConfigControllerTest extends TestCase { $this->settingManager, $this->appManager, ]) - ->setMethods($methods) + ->onlyMethods($methods) ->getMock(); } } @@ -95,7 +96,7 @@ class AppConfigControllerTest extends TestCase { $this->assertEquals(['data' => ['apps']], $result->getData()); } - public function dataGetKeys() { + public static function dataGetKeys(): array { return [ ['app1 ', null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN], ['app2', ['keys'], null, Http::STATUS_OK], @@ -104,12 +105,8 @@ class AppConfigControllerTest extends TestCase { /** * @dataProvider dataGetKeys - * @param string $app - * @param array|null $keys - * @param \Exception|null $throws - * @param int $status */ - public function testGetKeys($app, $keys, $throws, $status): void { + public function testGetKeys(string $app, ?array $keys, ?\Throwable $throws, int $status): void { $api = $this->getInstance(['verifyAppId']); if ($throws instanceof \Exception) { $api->expects($this->once()) @@ -140,7 +137,7 @@ class AppConfigControllerTest extends TestCase { } } - public function dataGetValue() { + public static function dataGetValue(): array { return [ ['app1', 'key', 'default', null, new \InvalidArgumentException('error'), Http::STATUS_FORBIDDEN], ['app2', 'key', 'default', 'return', null, Http::STATUS_OK], @@ -149,14 +146,8 @@ class AppConfigControllerTest extends TestCase { /** * @dataProvider dataGetValue - * @param string $app - * @param string|null $key - * @param string|null $default - * @param string|null $return - * @param \Exception|null $throws - * @param int $status */ - public function testGetValue($app, $key, $default, $return, $throws, $status): void { + public function testGetValue(string $app, string $key, string $default, ?string $return, ?\Throwable $throws, int $status): void { $api = $this->getInstance(['verifyAppId']); if ($throws instanceof \Exception) { $api->expects($this->once()) @@ -184,7 +175,7 @@ class AppConfigControllerTest extends TestCase { } } - public function dataSetValue() { + public static function dataSetValue(): array { return [ ['app1', 'key', 'default', new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN], ['app2', 'key', 'default', null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN], @@ -201,14 +192,8 @@ class AppConfigControllerTest extends TestCase { /** * @dataProvider dataSetValue - * @param string $app - * @param string|null $key - * @param string|null $value - * @param \Exception|null $appThrows - * @param \Exception|null $keyThrows - * @param int|\Throwable $status */ - public function testSetValue($app, $key, $value, $appThrows, $keyThrows, $status, int|\Throwable $type = IAppConfig::VALUE_MIXED): void { + public function testSetValue(string $app, string $key, string $value, ?\Throwable $appThrows, ?\Throwable $keyThrows, int $status, int|\Throwable $type = IAppConfig::VALUE_MIXED): void { $adminUser = $this->createMock(IUser::class); $adminUser->expects($this->once()) ->method('getUid') @@ -297,7 +282,7 @@ class AppConfigControllerTest extends TestCase { } } - public function dataDeleteValue() { + public static function dataDeleteValue(): array { return [ ['app1', 'key', new \InvalidArgumentException('error1'), null, Http::STATUS_FORBIDDEN], ['app2', 'key', null, new \InvalidArgumentException('error2'), Http::STATUS_FORBIDDEN], @@ -307,13 +292,8 @@ class AppConfigControllerTest extends TestCase { /** * @dataProvider dataDeleteValue - * @param string $app - * @param string|null $key - * @param \Exception|null $appThrows - * @param \Exception|null $keyThrows - * @param int $status */ - public function testDeleteValue($app, $key, $appThrows, $keyThrows, $status): void { + public function testDeleteValue(string $app, string $key, ?\Throwable $appThrows, ?\Throwable $keyThrows, int $status): void { $api = $this->getInstance(['verifyAppId', 'verifyConfigKey']); if ($appThrows instanceof \Exception) { $api->expects($this->once()) @@ -367,7 +347,7 @@ class AppConfigControllerTest extends TestCase { $this->addToAssertionCount(1); } - public function dataVerifyAppIdThrows() { + public static function dataVerifyAppIdThrows(): array { return [ ['activity..'], ['activity/'], @@ -378,16 +358,15 @@ class AppConfigControllerTest extends TestCase { /** * @dataProvider dataVerifyAppIdThrows - * @param string $app */ - public function testVerifyAppIdThrows($app): void { + public function testVerifyAppIdThrows(string $app): void { $this->expectException(\InvalidArgumentException::class); $api = $this->getInstance(); $this->invokePrivate($api, 'verifyAppId', [$app]); } - public function dataVerifyConfigKey() { + public static function dataVerifyConfigKey(): array { return [ ['activity', 'abc', ''], ['dav', 'public_route', ''], @@ -398,17 +377,14 @@ class AppConfigControllerTest extends TestCase { /** * @dataProvider dataVerifyConfigKey - * @param string $app - * @param string $key - * @param string $value */ - public function testVerifyConfigKey($app, $key, $value): void { + public function testVerifyConfigKey(string $app, string $key, string $value): void { $api = $this->getInstance(); $this->invokePrivate($api, 'verifyConfigKey', [$app, $key, $value]); $this->addToAssertionCount(1); } - public function dataVerifyConfigKeyThrows() { + public static function dataVerifyConfigKeyThrows(): array { return [ ['activity', 'installed_version', ''], ['calendar', 'enabled', ''], @@ -424,11 +400,8 @@ class AppConfigControllerTest extends TestCase { /** * @dataProvider dataVerifyConfigKeyThrows - * @param string $app - * @param string $key - * @param string $value */ - public function testVerifyConfigKeyThrows($app, $key, $value): void { + public function testVerifyConfigKeyThrows(string $app, string $key, string $value): void { $this->expectException(\InvalidArgumentException::class); $api = $this->getInstance(); diff --git a/apps/provisioning_api/tests/Controller/AppsControllerTest.php b/apps/provisioning_api/tests/Controller/AppsControllerTest.php index bbcabfddd8b..f56be7c4c36 100644 --- a/apps/provisioning_api/tests/Controller/AppsControllerTest.php +++ b/apps/provisioning_api/tests/Controller/AppsControllerTest.php @@ -24,12 +24,9 @@ use OCP\Server; * @package OCA\Provisioning_API\Tests */ class AppsControllerTest extends TestCase { - /** @var IAppManager */ - private $appManager; - /** @var AppsController */ - private $api; - /** @var IUserSession */ - private $userSession; + private IAppManager $appManager; + private AppsController $api; + private IUserSession $userSession; protected function setUp(): void { parent::setUp(); @@ -38,9 +35,7 @@ class AppsControllerTest extends TestCase { $this->groupManager = Server::get(IGroupManager::class); $this->userSession = Server::get(IUserSession::class); - $request = $this->getMockBuilder(IRequest::class) - ->disableOriginalConstructor() - ->getMock(); + $request = $this->createMock(IRequest::class); $this->api = new AppsController( 'provisioning_api', @@ -96,7 +91,7 @@ class AppsControllerTest extends TestCase { $this->assertEquals(count($disabled), count($data['apps'])); } - + public function testGetAppsInvalidFilter(): void { $this->expectException(OCSException::class); $this->expectExceptionCode(101); diff --git a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php index 29b098429e8..e4f8b3af183 100644 --- a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php +++ b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php @@ -22,30 +22,20 @@ use OCP\IUserManager; use OCP\IUserSession; use OCP\L10N\IFactory; use OCP\UserInterface; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; class GroupsControllerTest extends \Test\TestCase { - /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */ - protected $request; - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $userManager; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupManager; - /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ - protected $userSession; - /** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $accountManager; - /** @var ISubAdmin|\PHPUnit\Framework\MockObject\MockObject */ - protected $subAdminManager; - /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */ - protected $l10nFactory; - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ - protected $logger; - - /** @var GroupsController|\PHPUnit\Framework\MockObject\MockObject */ - protected $api; + protected IRequest&MockObject $request; + protected IUserManager&MockObject $userManager; + protected IConfig&MockObject $config; + protected Manager&MockObject $groupManager; + protected IUserSession&MockObject $userSession; + protected IAccountManager&MockObject $accountManager; + protected ISubAdmin&MockObject $subAdminManager; + protected IFactory&MockObject $l10nFactory; + protected LoggerInterface&MockObject $logger; + protected GroupsController&MockObject $api; private IRootFolder $rootFolder; @@ -82,16 +72,12 @@ class GroupsControllerTest extends \Test\TestCase { $this->rootFolder, $this->logger ]) - ->setMethods(['fillStorageInfo']) + ->onlyMethods(['fillStorageInfo']) ->getMock(); } - /** - * @param string $gid - * @return IGroup|\PHPUnit\Framework\MockObject\MockObject - */ - private function createGroup($gid) { - $group = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock(); + private function createGroup(string $gid): IGroup&MockObject { + $group = $this->createMock(\OCP\IGroup::class); $group ->method('getGID') ->willReturn($gid); @@ -116,7 +102,7 @@ class GroupsControllerTest extends \Test\TestCase { /** * @param string $uid - * @return IUser|\PHPUnit\Framework\MockObject\MockObject + * @return IUser&MockObject */ private function createUser($uid) { $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock(); @@ -165,7 +151,7 @@ class GroupsControllerTest extends \Test\TestCase { }); } - public function dataGetGroups() { + public static function dataGetGroups(): array { return [ [null, 0, 0], ['foo', 0, 0], @@ -177,12 +163,8 @@ class GroupsControllerTest extends \Test\TestCase { /** * @dataProvider dataGetGroups - * - * @param string|null $search - * @param int|null $limit - * @param int|null $offset */ - public function testGetGroups($search, $limit, $offset): void { + public function testGetGroups(?string $search, int $limit, int $offset): void { $groups = [$this->createGroup('group1'), $this->createGroup('group2')]; $search = $search === null ? '' : $search; @@ -509,7 +491,7 @@ class GroupsControllerTest extends \Test\TestCase { ->method('getUserGroups') ->willReturn([$group]); - /** @var \PHPUnit\Framework\MockObject\MockObject */ + /** @var MockObject */ $this->subAdminManager->expects($this->any()) ->method('isSubAdminOfGroup') ->willReturn(false); @@ -554,7 +536,7 @@ class GroupsControllerTest extends \Test\TestCase { ->method('getUserGroups') ->willReturn([$group]); - /** @var \PHPUnit\Framework\MockObject\MockObject */ + /** @var MockObject */ $this->subAdminManager->expects($this->any()) ->method('isSubAdminOfGroup') ->willReturn(false); diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index 7d4f99356b3..80d6d0f6152 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -46,41 +46,24 @@ use RuntimeException; use Test\TestCase; class UsersControllerTest extends TestCase { - /** @var IUserManager|MockObject */ - protected $userManager; - /** @var IConfig|MockObject */ - protected $config; - /** @var Manager|MockObject */ - protected $groupManager; - /** @var IUserSession|MockObject */ - protected $userSession; - /** @var LoggerInterface|MockObject */ - protected $logger; - /** @var UsersController|MockObject */ - protected $api; - /** @var IAccountManager|MockObject */ - protected $accountManager; - /** @var ISubAdmin|MockObject */ - protected $subAdminManager; - /** @var IURLGenerator|MockObject */ - protected $urlGenerator; - /** @var IRequest|MockObject */ - protected $request; - /** @var IFactory|MockObject */ - private $l10nFactory; - /** @var NewUserMailHelper|MockObject */ - private $newUserMailHelper; - /** @var ISecureRandom|MockObject */ - private $secureRandom; - /** @var RemoteWipe|MockObject */ - private $remoteWipe; - /** @var KnownUserService|MockObject */ - private $knownUserService; - /** @var IEventDispatcher|MockObject */ - private $eventDispatcher; + protected IUserManager&MockObject $userManager; + protected IConfig&MockObject $config; + protected Manager&MockObject $groupManager; + protected IUserSession&MockObject $userSession; + protected LoggerInterface&MockObject $logger; + protected UsersController&MockObject $api; + protected IAccountManager&MockObject $accountManager; + protected ISubAdmin&MockObject $subAdminManager; + protected IURLGenerator&MockObject $urlGenerator; + protected IRequest&MockObject $request; + private IFactory&MockObject $l10nFactory; + private NewUserMailHelper&MockObject $newUserMailHelper; + private ISecureRandom&MockObject $secureRandom; + private RemoteWipe&MockObject $remoteWipe; + private KnownUserService&MockObject $knownUserService; + private IEventDispatcher&MockObject $eventDispatcher; private IRootFolder $rootFolder; - /** @var IPhoneNumberUtil */ - private $phoneNumberUtil; + private IPhoneNumberUtil $phoneNumberUtil; protected function setUp(): void { parent::setUp(); @@ -449,10 +432,6 @@ class UsersControllerTest extends TestCase { $this->groupManager ->expects($this->exactly(2)) ->method('groupExists') - ->withConsecutive( - ['ExistingGroup'], - ['NonExistingGroup'] - ) ->willReturnMap([ ['ExistingGroup', true], ['NonExistingGroup', false] @@ -798,18 +777,20 @@ class UsersControllerTest extends TestCase { ->method('get') ->with('ExistingGroup') ->willReturn($group); + + $calls = [ + ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']], + ['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']], + ]; $this->logger ->expects($this->exactly(2)) ->method('info') - ->withConsecutive( - ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']], - ['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']] - ); + ->willReturnCallback(function () use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); - $this->assertTrue(key_exists( - 'id', - $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup'])->getData() - )); + $this->assertArrayHasKey('id', $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup'])->getData()); } @@ -966,11 +947,10 @@ class UsersControllerTest extends TestCase { $this->groupManager ->expects($this->exactly(2)) ->method('groupExists') - ->withConsecutive( - ['ExistingGroup1'], - ['ExistingGroup2'] - ) - ->willReturn(true); + ->willReturnMap([ + ['ExistingGroup1', true], + ['ExistingGroup2', true] + ]); $user = $this->getMockBuilder(IUser::class) ->disableOriginalConstructor() ->getMock(); @@ -996,24 +976,23 @@ class UsersControllerTest extends TestCase { $this->groupManager ->expects($this->exactly(4)) ->method('get') - ->withConsecutive( - ['ExistingGroup1'], - ['ExistingGroup2'], - ['ExistingGroup1'], - ['ExistingGroup2'] - ) ->willReturnMap([ ['ExistingGroup1', $existingGroup1], ['ExistingGroup2', $existingGroup2] ]); + + $calls = [ + ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']], + ['Added userid NewUser to group ExistingGroup1', ['app' => 'ocs_api']], + ['Added userid NewUser to group ExistingGroup2', ['app' => 'ocs_api']], + ]; $this->logger ->expects($this->exactly(3)) ->method('info') - ->withConsecutive( - ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']], - ['Added userid NewUser to group ExistingGroup1', ['app' => 'ocs_api']], - ['Added userid NewUser to group ExistingGroup2', ['app' => 'ocs_api']] - ); + ->willReturnCallback(function () use (&$calls) { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $subAdminManager = $this->getMockBuilder('OC\SubAdmin') ->disableOriginalConstructor()->getMock(); $this->groupManager @@ -1023,16 +1002,12 @@ class UsersControllerTest extends TestCase { $subAdminManager ->expects($this->exactly(2)) ->method('isSubAdminOfGroup') - ->withConsecutive( - [$loggedInUser, $existingGroup1], - [$loggedInUser, $existingGroup2] - ) - ->willReturn(true); + ->willReturnMap([ + [$loggedInUser, $existingGroup1, true], + [$loggedInUser, $existingGroup2, true], + ]); - $this->assertTrue(key_exists( - 'id', - $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup1', 'ExistingGroup2'])->getData() - )); + $this->assertArrayHasKey('id', $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup1', 'ExistingGroup2'])->getData()); } @@ -1541,7 +1516,7 @@ class UsersControllerTest extends TestCase { $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID'])); } - public function dataSearchByPhoneNumbers(): array { + public static function dataSearchByPhoneNumbers(): array { return [ 'Invalid country' => ['Not a country code', ['12345' => ['NaN']], 400, null, null, []], 'No number to search' => ['DE', ['12345' => ['NaN']], 200, null, null, []], @@ -1556,10 +1531,6 @@ class UsersControllerTest extends TestCase { /** * @dataProvider dataSearchByPhoneNumbers - * @param string $location - * @param array $search - * @param int $status - * @param array $expected */ public function testSearchByPhoneNumbers(string $location, array $search, int $status, ?array $searchUsers, ?array $userMatches, array $expected): void { $knownTo = 'knownTo'; @@ -1870,7 +1841,7 @@ class UsersControllerTest extends TestCase { $this->api->editUser('UserToEdit', 'email', 'demo.org'); } - public function selfEditChangePropertyProvider() { + public static function selfEditChangePropertyProvider(): array { return [ [IAccountManager::PROPERTY_TWITTER, '@oldtwitter', '@newtwitter'], [IAccountManager::PROPERTY_FEDIVERSE, '@oldFediverse@floss.social', '@newFediverse@floss.social'], @@ -2294,7 +2265,7 @@ class UsersControllerTest extends TestCase { $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'de')->getData()); } - public function dataEditUserSelfEditChangeLanguageButForced() { + public static function dataEditUserSelfEditChangeLanguageButForced(): array { return [ ['de'], [true], @@ -3942,11 +3913,10 @@ class UsersControllerTest extends TestCase { $api->expects($this->exactly(2)) ->method('getUserData') - ->withConsecutive( - ['uid', false], - ['currentuser', true], - ) - ->willReturn($expected); + ->willReturnMap([ + ['uid', false, $expected], + ['currentuser', true, $expected], + ]); $this->assertSame($expected, $api->getUser('uid')->getData()); @@ -4263,7 +4233,7 @@ class UsersControllerTest extends TestCase { } - public function dataGetEditableFields() { + public static function dataGetEditableFields(): array { return [ [false, true, ISetDisplayNameBackend::class, [ IAccountManager::PROPERTY_EMAIL, @@ -4388,10 +4358,6 @@ class UsersControllerTest extends TestCase { /** * @dataProvider dataGetEditableFields - * - * @param bool $allowedToChangeDisplayName - * @param string $userBackend - * @param array $expected */ public function testGetEditableFields(bool $allowedToChangeDisplayName, bool $allowedToChangeEmail, string $userBackend, array $expected): void { $this->config->method('getSystemValue')->willReturnCallback(fn (string $key, mixed $default) => match ($key) { |