From 9e6a97fb8a190fe48e9c72de3bcbd673c14afcf7 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Thu, 19 Sep 2024 22:15:04 +0200 Subject: [PATCH] test: Migrate away from deprecated PHPUnit code Signed-off-by: provokateurin --- tests/ACL/RuleManagerTest.php | 43 +++++++++++++++++------------- tests/Folder/FolderManagerTest.php | 17 ++++++------ 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/tests/ACL/RuleManagerTest.php b/tests/ACL/RuleManagerTest.php index 3c19dfb8c..6db0cc766 100644 --- a/tests/ACL/RuleManagerTest.php +++ b/tests/ACL/RuleManagerTest.php @@ -14,8 +14,10 @@ use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager; use OCA\GroupFolders\ACL\UserMapping\UserMapping; use OCP\EventDispatcher\IEventDispatcher; +use OCP\IDBConnection; use OCP\IUser; use OCP\Log\Audit\CriticalActionPerformedEvent; +use OCP\Server; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -41,7 +43,7 @@ protected function setUp(): void { ->willReturnCallback(fn (string $type, string $id): UserMapping => new UserMapping($type, $id)); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); - $this->ruleManager = new RuleManager(\OC::$server->getDatabaseConnection(), $this->userMappingManager, $this->eventDispatcher); + $this->ruleManager = new RuleManager(Server::get(IDBConnection::class), $this->userMappingManager, $this->eventDispatcher); } public function testGetSetRule(): void { @@ -51,41 +53,44 @@ public function testGetSetRule(): void { ->with($this->user) ->willReturn([$mapping]); + $parameters = null; $this->eventDispatcher->expects($this->any()) ->method('dispatchTyped') - ->withConsecutive( - [$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [ - 'permissions' => 0b00001001, - 'mask' => 0b00001111, - 'fileId' => 10, - 'user' => '1 (1)', - ])], - [$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [ - 'permissions' => 0b00001000, - 'mask' => 0b00001111, - 'fileId' => 10, - 'user' => '1 (1)', - ])], - [$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [ - 'fileId' => 10, - 'user' => '1 (1)', - ])], - ); + ->willReturnCallback(function (CriticalActionPerformedEvent $event) use (&$parameters): bool { + $parameters = $event->getParameters(); + return true; + }); $rule = new Rule($mapping, 10, 0b00001111, 0b00001001); $this->ruleManager->saveRule($rule); + $this->assertEquals([ + 'permissions' => 0b00001001, + 'mask' => 0b00001111, + 'fileId' => 10, + 'user' => '1 (1)', + ], $parameters); $result = $this->ruleManager->getRulesForFilesById($this->user, [10]); $this->assertEquals([10 => [$rule]], $result); $updatedRule = new Rule($mapping, 10, 0b00001111, 0b00001000); $this->ruleManager->saveRule($updatedRule); + $this->assertEquals([ + 'permissions' => 0b00001000, + 'mask' => 0b00001111, + 'fileId' => 10, + 'user' => '1 (1)', + ], $parameters); $result = $this->ruleManager->getRulesForFilesById($this->user, [10]); $this->assertEquals([10 => [$updatedRule]], $result); // cleanup $this->ruleManager->deleteRule($rule); + $this->assertEquals([ + 'fileId' => 10, + 'user' => '1 (1)', + ], $parameters); } public function testGetMultiple(): void { diff --git a/tests/Folder/FolderManagerTest.php b/tests/Folder/FolderManagerTest.php index 9c423d4b1..94ff9f377 100644 --- a/tests/Folder/FolderManagerTest.php +++ b/tests/Folder/FolderManagerTest.php @@ -14,6 +14,7 @@ use OCP\IDBConnection; use OCP\IGroupManager; use OCP\IUser; +use OCP\Server; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -42,7 +43,7 @@ protected function setUp(): void { ->with('groupfolders.quota.default', -3) ->willReturn(-3); $this->manager = new FolderManager( - \OC::$server->getDatabaseConnection(), + Server::get(IDBConnection::class), $this->groupManager, $this->mimeLoader, $this->logger, @@ -53,11 +54,11 @@ protected function setUp(): void { } private function clean(): void { - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $query->delete('group_folders')->execute(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); + $query->delete('group_folders')->executeStatement(); - $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $query->delete('group_folders_groups')->execute(); + $query = Server::get(IDBConnection::class)->getQueryBuilder(); + $query->delete('group_folders_groups')->executeStatement(); } private function assertHasFolders(array $folders): void { @@ -315,7 +316,7 @@ public function testGetFoldersForUserSimple(): void { $db = $this->createMock(IDBConnection::class); $manager = $this->getMockBuilder(FolderManager::class) ->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config]) - ->setMethods(['getFoldersForGroups']) + ->onlyMethods(['getFoldersForGroups']) ->getMock(); $folder = [ @@ -337,7 +338,7 @@ public function testGetFoldersForUserMerge(): void { $db = $this->createMock(IDBConnection::class); $manager = $this->getMockBuilder(FolderManager::class) ->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config]) - ->setMethods(['getFoldersForGroups']) + ->onlyMethods(['getFoldersForGroups']) ->getMock(); $folder1 = [ @@ -372,7 +373,7 @@ public function testGetFolderPermissionsForUserMerge(): void { $db = $this->createMock(IDBConnection::class); $manager = $this->getMockBuilder(FolderManager::class) ->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config]) - ->setMethods(['getFoldersForGroups']) + ->onlyMethods(['getFoldersForGroups']) ->getMock(); $folder1 = [