From f5e00ccb0bf450188a2b7e2c53f20a923cf46f0e Mon Sep 17 00:00:00 2001 From: Jannik Stehle Date: Wed, 14 Apr 2021 12:19:19 +0200 Subject: [PATCH] Add unit tests for the newly added command --- lib/Command/RepairNotifications.php | 4 +- .../Unit/Command/RepairNotificationsTest.php | 83 +++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Command/RepairNotificationsTest.php diff --git a/lib/Command/RepairNotifications.php b/lib/Command/RepairNotifications.php index b000281a..d9076c24 100644 --- a/lib/Command/RepairNotifications.php +++ b/lib/Command/RepairNotifications.php @@ -33,7 +33,7 @@ class RepairNotifications extends Command { /** @var IDBConnection */ protected $connection; - private static $availableSubjects = [ + public static $availableSubjects = [ 'relativeLinks' ]; @@ -75,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { if (!$result) { $output->writeln('No notifications found to repair.'); - return false; + return 0; } $output->writeln(\sprintf('%s notification(s) found to repair', \count($result))); diff --git a/tests/Unit/Command/RepairNotificationsTest.php b/tests/Unit/Command/RepairNotificationsTest.php new file mode 100644 index 00000000..a96d6ae3 --- /dev/null +++ b/tests/Unit/Command/RepairNotificationsTest.php @@ -0,0 +1,83 @@ + + * @author Jan Ackermann + * + * @copyright Copyright (c) 2021, ownCloud GmbH + * @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 + * + */ + +namespace OCA\Notifications\Tests\Unit\Command; + +use Doctrine\DBAL\Driver\Statement; +use OCA\Notifications\Command\Generate; +use OCA\Notifications\Command\RepairNotifications; +use OCA\Notifications\Tests\Unit\TestCase; +use OCP\DB\QueryBuilder\IExpressionBuilder; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; +use Symfony\Component\Console\Tester\CommandTester; + +class RepairNotificationsTest extends TestCase { + + /** @var IDBConnection | \PHPUnit\Framework\MockObject\MockObject */ + protected $connection; + /** @var Generate */ + protected $command; + /** @var CommandTester */ + protected $tester; + + protected function setUp(): void { + parent::setUp(); + + $this->connection = $this->createMock(IDBConnection::class); + $this->command = new RepairNotifications($this->connection); + $this->tester = new CommandTester($this->command); + } + + public function testInvalidSubject() { + $this->expectException(\LogicException::class); + + $options = []; + $input = ['subject' => 'test']; + $this->tester->execute($input, $options); + } + + public function testRepairLinks() { + $options = []; + $input = ['subject' => RepairNotifications::$availableSubjects[0]]; + + $dbResult = [ + ['notification_id' => 1, 'link' => 'http://owncloud.com/test', 'actions' => '[]'] + ]; + + $exprBuilder = $this->createMock(IExpressionBuilder::class); + $statementMock = $this->createMock(Statement::class); + $statementMock->method('fetchAll')->willReturn($dbResult); + $qbMock = $this->createMock(IQueryBuilder::class); + $qbMock->method('select')->willReturnSelf(); + $qbMock->method('from')->willReturnSelf(); + $qbMock->method('update')->willReturnSelf(); + $qbMock->method('where')->willReturnSelf(); + $qbMock->method('expr')->willReturn($exprBuilder); + $qbMock->method('execute')->willReturn($statementMock); + + $this->connection->method('getQueryBuilder')->willReturn($qbMock); + + $response = $this->tester->execute($input, $options); + $this->assertEquals(0, $response); + } +}