From e3c75aae30b57c7844d6a8777dd132b4e8e124fe Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Thu, 18 May 2023 16:51:40 +0545 Subject: [PATCH 1/2] Adjust Symfony OutputFormatterInterface in unit tests --- tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php | 5 ++++- tests/lib/Encryption/DecryptAllTest.php | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php index 061e2a932710..5adf8a4ccbe1 100644 --- a/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php +++ b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php @@ -26,6 +26,7 @@ use OC\Files\View; use OCP\IConfig; use OCP\IUserManager; +use Symfony\Component\Console\Formatter\OutputFormatterInterface; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -71,7 +72,9 @@ public function setUp(): void { $this->outputInterface = $this->createMock('Symfony\Component\Console\Output\OutputInterface'); $this->userInterface = $this->createMock('\OCP\UserInterface'); - $outputFormatterInterface = $this->createMock('Symfony\Component\Console\Formatter\OutputFormatterInterface'); + $outputFormatterInterface = $this->createMock(OutputFormatterInterface::class); + $outputFormatterInterface->expects($this->any())->method('isDecorated') + ->willReturn(false); $this->outputInterface->expects($this->any())->method('getFormatter') ->willReturn($outputFormatterInterface); diff --git a/tests/lib/Encryption/DecryptAllTest.php b/tests/lib/Encryption/DecryptAllTest.php index da42efc78e7f..8978a19ca568 100644 --- a/tests/lib/Encryption/DecryptAllTest.php +++ b/tests/lib/Encryption/DecryptAllTest.php @@ -102,8 +102,11 @@ public function setUp(): void { $this->userInterface = $this->getMockBuilder(UserInterface::class) ->disableOriginalConstructor()->getMock(); + $outputFormatterInterface = $this->createMock(OutputFormatterInterface::class); + $outputFormatterInterface->expects($this->any())->method('isDecorated') + ->willReturn(false); $this->outputInterface->expects($this->any())->method('getFormatter') - ->willReturn($this->createMock(OutputFormatterInterface::class)); + ->willReturn($outputFormatterInterface); $this->instance = new DecryptAll($this->encryptionManager, $this->userManager, $this->view, $this->logger); From f7d7f9cd179ceecf743598b1fbd04e49f401c3aa Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Fri, 19 May 2023 11:56:05 +0545 Subject: [PATCH 2/2] Adjust PHP unit tests for Symfony 5 --- .../tests/NotificationsTest.php | 4 ++-- apps/federation/tests/TrustedServersTest.php | 6 ++++++ .../tests/Controller/ViewControllerTest.php | 6 ++++++ apps/files_sharing/tests/HooksTest.php | 18 +++++++++--------- apps/files_trashbin/tests/TrashbinTest.php | 2 +- tests/Core/Controller/RolesControllerTest.php | 1 + tests/lib/Repair/AppsTest.php | 6 ++++++ 7 files changed, 31 insertions(+), 12 deletions(-) diff --git a/apps/federatedfilesharing/tests/NotificationsTest.php b/apps/federatedfilesharing/tests/NotificationsTest.php index 416b35f1093c..5ff29603c02a 100644 --- a/apps/federatedfilesharing/tests/NotificationsTest.php +++ b/apps/federatedfilesharing/tests/NotificationsTest.php @@ -341,14 +341,14 @@ public function dataTryHttpPostToShareEndpointInException() { public function testDeclineEvent() { $dispatcher = \OC::$server->getEventDispatcher(); $event = $dispatcher->dispatch( - DeclineShare::class, new DeclineShare( [ 'remote_id' => '4354353', 'remote' => 'http://localhost', 'share_token' => 'ohno' ] - ) + ), + DeclineShare::class ); $this->assertInstanceOf(DeclineShare::class, $event); } diff --git a/apps/federation/tests/TrustedServersTest.php b/apps/federation/tests/TrustedServersTest.php index 4b7dd1fc3019..735b380124c1 100644 --- a/apps/federation/tests/TrustedServersTest.php +++ b/apps/federation/tests/TrustedServersTest.php @@ -74,6 +74,12 @@ public function setUp(): void { ->disableOriginalConstructor()->getMock(); $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface') ->disableOriginalConstructor()->getMock(); + $this->dispatcher->expects($this->any())->method('dispatch') + ->will( + $this->returnCallback(function ($object) { + return $object; + }) + ); $this->httpClientService = $this->createMock('OCP\Http\Client\IClientService'); $this->httpClient = $this->createMock('OCP\Http\Client\IClient'); $this->response = $this->createMock('OCP\Http\Client\IResponse'); diff --git a/apps/files/tests/Controller/ViewControllerTest.php b/apps/files/tests/Controller/ViewControllerTest.php index 48b2c84c1c9c..49ef6152c969 100644 --- a/apps/files/tests/Controller/ViewControllerTest.php +++ b/apps/files/tests/Controller/ViewControllerTest.php @@ -75,6 +75,12 @@ public function setUp(): void { $this->l10n = $this->createMock('\OCP\IL10N'); $this->config = $this->createMock('\OCP\IConfig'); $this->eventDispatcher = $this->createMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->eventDispatcher->expects($this->any())->method('dispatch') + ->will( + $this->returnCallback(function ($object) { + return $object; + }) + ); $this->userSession = $this->createMock('\OCP\IUserSession'); $this->appManager = $this->createMock('\OCP\App\IAppManager'); $this->user = $this->createMock('\OCP\IUser'); diff --git a/apps/files_sharing/tests/HooksTest.php b/apps/files_sharing/tests/HooksTest.php index 22b91a6ab4ed..b7e930015aa6 100644 --- a/apps/files_sharing/tests/HooksTest.php +++ b/apps/files_sharing/tests/HooksTest.php @@ -147,7 +147,7 @@ public function testPrivateLink() { 'resolvedWebLink' => null, 'resolvedDavLink' => null, ]); - $this->eventDispatcher->dispatch('files.resolvePrivateLink', $event); + $this->eventDispatcher->dispatch($event, 'files.resolvePrivateLink'); $this->assertEquals('/owncloud/index.php/apps/files/?view=sharingin&scrollto=123', $event->getArgument('resolvedWebLink')); $this->assertNull($event->getArgument('resolvedDavLink')); @@ -168,7 +168,7 @@ public function testPrivateLinkNoMatch() { 'resolvedWebLink' => null, 'resolvedDavLink' => null, ]); - $this->eventDispatcher->dispatch('files.resolvePrivateLink', $event); + $this->eventDispatcher->dispatch($event, 'files.resolvePrivateLink'); $this->assertNull($event->getArgument('resolvedWebLink')); $this->assertNull($event->getArgument('resolvedDavLink')); @@ -182,7 +182,7 @@ public function testDeleteGroup() { $this->sharingAllowlist->expects($this->once())->method('setPublicShareSharersGroupsAllowlist')->with([]); $event = new GenericEvent($group); - $this->eventDispatcher->dispatch('group.postDelete', $event); + $this->eventDispatcher->dispatch($event, 'group.postDelete'); } public function testPublishShareNotification() { @@ -196,7 +196,7 @@ public function testPublishShareNotification() { 'share' => ['id' => '123'], 'shareObject' => $share, ]); - $this->eventDispatcher->dispatch('share.afterCreate', $event); + $this->eventDispatcher->dispatch($event, 'share.afterCreate'); } public function testDiscardShareNotification() { @@ -210,7 +210,7 @@ public function testDiscardShareNotification() { 'share' => ['id' => '123'], 'shareObject' => $share, ]); - $this->eventDispatcher->dispatch('share.afterDelete', $event); + $this->eventDispatcher->dispatch($event, 'share.afterDelete'); } public function providesDataForCanGet() { @@ -271,7 +271,7 @@ public function testCheckDirectCanBeDownloaded($path, $userFolder, $run) { // Simulate direct download of file $event = new GenericEvent(null, [ 'path' => $path ]); - $this->eventDispatcher->dispatch('file.beforeGetDirect', $event); + $this->eventDispatcher->dispatch($event, 'file.beforeGetDirect'); $this->assertEquals($run, !$event->hasArgument('errorMessage')); } @@ -349,7 +349,7 @@ public function testCheckZipCanBeDownloaded($dir, $files, $userFolder, $run) { // Simulate zip download of folder folder $event = new GenericEvent(null, ['dir' => $dir, 'files' => $files, 'run' => true]); - $this->eventDispatcher->dispatch('file.beforeCreateZip', $event); + $this->eventDispatcher->dispatch($event, 'file.beforeCreateZip'); $this->assertEquals($run, $event->getArgument('run')); $this->assertEquals($run, !$event->hasArgument('errorMessage')); @@ -360,7 +360,7 @@ public function testCheckFileUserNotFound() { // Simulate zip download of folder folder $event = new GenericEvent(null, ['dir' => '/test', 'files' => ['test.txt'], 'run' => true]); - $this->eventDispatcher->dispatch('file.beforeCreateZip', $event); + $this->eventDispatcher->dispatch($event, 'file.beforeCreateZip'); // It should run as this would restrict e.g. share links otherwise $this->assertTrue($event->getArgument('run')); @@ -394,7 +394,7 @@ public function testPublishShareSelfUnshareEvent() { 'shareRecipient' => 'recipient_user', 'shareOwner' => 'owner_user', ]); - $this->eventDispatcher->dispatch('fromself.unshare', $event); + $this->eventDispatcher->dispatch($event, 'fromself.unshare'); } public function testExtendJsConfig() { diff --git a/apps/files_trashbin/tests/TrashbinTest.php b/apps/files_trashbin/tests/TrashbinTest.php index 0cbd4f74441c..8a9f550c8180 100644 --- a/apps/files_trashbin/tests/TrashbinTest.php +++ b/apps/files_trashbin/tests/TrashbinTest.php @@ -565,7 +565,7 @@ public function testPrivateLink() { 'resolvedWebLink' => null, 'resolvedDavLink' => null, ]); - \OC::$server->getEventDispatcher()->dispatch('files.resolvePrivateLink', $event); + \OC::$server->getEventDispatcher()->dispatch($event, 'files.resolvePrivateLink'); $this->assertEquals('/owncloud/index.php/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt', $event->getArgument('resolvedWebLink')); $this->assertNull($event->getArgument('resolvedDavLink')); diff --git a/tests/Core/Controller/RolesControllerTest.php b/tests/Core/Controller/RolesControllerTest.php index afea0309e6ce..962cb68ebf1f 100644 --- a/tests/Core/Controller/RolesControllerTest.php +++ b/tests/Core/Controller/RolesControllerTest.php @@ -47,6 +47,7 @@ public function testGetAvatarNoAvatar() { 'id' => 'test.tester', 'displayName' => 'A tester which tests ....' ]); + return $event; }); $result = $controller->getRoles(); diff --git a/tests/lib/Repair/AppsTest.php b/tests/lib/Repair/AppsTest.php index 2c4287beecc4..f7b48e4c9c6a 100644 --- a/tests/lib/Repair/AppsTest.php +++ b/tests/lib/Repair/AppsTest.php @@ -49,6 +49,12 @@ protected function setUp(): void { $this->appManager = $this->createMock(IAppManager::class); $this->defaults = $this->createMock(\OC_Defaults::class); $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); + $this->eventDispatcher->expects($this->any())->method('dispatch') + ->will( + $this->returnCallback(function ($object) { + return $object; + }) + ); $this->config = $this->createMock(IConfig::class); $this->repair = new Apps( $this->appManager,