From c5daf112bed9d958f70073fd1cb199beff322334 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Wed, 28 Oct 2020 23:06:49 +0100 Subject: Command: Add unittests Signed-off-by: Sean Molenaar --- tests/Unit/Command/AfterUpdateTest.php | 80 ++++++++++++++++++ tests/Unit/Command/BeforeUpdateTest.php | 67 +++++++++++++++ tests/Unit/Command/ExploreGeneratorTest.php | 3 +- tests/Unit/Command/FeedAddTest.php | 93 +++++++++++++++++++++ tests/Unit/Command/FeedDeleteTest.php | 77 +++++++++++++++++ tests/Unit/Command/FeedListTest.php | 123 ++++++++++++++++++++++++++++ tests/Unit/Command/FolderAddTest.php | 83 +++++++++++++++++++ tests/Unit/Command/FolderDeleteTest.php | 101 +++++++++++++++++++++++ tests/Unit/Command/FolderListTest.php | 120 +++++++++++++++++++++++++++ tests/Unit/Command/OpmlExportTest.php | 78 ++++++++++++++++++ tests/Unit/Command/ShowFeedTest.php | 122 +++++++++++++++++++++++++++ tests/Unit/Command/UpdateFeedTest.php | 110 +++++++++++++++++++++++++ 12 files changed, 1056 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Command/AfterUpdateTest.php create mode 100644 tests/Unit/Command/BeforeUpdateTest.php create mode 100644 tests/Unit/Command/FeedAddTest.php create mode 100644 tests/Unit/Command/FeedDeleteTest.php create mode 100644 tests/Unit/Command/FeedListTest.php create mode 100644 tests/Unit/Command/FolderAddTest.php create mode 100644 tests/Unit/Command/FolderDeleteTest.php create mode 100644 tests/Unit/Command/FolderListTest.php create mode 100644 tests/Unit/Command/OpmlExportTest.php create mode 100644 tests/Unit/Command/ShowFeedTest.php create mode 100644 tests/Unit/Command/UpdateFeedTest.php (limited to 'tests/Unit/Command') diff --git a/tests/Unit/Command/AfterUpdateTest.php b/tests/Unit/Command/AfterUpdateTest.php new file mode 100644 index 000000000..fbc327ecc --- /dev/null +++ b/tests/Unit/Command/AfterUpdateTest.php @@ -0,0 +1,80 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Updater\AfterUpdate; +use OCA\News\Fetcher\Fetcher; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Service\ItemServiceV2; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class AfterUpdateTest extends TestCase +{ + /** @var MockObject|ItemServiceV2 */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var AfterUpdate */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(ItemServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class) + ->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class) + ->getMock(); + + $this->command = new AfterUpdate($this->service); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('purge_count') + ->willReturn('1'); + + $this->service->expects($this->exactly(1)) + ->method('purgeOverThreshold') + ->with('1') + ->willReturn('test'); + + $this->consoleOutput->expects($this->exactly(1)) + ->method('writeln') + ->with('test'); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } +} diff --git a/tests/Unit/Command/BeforeUpdateTest.php b/tests/Unit/Command/BeforeUpdateTest.php new file mode 100644 index 000000000..282bdacde --- /dev/null +++ b/tests/Unit/Command/BeforeUpdateTest.php @@ -0,0 +1,67 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Updater\BeforeUpdate; +use OCA\News\Service\UpdaterService; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class BeforeUpdateTest extends TestCase +{ + /** @var MockObject|UpdaterService */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var BeforeUpdate */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(UpdaterService::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class) + ->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class) + ->getMock(); + + $this->command = new BeforeUpdate($this->service); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->service->expects($this->exactly(1)) + ->method('beforeUpdate'); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } +} diff --git a/tests/Unit/Command/ExploreGeneratorTest.php b/tests/Unit/Command/ExploreGeneratorTest.php index 4512e206c..ec04c4241 100644 --- a/tests/Unit/Command/ExploreGeneratorTest.php +++ b/tests/Unit/Command/ExploreGeneratorTest.php @@ -30,7 +30,8 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; -class ExploreGeneratorTest extends TestCase { +class ExploreGeneratorTest extends TestCase +{ /** @var \PHPUnit_Framework_MockObject_MockObject */ protected $favicon; /** @var \PHPUnit_Framework_MockObject_MockObject */ diff --git a/tests/Unit/Command/FeedAddTest.php b/tests/Unit/Command/FeedAddTest.php new file mode 100644 index 000000000..c0d4de1bd --- /dev/null +++ b/tests/Unit/Command/FeedAddTest.php @@ -0,0 +1,93 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Config\FeedAdd; +use OCA\News\Command\Updater\UpdateFeed; +use OCA\News\Db\Feed; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Service\FeedServiceV2; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class FeedAddTest extends TestCase +{ + /** @var MockObject|FeedServiceV2 */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var UpdateFeed */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(FeedServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + + $this->command = new FeedAdd($this->service); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['feed', 'http://feed'], + ['user-id', 'admin'], + ])); + + $this->consoleInput->expects($this->exactly(5)) + ->method('getOption') + ->will($this->returnValueMap([ + ['folder', '0'], + ['title', 'title'], + ['username', 'user'], + ['password', 'pass'], + ['full-text', '1'], + ])); + + $feed = $this->createMock(Feed::class); + + $this->service->expects($this->exactly(1)) + ->method('create') + ->with('admin', 'http://feed', 0, true, 'title', 'user', 'pass') + ->willReturn($feed); + + $this->service->expects($this->exactly(1)) + ->method('fetch') + ->with($feed); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } +} diff --git a/tests/Unit/Command/FeedDeleteTest.php b/tests/Unit/Command/FeedDeleteTest.php new file mode 100644 index 000000000..09b8fb721 --- /dev/null +++ b/tests/Unit/Command/FeedDeleteTest.php @@ -0,0 +1,77 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Config\FeedAdd; +use OCA\News\Command\Config\FeedDelete; +use OCA\News\Command\Updater\UpdateFeed; +use OCA\News\Db\Feed; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Service\FeedServiceV2; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class FeedDeleteTest extends TestCase +{ + /** @var MockObject|FeedServiceV2 */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var UpdateFeed */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(FeedServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + + $this->command = new FeedDelete($this->service); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['feed-id', '1'], + ['user-id', 'admin'], + ])); + + $this->service->expects($this->exactly(1)) + ->method('delete') + ->with('admin', '1'); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } +} diff --git a/tests/Unit/Command/FeedListTest.php b/tests/Unit/Command/FeedListTest.php new file mode 100644 index 000000000..52b63e6ec --- /dev/null +++ b/tests/Unit/Command/FeedListTest.php @@ -0,0 +1,123 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Config\FeedAdd; +use OCA\News\Command\Config\FeedList; +use OCA\News\Command\Updater\UpdateFeed; +use OCA\News\Db\Feed; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Service\FeedServiceV2; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class FeedListTest extends TestCase +{ + /** @var MockObject|FeedServiceV2 */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var UpdateFeed */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(FeedServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + + $this->command = new FeedList($this->service); + } + + /** + * Test a valid call will work + */ + public function testValidRecurse() + { + $this->consoleInput->expects($this->exactly(1)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['user-id', 'admin'], + ])); + + $this->consoleInput->expects($this->exactly(1)) + ->method('getOption') + ->will($this->returnValueMap([ + ['recursive', true], + ])); + + $feed = $this->createMock(Feed::class); + + $this->service->expects($this->exactly(1)) + ->method('findAllForUserRecursive') + ->with('admin') + ->willReturn([$feed]); + + $this->consoleOutput->expects($this->exactly(1)) + ->method('writeln') + ->with("[\n []\n]"); + + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->exactly(1)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['user-id', 'admin'], + ])); + + $this->consoleInput->expects($this->exactly(1)) + ->method('getOption') + ->will($this->returnValueMap([ + ['recursive', false], + ])); + + $feed = $this->createMock(Feed::class); + + $this->service->expects($this->exactly(1)) + ->method('findAllForUser') + ->with('admin') + ->willReturn([$feed]); + + $this->consoleOutput->expects($this->exactly(1)) + ->method('writeln') + ->with("[\n []\n]"); + + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } +} diff --git a/tests/Unit/Command/FolderAddTest.php b/tests/Unit/Command/FolderAddTest.php new file mode 100644 index 000000000..5991920bb --- /dev/null +++ b/tests/Unit/Command/FolderAddTest.php @@ -0,0 +1,83 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Config\FolderAdd; +use OCA\News\Db\Folder; +use OCA\News\Service\FolderServiceV2; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class FolderAddTest extends TestCase +{ + /** @var MockObject|FolderServiceV2 */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var FolderAdd */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(FolderServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + + $this->command = new FolderAdd($this->service); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['name', 'folder'], + ['user-id', 'admin'], + ])); + + $this->consoleInput->expects($this->exactly(1)) + ->method('getOption') + ->will($this->returnValueMap([ + ['parent', '0'], + ])); + + $feed = $this->createMock(Folder::class); + + $this->service->expects($this->exactly(1)) + ->method('create') + ->with('admin', 'folder', 0) + ->willReturn($feed); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } +} diff --git a/tests/Unit/Command/FolderDeleteTest.php b/tests/Unit/Command/FolderDeleteTest.php new file mode 100644 index 000000000..9db078b33 --- /dev/null +++ b/tests/Unit/Command/FolderDeleteTest.php @@ -0,0 +1,101 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Config\FeedAdd; +use OCA\News\Command\Config\FeedDelete; +use OCA\News\Command\Config\FolderDelete; +use OCA\News\Command\Updater\UpdateFeed; +use OCA\News\Db\Feed; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Service\FeedServiceV2; +use OCA\News\Service\FolderServiceV2; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class FolderDeleteTest extends TestCase +{ + /** @var MockObject|FolderServiceV2 */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var UpdateFeed */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(FolderServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + + $this->command = new FolderDelete($this->service); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['folder-id', '1'], + ['user-id', 'admin'], + ])); + + $this->service->expects($this->exactly(1)) + ->method('delete') + ->with('admin', '1'); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } + + /** + * Test a valid call will work + */ + public function testInValid() + { + $this->expectException('OCA\News\Service\Exceptions\ServiceException'); + $this->expectExceptionMessage('Can not remove root folder!'); + + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['folder-id', '0'], + ['user-id', 'admin'], + ])); + + $this->service->expects($this->never()) + ->method('delete'); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } +} diff --git a/tests/Unit/Command/FolderListTest.php b/tests/Unit/Command/FolderListTest.php new file mode 100644 index 000000000..04f20dce6 --- /dev/null +++ b/tests/Unit/Command/FolderListTest.php @@ -0,0 +1,120 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Config\FolderList; +use OCA\News\Db\Folder; +use OCA\News\Service\FolderServiceV2; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class FolderListTest extends TestCase +{ + /** @var MockObject|FolderServiceV2 */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var FolderList */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(FolderServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + + $this->command = new FolderList($this->service); + } + + /** + * Test a valid call will work + */ + public function testValidRecurse() + { + $this->consoleInput->expects($this->exactly(1)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['user-id', 'admin'], + ])); + + $this->consoleInput->expects($this->exactly(1)) + ->method('getOption') + ->will($this->returnValueMap([ + ['recursive', true], + ])); + + $feed = $this->createMock(Folder::class); + + $this->service->expects($this->exactly(1)) + ->method('findAllForUserRecursive') + ->with('admin') + ->willReturn([$feed]); + + $this->consoleOutput->expects($this->exactly(1)) + ->method('writeln') + ->with("[\n []\n]"); + + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->exactly(1)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['user-id', 'admin'], + ])); + + $this->consoleInput->expects($this->exactly(1)) + ->method('getOption') + ->will($this->returnValueMap([ + ['recursive', false], + ])); + + $feed = $this->createMock(Folder::class); + + $this->service->expects($this->exactly(1)) + ->method('findAllForUser') + ->with('admin') + ->willReturn([$feed]); + + $this->consoleOutput->expects($this->exactly(1)) + ->method('writeln') + ->with("[\n []\n]"); + + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } +} diff --git a/tests/Unit/Command/OpmlExportTest.php b/tests/Unit/Command/OpmlExportTest.php new file mode 100644 index 000000000..a7828279e --- /dev/null +++ b/tests/Unit/Command/OpmlExportTest.php @@ -0,0 +1,78 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Config\OpmlExport; +use OCA\News\Command\Updater\UpdateFeed; +use OCA\News\Service\OpmlService; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class OpmlExportTest extends TestCase +{ + /** @var MockObject|OpmlService */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var UpdateFeed */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(OpmlService::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + + $this->command = new OpmlExport($this->service); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->exactly(1)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['user-id', 'admin'], + ])); + + $this->service->expects($this->exactly(1)) + ->method('export') + ->with('admin') + ->willReturn('export'); + + $this->consoleOutput->expects($this->exactly(1)) + ->method('write') + ->with('export'); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } +} diff --git a/tests/Unit/Command/ShowFeedTest.php b/tests/Unit/Command/ShowFeedTest.php new file mode 100644 index 000000000..ecaf4b0a0 --- /dev/null +++ b/tests/Unit/Command/ShowFeedTest.php @@ -0,0 +1,122 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\ShowFeed; +use OCA\News\Fetcher\Fetcher; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class ShowFeedTest extends TestCase +{ + /** @var MockObject|Fetcher */ + protected $fetcher; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var ShowFeed */ + protected $command; + + protected function setUp(): void + { + $this->fetcher = $this->getMockBuilder(Fetcher::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + + $this->command = new ShowFeed($this->fetcher); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('feed') + ->willReturn('feed'); + + $this->consoleInput->expects($this->exactly(3)) + ->method('getOption') + ->will($this->returnValueMap([ + ['user', 'user'], + ['password', 'user'], + ['full-text', '1'], + ])); + + $this->fetcher->expects($this->exactly(1)) + ->method('fetch') + ->with('feed', true, null, true, 'user', 'user') + ->willReturn([['feed'], [['items']]]); + + $this->consoleOutput->expects($this->exactly(2)) + ->method('writeln') + ->withConsecutive( + ["Feed: [\n \"feed\"\n]"], + ["Items: [\n [\n \"items\"\n ]\n]"] + ); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } + + /** + * Test a valid call will work + */ + public function testInValid() + { + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('feed') + ->willReturn('feed'); + + $this->consoleInput->expects($this->exactly(3)) + ->method('getOption') + ->will($this->returnValueMap([ + ['user', 'user'], + ['password', 'user'], + ['full-text', '1'], + ])); + + $this->fetcher->expects($this->exactly(1)) + ->method('fetch') + ->with('feed', true, null, true, 'user', 'user') + ->will($this->throwException(new ServiceNotFoundException('test'))); + + $this->consoleOutput->expects($this->exactly(2)) + ->method('writeln') + ->withConsecutive( + ['Failed to fetch feed info:'], + ['test'] + ); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(1, $result); + } +} diff --git a/tests/Unit/Command/UpdateFeedTest.php b/tests/Unit/Command/UpdateFeedTest.php new file mode 100644 index 000000000..05d12fa66 --- /dev/null +++ b/tests/Unit/Command/UpdateFeedTest.php @@ -0,0 +1,110 @@ + + * + * @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\News\Tests\Unit\Command; + +use OCA\News\Command\Updater\UpdateFeed; +use OCA\News\Db\Feed; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Service\FeedServiceV2; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class UpdateFeedTest extends TestCase +{ + /** @var MockObject|FeedServiceV2 */ + protected $service; + /** @var MockObject|InputInterface */ + protected $consoleInput; + /** @var MockObject|OutputInterface */ + protected $consoleOutput; + + /** @var UpdateFeed */ + protected $command; + + protected function setUp(): void + { + $this->service = $this->getMockBuilder(FeedServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); + $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + + $this->command = new UpdateFeed($this->service); + } + + /** + * Test a valid call will work + */ + public function testValid() + { + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['feed-id', '1'], + ['user-id', 'admin'], + ])); + + $feed = $this->createMock(Feed::class); + + $this->service->expects($this->exactly(1)) + ->method('findForUser') + ->with('admin', '1') + ->willReturn($feed); + + $this->service->expects($this->exactly(1)) + ->method('fetch') + ->with($feed); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(0, $result); + } + + /** + * Test a valid call will work + */ + public function testInValid() + { + $this->consoleInput->expects($this->exactly(2)) + ->method('getArgument') + ->will($this->returnValueMap([ + ['feed-id', '1'], + ['user-id', 'admin'], + ])); + + $feed = $this->createMock(Feed::class); + + $this->service->expects($this->exactly(1)) + ->method('findForUser') + ->with('admin', '1') + ->willReturn($feed); + + $this->service->expects($this->exactly(1)) + ->method('fetch') + ->with($feed) + ->will($this->throwException(new ServiceNotFoundException(''))); + + $result = $this->command->run($this->consoleInput, $this->consoleOutput); + $this->assertSame(1, $result); + } +} -- cgit v1.2.3