diff options
Diffstat (limited to 'apps/comments/tests/Unit/Notification')
-rw-r--r-- | apps/comments/tests/Unit/Notification/ListenerTest.php | 120 | ||||
-rw-r--r-- | apps/comments/tests/Unit/Notification/NotifierTest.php | 88 |
2 files changed, 72 insertions, 136 deletions
diff --git a/apps/comments/tests/Unit/Notification/ListenerTest.php b/apps/comments/tests/Unit/Notification/ListenerTest.php index b70c00a22cd..356a26f23cd 100644 --- a/apps/comments/tests/Unit/Notification/ListenerTest.php +++ b/apps/comments/tests/Unit/Notification/ListenerTest.php @@ -1,27 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Comments\Tests\Unit\Notification; @@ -32,26 +14,20 @@ use OCP\IURLGenerator; use OCP\IUserManager; use OCP\Notification\IManager; use OCP\Notification\INotification; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class ListenerTest extends TestCase { - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $notificationManager; - - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $userManager; - - /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ - protected $urlGenerator; - - /** @var Listener */ - protected $listener; + protected IManager&MockObject $notificationManager; + protected IUserManager&MockObject $userManager; + protected IURLGenerator&MockObject $urlGenerator; + protected Listener $listener; protected function setUp(): void { parent::setUp(); - $this->notificationManager = $this->createMock(\OCP\Notification\IManager::class); - $this->userManager = $this->createMock(\OCP\IUserManager::class); + $this->notificationManager = $this->createMock(IManager::class); + $this->userManager = $this->createMock(IUserManager::class); $this->listener = new Listener( $this->notificationManager, @@ -59,7 +35,7 @@ class ListenerTest extends TestCase { ); } - public function eventProvider() { + public static function eventProvider(): array { return [ [CommentsEvent::EVENT_ADD, 'notify'], [CommentsEvent::EVENT_UPDATE, 'notify'], @@ -69,13 +45,13 @@ class ListenerTest extends TestCase { } /** - * @dataProvider eventProvider * @param string $eventType * @param string $notificationMethod */ - public function testEvaluate($eventType, $notificationMethod) { - /** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */ - $comment = $this->getMockBuilder(IComment::class)->getMock(); + #[\PHPUnit\Framework\Attributes\DataProvider('eventProvider')] + public function testEvaluate($eventType, $notificationMethod): void { + /** @var IComment|MockObject $comment */ + $comment = $this->createMock(IComment::class); $comment->expects($this->any()) ->method('getObjectType') ->willReturn('files'); @@ -96,10 +72,8 @@ class ListenerTest extends TestCase { ->method('getId') ->willReturn('1234'); - /** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */ - $event = $this->getMockBuilder(CommentsEvent::class) - ->disableOriginalConstructor() - ->getMock(); + /** @var CommentsEvent|MockObject $event */ + $event = $this->createMock(CommentsEvent::class); $event->expects($this->once()) ->method('getComment') ->willReturn($comment); @@ -107,8 +81,8 @@ class ListenerTest extends TestCase { ->method(('getEvent')) ->willReturn($eventType); - /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */ - $notification = $this->getMockBuilder(INotification::class)->getMock(); + /** @var INotification|MockObject $notification */ + $notification = $this->createMock(INotification::class); $notification->expects($this->any()) ->method($this->anything()) ->willReturn($notification); @@ -124,26 +98,22 @@ class ListenerTest extends TestCase { $this->userManager->expects($this->exactly(6)) ->method('userExists') - ->withConsecutive( - ['foobar'], - ['barfoo'], - ['foo@bar.com'], - ['bar@foo.org@foobar.io'], - ['23452-4333-54353-2342'], - ['yolo'] - ) - ->willReturn(true); + ->willReturnMap([ + ['foobar', true], + ['barfoo', true], + ['foo@bar.com', true], + ['bar@foo.org@foobar.io', true], + ['23452-4333-54353-2342', true], + ['yolo', true] + ]); $this->listener->evaluate($event); } - /** - * @dataProvider eventProvider - * @param string $eventType - */ - public function testEvaluateNoMentions($eventType) { - /** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */ - $comment = $this->getMockBuilder(IComment::class)->getMock(); + #[\PHPUnit\Framework\Attributes\DataProvider('eventProvider')] + public function testEvaluateNoMentions(string $eventType): void { + /** @var IComment|MockObject $comment */ + $comment = $this->createMock(IComment::class); $comment->expects($this->any()) ->method('getObjectType') ->willReturn('files'); @@ -154,10 +124,8 @@ class ListenerTest extends TestCase { ->method('getMentions') ->willReturn([]); - /** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */ - $event = $this->getMockBuilder(CommentsEvent::class) - ->disableOriginalConstructor() - ->getMock(); + /** @var CommentsEvent|MockObject $event */ + $event = $this->createMock(CommentsEvent::class); $event->expects($this->once()) ->method('getComment') ->willReturn($comment); @@ -178,9 +146,9 @@ class ListenerTest extends TestCase { $this->listener->evaluate($event); } - public function testEvaluateUserDoesNotExist() { - /** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */ - $comment = $this->getMockBuilder(IComment::class)->getMock(); + public function testEvaluateUserDoesNotExist(): void { + /** @var IComment|MockObject $comment */ + $comment = $this->createMock(IComment::class); $comment->expects($this->any()) ->method('getObjectType') ->willReturn('files'); @@ -194,10 +162,8 @@ class ListenerTest extends TestCase { ->method('getId') ->willReturn('1234'); - /** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */ - $event = $this->getMockBuilder(CommentsEvent::class) - ->disableOriginalConstructor() - ->getMock(); + /** @var CommentsEvent|MockObject $event */ + $event = $this->createMock(CommentsEvent::class); $event->expects($this->once()) ->method('getComment') ->willReturn($comment); @@ -205,8 +171,8 @@ class ListenerTest extends TestCase { ->method(('getEvent')) ->willReturn(CommentsEvent::EVENT_ADD); - /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */ - $notification = $this->getMockBuilder(INotification::class)->getMock(); + /** @var INotification|MockObject $notification */ + $notification = $this->createMock(INotification::class); $notification->expects($this->any()) ->method($this->anything()) ->willReturn($notification); @@ -221,9 +187,7 @@ class ListenerTest extends TestCase { $this->userManager->expects($this->once()) ->method('userExists') - ->withConsecutive( - ['foobar'] - ) + ->with('foobar') ->willReturn(false); $this->listener->evaluate($event); diff --git a/apps/comments/tests/Unit/Notification/NotifierTest.php b/apps/comments/tests/Unit/Notification/NotifierTest.php index e94f0686f18..37cad0b43df 100644 --- a/apps/comments/tests/Unit/Notification/NotifierTest.php +++ b/apps/comments/tests/Unit/Notification/NotifierTest.php @@ -1,28 +1,10 @@ <?php + +declare(strict_types=1); + /** - * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Comments\Tests\Unit\Notification; @@ -37,31 +19,23 @@ use OCP\IL10N; use OCP\IURLGenerator; use OCP\IUserManager; use OCP\L10N\IFactory; +use OCP\Notification\AlreadyProcessedException; use OCP\Notification\INotification; +use OCP\Notification\UnknownNotificationException; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class NotifierTest extends TestCase { - /** @var Notifier */ - protected $notifier; - /** @var IFactory|MockObject */ - protected $l10nFactory; - /** @var IL10N|MockObject */ - protected $l; - /** @var IRootFolder|MockObject */ - protected $folder; - /** @var ICommentsManager|MockObject */ - protected $commentsManager; - /** @var IURLGenerator|MockObject */ - protected $url; - /** @var IUserManager|MockObject */ - protected $userManager; - /** @var INotification|MockObject */ - protected $notification; - /** @var IComment|MockObject */ - protected $comment; - /** @var string */ - protected $lc = 'tlh_KX'; + protected IFactory&MockObject $l10nFactory; + protected IL10N&MockObject $l; + protected IRootFolder&MockObject $folder; + protected ICommentsManager&MockObject $commentsManager; + protected IURLGenerator&MockObject $url; + protected IUserManager&MockObject $userManager; + protected INotification&MockObject $notification; + protected IComment&MockObject $comment; + protected Notifier $notifier; + protected string $lc = 'tlh_KX'; protected function setUp(): void { parent::setUp(); @@ -91,12 +65,11 @@ class NotifierTest extends TestCase { $this->comment = $this->createMock(IComment::class); } - public function testPrepareSuccess() { + public function testPrepareSuccess(): void { $fileName = 'Gre\'thor.odp'; $displayName = 'Huraga'; - $message = '@Huraga mentioned you in a comment on "Gre\'thor.odp"'; - /** @var Node|MockObject $node */ + /** @var Node&MockObject $node */ $node = $this->createMock(Node::class); $node ->expects($this->atLeastOnce()) @@ -209,9 +182,8 @@ class NotifierTest extends TestCase { $this->notifier->prepare($this->notification, $this->lc); } - public function testPrepareSuccessDeletedUser() { + public function testPrepareSuccessDeletedUser(): void { $fileName = 'Gre\'thor.odp'; - $message = 'You were mentioned on "Gre\'thor.odp", in a comment by an account that has since been deleted'; /** @var Node|MockObject $node */ $node = $this->createMock(Node::class); @@ -324,8 +296,8 @@ class NotifierTest extends TestCase { } - public function testPrepareDifferentApp() { - $this->expectException(\InvalidArgumentException::class); + public function testPrepareDifferentApp(): void { + $this->expectException(UnknownNotificationException::class); $this->folder ->expects($this->never()) @@ -361,8 +333,8 @@ class NotifierTest extends TestCase { } - public function testPrepareNotFound() { - $this->expectException(\InvalidArgumentException::class); + public function testPrepareNotFound(): void { + $this->expectException(UnknownNotificationException::class); $this->folder ->expects($this->never()) @@ -399,8 +371,8 @@ class NotifierTest extends TestCase { } - public function testPrepareDifferentSubject() { - $this->expectException(\InvalidArgumentException::class); + public function testPrepareDifferentSubject(): void { + $this->expectException(UnknownNotificationException::class); $displayName = 'Huraga'; @@ -456,8 +428,8 @@ class NotifierTest extends TestCase { } - public function testPrepareNotFiles() { - $this->expectException(\InvalidArgumentException::class); + public function testPrepareNotFiles(): void { + $this->expectException(UnknownNotificationException::class); $displayName = 'Huraga'; @@ -514,8 +486,8 @@ class NotifierTest extends TestCase { } - public function testPrepareUnresolvableFileID() { - $this->expectException(\OCP\Notification\AlreadyProcessedException::class); + public function testPrepareUnresolvableFileID(): void { + $this->expectException(AlreadyProcessedException::class); $displayName = 'Huraga'; |