diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2020-09-20 22:03:05 +0200 |
---|---|---|
committer | Benjamin Brahmer <info@b-brahmer.de> | 2020-09-25 19:18:04 +0200 |
commit | 60ab4941cc7e6ede095e9e4aee3c2bf9a5c3bff6 (patch) | |
tree | baf0b07dd1c545efeb59437af46a99f4d9f69425 /tests/Unit | |
parent | 2c8b4fa019749113658b9ed8cae211b679e4cbc0 (diff) |
Move to nextcloud config and update phpunit
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'tests/Unit')
30 files changed, 600 insertions, 828 deletions
diff --git a/tests/Unit/Config/ConfigTest.php b/tests/Unit/Config/ConfigTest.php deleted file mode 100644 index 60a292afc..000000000 --- a/tests/Unit/Config/ConfigTest.php +++ /dev/null @@ -1,261 +0,0 @@ -<?php -/** - * Nextcloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Alessandro Cosentino <cosenal@gmail.com> - * @author Bernhard Posselt <dev@bernhard-posselt.com> - * @copyright 2012 Alessandro Cosentino - * @copyright 2012-2014 Bernhard Posselt - */ - -namespace OCA\News\Tests\Unit\Config; - -use OCA\News\Config\Config; -use OCP\Files\File; -use OCP\Files\Folder; -use OCP\ILogger; -use PHPUnit\Framework\TestCase; - -class ConfigTest extends TestCase -{ - - private $fileSystem; - private $config; - private $configPath; - private $loggerParams; - - public function setUp() - { - $this->logger = $this->getMockBuilder('OCA\News\Utility\PsrLogger') - ->disableOriginalConstructor() - ->getMock(); - $this->fileSystem = $this->getMockBuilder(Folder::class)->getMock(); - $this->loggerParams = ['hi']; - $this->config = new Config( - $this->fileSystem, - $this->logger, - $this->loggerParams - ); - $this->configPath = 'config.json'; - } - - - public function testDefaults() - { - $this->assertEquals(60, $this->config->getAutoPurgeMinimumInterval()); - $this->assertEquals(200, $this->config->getAutoPurgeCount()); - $this->assertEquals(10, $this->config->getMaxRedirects()); - $this->assertEquals(60, $this->config->getFeedFetcherTimeout()); - $this->assertEquals(3600, $this->config->getUpdateInterval()); - $this->assertEquals(true, $this->config->getUseCronUpdates()); - $this->assertEquals('', $this->config->getExploreUrl()); - $this->assertEquals(1024*1024*100, $this->config->getMaxSize()); - } - - - public function testRead() - { - $file = $this->getMockBuilder(File::class)->getMock(); - $this->fileSystem->expects($this->once()) - ->method('get') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue($file)); - $file->expects($this->once()) - ->method('getContent') - ->will( - $this->returnValue( - 'autoPurgeCount = 3' . "\n" . 'useCronUpdates = true' - ) - ); - - - $this->config->read($this->configPath); - - $this->assertSame(3, $this->config->getAutoPurgeCount()); - $this->assertSame(true, $this->config->getUseCronUpdates()); - } - - - public function testReadIgnoresVeryLowPurgeInterval() - { - $file = $this->getMockBuilder(File::class)->getMock(); - $this->fileSystem->expects($this->once()) - ->method('get') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue($file)); - $file->expects($this->once()) - ->method('getContent') - ->will($this->returnValue('autoPurgeMinimumInterval = 59')); - - $this->config->read($this->configPath); - - $this->assertSame(60, $this->config->getAutoPurgeMinimumInterval()); - } - - - - public function testReadBool() - { - $file = $this->getMockBuilder(File::class)->getMock(); - $this->fileSystem->expects($this->once()) - ->method('get') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue($file)); - $file->expects($this->once()) - ->method('getContent') - ->will( - $this->returnValue( - 'autoPurgeCount = 3' . "\n" . 'useCronUpdates = false' - ) - ); - - $this->config->read($this->configPath); - - $this->assertSame(3, $this->config->getAutoPurgeCount()); - $this->assertSame(false, $this->config->getUseCronUpdates()); - } - - - public function testReadLogsInvalidValue() - { - $file = $this->getMockBuilder(File::class)->getMock(); - $this->fileSystem->expects($this->once()) - ->method('get') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue($file)); - $file->expects($this->once()) - ->method('getContent') - ->will($this->returnValue('autoPurgeCounts = 3')); - $this->logger->expects($this->once()) - ->method('warning') - ->with( - $this->equalTo( - 'Configuration value "autoPurgeCounts" ' . - 'does not exist. Ignored value.' - ), - $this->equalTo($this->loggerParams) - ); - - $this->config->read($this->configPath); - } - - - public function testReadLogsInvalidINI() - { - $file = $this->getMockBuilder(File::class)->getMock(); - $this->fileSystem->expects($this->once()) - ->method('get') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue($file)); - $file->expects($this->once()) - ->method('getContent') - ->will($this->returnValue('')); - $this->logger->expects($this->once()) - ->method('warning') - ->with( - $this->equalTo('Configuration invalid. Ignoring values.'), - $this->equalTo($this->loggerParams) - ); - - $this->config->read($this->configPath); - } - - - public function testWrite() - { - $json = 'autoPurgeMinimumInterval = 60' . "\n" . - 'autoPurgeCount = 3' . "\n" . - 'maxRedirects = 10' . "\n" . - 'maxSize = 399' . "\n" . - 'exploreUrl = http://google.de' . "\n" . - 'feedFetcherTimeout = 60' . "\n" . - 'updateInterval = 3600' . "\n" . - 'useCronUpdates = true'; - $this->config->setAutoPurgeCount(3); - $this->config->setMaxSize(399); - $this->config->setExploreUrl('http://google.de'); - - $file = $this->getMockBuilder(File::class)->getMock(); - $this->fileSystem->expects($this->once()) - ->method('get') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue($file)); - $file->expects($this->once()) - ->method('putContent') - ->with($this->equalTo($json)); - - $this->config->write($this->configPath); - } - - - - public function testReadingNonExistentConfigWillWriteDefaults() - { - $this->fileSystem->expects($this->once()) - ->method('nodeExists') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue(false)); - - $this->config->setUseCronUpdates(false); - - $json = 'autoPurgeMinimumInterval = 60' . "\n" . - 'autoPurgeCount = 200' . "\n" . - 'maxRedirects = 10' . "\n" . - 'maxSize = 104857600' . "\n" . - 'exploreUrl = ' . "\n" . - 'feedFetcherTimeout = 60' . "\n" . - 'updateInterval = 3600' . "\n" . - 'useCronUpdates = false'; - - $this->fileSystem->expects($this->once()) - ->method('newFile') - ->with($this->equalTo($this->configPath)); - $file = $this->getMockBuilder(File::class)->getMock(); - $this->fileSystem->expects($this->once()) - ->method('get') - ->with($this->equalTo($this->configPath)) - ->will($this->returnValue($file)); - $file->expects($this->once()) - ->method('putContent') - ->with($this->equalTo($json)); - - $this->config->read($this->configPath, true); - } - - - public function testNoLowMinimumAutoPurgeInterval() - { - $this->config->setAutoPurgeMinimumInterval(59); - $interval = $this->config->getAutoPurgeMinimumInterval(); - - $this->assertSame(60, $interval); - } - - - public function testMinimumAutoPurgeInterval() - { - $this->config->setAutoPurgeMinimumInterval(61); - $interval = $this->config->getAutoPurgeMinimumInterval(); - - $this->assertSame(61, $interval); - } - - public function testMaxRedirects() - { - $this->config->setMaxRedirects(21); - $redirects = $this->config->getMaxRedirects(); - - $this->assertSame(21, $redirects); - } - - public function testFeedFetcherTimeout() - { - $this->config->setFeedFetcherTimeout(2); - $timout = $this->config->getFeedFetcherTimeout(); - - $this->assertSame(2, $timout); - } -} diff --git a/tests/Unit/Controller/AdminControllerTest.php b/tests/Unit/Controller/AdminControllerTest.php index b0b551006..81490bb6f 100644 --- a/tests/Unit/Controller/AdminControllerTest.php +++ b/tests/Unit/Controller/AdminControllerTest.php @@ -13,82 +13,84 @@ namespace OCA\News\Tests\Unit\Controller; -use OCA\News\Config\Config; use OCA\News\Controller\AdminController; use OCA\News\Service\ItemService; +use OCP\IConfig; use OCP\IRequest; use PHPUnit\Framework\TestCase; class AdminControllerTest extends TestCase { - + /** + * @var string + */ private $appName; + + /** + * @var \PHPUnit\Framework\MockObject\MockObject|IRequest + */ private $request; + + /** + * @var AdminController + */ private $controller; + + /** + * @var \PHPUnit\Framework\MockObject\MockObject|IConfig + */ private $config; - private $configPath; + + /** + * @var \PHPUnit\Framework\MockObject\MockObject|ItemService + */ private $itemService; /** * Gets run before each test */ - public function setUp() + public function setUp(): void { $this->appName = 'news'; $this->request = $this->getMockBuilder(IRequest::class) ->disableOriginalConstructor() ->getMock(); - $this->config = $this->getMockBuilder(Config::class) + $this->config = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock(); $this->itemService = $this->getMockBuilder(ItemService::class) ->disableOriginalConstructor() ->getMock(); - $this->configPath = 'my.ini'; - $this->controller = new AdminController( - $this->appName, $this->request, - $this->config, $this->itemService, $this->configPath - ); + $this->controller = new AdminController($this->appName, $this->request, $this->config, $this->itemService); } - - public function testIndex() + /** + * Test \OCA\News\Controller\AdminController::index + */ + public function testIndex() { $expected = [ 'autoPurgeMinimumInterval' => 1, 'autoPurgeCount' => 2, 'maxRedirects' => 3, 'feedFetcherTimeout' => 4, - 'useCronUpdates' => 5, - 'maxSize' => 7, + 'useCronUpdates' => false, 'exploreUrl' => 'test', - 'updateInterval' => 3600 + 'updateInterval' => 3601 ]; - $this->config->expects($this->once()) - ->method('getAutoPurgeMinimumInterval') - ->will($this->returnValue($expected['autoPurgeMinimumInterval'])); - $this->config->expects($this->once()) - ->method('getAutoPurgeCount') - ->will($this->returnValue($expected['autoPurgeCount'])); - $this->config->expects($this->once()) - ->method('getMaxRedirects') - ->will($this->returnValue($expected['maxRedirects'])); - $this->config->expects($this->once()) - ->method('getFeedFetcherTimeout') - ->will($this->returnValue($expected['feedFetcherTimeout'])); - $this->config->expects($this->once()) - ->method('getUseCronUpdates') - ->will($this->returnValue($expected['useCronUpdates'])); - $this->config->expects($this->once()) - ->method('getMaxSize') - ->will($this->returnValue($expected['maxSize'])); - $this->config->expects($this->once()) - ->method('getExploreUrl') - ->will($this->returnValue($expected['exploreUrl'])); - $this->config->expects($this->once()) - ->method('getUpdateInterval') - ->will($this->returnValue($expected['updateInterval'])); + $map = [ + ['news','autoPurgeMinimumInterval', 60, 1], + ['news','autoPurgeCount', 200, 2], + ['news','maxRedirects', 10, 3], + ['news','feedFetcherTimeout', 60, 4], + ['news','useCronUpdates', true, false,], + ['news','exploreUrl', '', 'test'], + ['news','updateInterval', 3600, 3601] + ]; + $this->config->expects($this->exactly(count($map))) + ->method('getAppValue') + ->will($this->returnValueMap($map)); $response = $this->controller->index(); $data = $response->getParams(); @@ -101,75 +103,48 @@ class AdminControllerTest extends TestCase } - public function testUpdate() + public function testUpdate() { $expected = [ 'autoPurgeMinimumInterval' => 1, 'autoPurgeCount' => 2, 'maxRedirects' => 3, 'feedFetcherTimeout' => 4, - 'useCronUpdates' => 5, - 'maxSize' => 7, + 'useCronUpdates' => false, 'exploreUrl' => 'test', - 'updateInterval' => 3600 + 'updateInterval' => 3601 ]; - $this->config->expects($this->once()) - ->method('setAutoPurgeMinimumInterval') - ->with($this->equalTo($expected['autoPurgeMinimumInterval'])); - $this->config->expects($this->once()) - ->method('setAutoPurgeCount') - ->with($this->equalTo($expected['autoPurgeCount'])); - $this->config->expects($this->once()) - ->method('setMaxRedirects') - ->with($this->equalTo($expected['maxRedirects'])); - $this->config->expects($this->once()) - ->method('setFeedFetcherTimeout') - ->with($this->equalTo($expected['feedFetcherTimeout'])); - $this->config->expects($this->once()) - ->method('setUseCronUpdates') - ->with($this->equalTo($expected['useCronUpdates'])); - $this->config->expects($this->once()) - ->method('setExploreUrl') - ->with($this->equalTo($expected['exploreUrl'])); - $this->config->expects($this->once()) - ->method('setUpdateInterval') - ->with($this->equalTo($expected['updateInterval'])); - $this->config->expects($this->once()) - ->method('write') - ->with($this->equalTo($this->configPath)); - - $this->config->expects($this->once()) - ->method('getAutoPurgeMinimumInterval') - ->will($this->returnValue($expected['autoPurgeMinimumInterval'])); - $this->config->expects($this->once()) - ->method('getAutoPurgeCount') - ->will($this->returnValue($expected['autoPurgeCount'])); - $this->config->expects($this->once()) - ->method('getMaxRedirects') - ->will($this->returnValue($expected['maxRedirects'])); - $this->config->expects($this->once()) - ->method('getFeedFetcherTimeout') - ->will($this->returnValue($expected['feedFetcherTimeout'])); - $this->config->expects($this->once()) - ->method('getUseCronUpdates') - ->will($this->returnValue($expected['useCronUpdates'])); - $this->config->expects($this->once()) - ->method('getMaxSize') - ->will($this->returnValue($expected['maxSize'])); - $this->config->expects($this->once()) - ->method('getExploreUrl') - ->will($this->returnValue($expected['exploreUrl'])); - $this->config->expects($this->once()) - ->method('getUpdateInterval') - ->will($this->returnValue($expected['updateInterval'])); + $this->config->expects($this->exactly(count($expected))) + ->method('setAppValue') + ->withConsecutive( + ['news','autoPurgeMinimumInterval', 1], + ['news','autoPurgeCount', 2], + ['news','maxRedirects', 3], + ['news','feedFetcherTimeout', 4], + ['news','useCronUpdates', false], + ['news','exploreUrl', 'test'], + ['news','updateInterval', 3601] + ); + + $map = [ + ['news','autoPurgeMinimumInterval', 60, 1], + ['news','autoPurgeCount', 200, 2], + ['news','maxRedirects', 10, 3], + ['news','feedFetcherTimeout', 60, 4], + ['news','useCronUpdates', true, false,], + ['news','exploreUrl', '', 'test'], + ['news','updateInterval', 3600, 3601] + ]; + $this->config->expects($this->exactly(count($map))) + ->method('getAppValue') + ->will($this->returnValueMap($map)); $response = $this->controller->update( $expected['autoPurgeMinimumInterval'], $expected['autoPurgeCount'], $expected['maxRedirects'], $expected['feedFetcherTimeout'], - $expected['maxSize'], $expected['useCronUpdates'], $expected['exploreUrl'], $expected['updateInterval'] diff --git a/tests/Unit/Controller/ExportControllerTest.php b/tests/Unit/Controller/ExportControllerTest.php index 84ded5c6f..94ffa1ed1 100644 --- a/tests/Unit/Controller/ExportControllerTest.php +++ b/tests/Unit/Controller/ExportControllerTest.php @@ -41,7 +41,7 @@ class ExportControllerTest extends TestCase /** * Gets run before each test */ - public function setUp() + public function setUp(): void { $this->appName = 'news'; $this->user = 'john'; diff --git a/tests/Unit/Controller/FeedApiControllerTest.php b/tests/Unit/Controller/FeedApiControllerTest.php index cc387161d..c889a54b3 100644 --- a/tests/Unit/Controller/FeedApiControllerTest.php +++ b/tests/Unit/Controller/FeedApiControllerTest.php @@ -44,7 +44,7 @@ class FeedApiControllerTest extends TestCase private $logger; private $loggerParams; - protected function setUp() + protected function setUp(): void { $this->loggerParams = ['hi']; $this->logger = $this->getMockBuilder(ILogger::class) @@ -85,7 +85,7 @@ class FeedApiControllerTest extends TestCase } - public function testIndex() + public function testIndex() { $feeds = [new Feed()]; $starredCount = 3; @@ -116,7 +116,7 @@ class FeedApiControllerTest extends TestCase } - public function testIndexNoNewestItemId() + public function testIndexNoNewestItemId() { $feeds = [new Feed()]; $starredCount = 3; @@ -145,7 +145,7 @@ class FeedApiControllerTest extends TestCase } - public function testDelete() + public function testDelete() { $this->feedService->expects($this->once()) ->method('delete') @@ -158,7 +158,7 @@ class FeedApiControllerTest extends TestCase } - public function testDeleteDoesNotExist() + public function testDeleteDoesNotExist() { $this->feedService->expects($this->once()) ->method('delete') @@ -176,7 +176,7 @@ class FeedApiControllerTest extends TestCase } - public function testCreate() + public function testCreate() { $feeds = [new Feed()]; @@ -206,7 +206,7 @@ class FeedApiControllerTest extends TestCase } - public function testCreateNoItems() + public function testCreateNoItems() { $feeds = [new Feed()]; @@ -236,7 +236,7 @@ class FeedApiControllerTest extends TestCase - public function testCreateExists() + public function testCreateExists() { $this->feedService->expects($this->once()) ->method('purgeDeleted') @@ -255,7 +255,7 @@ class FeedApiControllerTest extends TestCase } - public function testCreateError() + public function testCreateError() { $this->feedService->expects($this->once()) ->method('create') @@ -271,7 +271,7 @@ class FeedApiControllerTest extends TestCase } - public function testRead() + public function testRead() { $this->itemService->expects($this->once()) ->method('readFeed') @@ -285,7 +285,7 @@ class FeedApiControllerTest extends TestCase } - public function testMove() + public function testMove() { $this->feedService->expects($this->once()) ->method('patch') @@ -299,7 +299,7 @@ class FeedApiControllerTest extends TestCase } - public function testMoveDoesNotExist() + public function testMoveDoesNotExist() { $this->feedService->expects($this->once()) ->method('patch') @@ -315,7 +315,7 @@ class FeedApiControllerTest extends TestCase } - public function testRename() + public function testRename() { $feedId = 3; $feedTitle = 'test'; @@ -332,7 +332,7 @@ class FeedApiControllerTest extends TestCase } - public function testRenameError() + public function testRenameError() { $feedId = 3; $feedTitle = 'test'; @@ -370,7 +370,7 @@ class FeedApiControllerTest extends TestCase } - public function testUpdate() |