From 71bb9bcdaebf91996548a7a7c5c4f6316077b2d1 Mon Sep 17 00:00:00 2001 From: zak39 Date: Wed, 13 Nov 2024 15:33:37 +0100 Subject: [PATCH] test(php): Add more dependencies for UserService and specify MockObject type --- tests/Unit/Service/UserServiceTest.php | 46 +++++++++++++++++--------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/tests/Unit/Service/UserServiceTest.php b/tests/Unit/Service/UserServiceTest.php index 522c94ed4..6f3ec85b0 100644 --- a/tests/Unit/Service/UserServiceTest.php +++ b/tests/Unit/Service/UserServiceTest.php @@ -25,27 +25,31 @@ namespace OCA\Workspace\Tests\Unit\Service; +use OCA\Workspace\Service\Group\ConnectedGroupsService; use OCA\Workspace\Service\Group\ManagersWorkspace; use OCA\Workspace\Service\UserService; -use OCA\Workspace\Service\WorkspaceService; use OCP\IGroup; use OCP\IGroupManager; +use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserSession; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; +use PHPUnit\Framework\MockObject\MockObject; class UserServiceTest extends TestCase { - private IGroupManager $groupManager; - private IUser $user; - private IUserSession $userSession; - private LoggerInterface $logger; - private WorkspaceService $workspaceService; + private MockObject&IGroupManager $groupManager; + private MockObject&IUser $user; + private MockObject&IUserSession $userSession; + private MockObject&LoggerInterface $logger; + private MockObject&ConnectedGroupsService $connectedGroupService; + private MockObject&IURLGenerator $urlGenerator; public function setUp(): void { $this->groupManager = $this->createMock(IGroupManager::class); $this->logger = $this->createMock(LoggerInterface::class); - $this->workspaceService = $this->createMock(WorkspaceService::class); + $this->connectedGroupService = $this->createMock(ConnectedGroupsService::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); // Sets up the user'session $this->userSession = $this->createMock(IUserSession::class); @@ -55,7 +59,7 @@ public function setUp(): void { ->willReturn($this->user); } - private function createTestUser($id, $name, $email): IUser { + private function createTestUser($id, $name, $email): MockObject&IUser { $mockUser = $this->createMock(IUser::class); $mockUser->expects($this->any()) ->method('getUID') @@ -69,7 +73,7 @@ private function createTestUser($id, $name, $email): IUser { return $mockUser; } - private function createTestGroup($id, $name, $users): IGroup { + private function createTestGroup($id, $name, $users): MockObject&IGroup { $mockGroup = $this->createMock(IGroup::class); $mockGroup->expects($this->any()) ->method('getGID') @@ -103,7 +107,9 @@ public function testIsUserGeneralAdmin(): void { $userService = new UserService( $this->groupManager, $this->userSession, - $this->logger); + $this->logger, + $this->connectedGroupService, + $this->urlGenerator); // Runs the method to be tested $result = $userService->isUserGeneralAdmin(); @@ -131,7 +137,9 @@ public function testIsNotUserGeneralAdmin(): void { $userService = new UserService( $this->groupManager, $this->userSession, - $this->logger); + $this->logger, + $this->connectedGroupService, + $this->urlGenerator); // Runs the method to be tested $result = $userService->isUserGeneralAdmin(); @@ -160,7 +168,9 @@ public function testIsSpaceManager(): void { $userService = new UserService( $this->groupManager, $this->userSession, - $this->logger); + $this->logger, + $this->connectedGroupService, + $this->urlGenerator); $this->userSession->expects($this->once()) ->method('getUser') @@ -199,7 +209,9 @@ public function testIsNotSpaceManager(): void { $userService = new UserService( $this->groupManager, $this->userSession, - $this->logger); + $this->logger, + $this->connectedGroupService, + $this->urlGenerator); // Runs the method to be tested $result = $userService->isSpaceManager(); @@ -228,7 +240,9 @@ public function testIsNotSpaceManagerOfSpace(): void { $userService = new UserService( $this->groupManager, $this->userSession, - $this->logger); + $this->logger, + $this->connectedGroupService, + $this->urlGenerator); // Runs the method to be tested $result = $userService->isSpaceManagerOfSpace([ @@ -262,7 +276,9 @@ public function testIsSpaceManagerOfSpace(): void { $userService = new UserService( $this->groupManager, $this->userSession, - $this->logger); + $this->logger, + $this->connectedGroupService, + $this->urlGenerator); // Runs the method to be tested $result = $userService->isSpaceManagerOfSpace([