From 004fcbbcc7609ca83807f2e38967ef54f469bf72 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 23 Jul 2016 21:24:54 +0200 Subject: Move to new directory structure --- tests/Unit/Config/ConfigTest.php | 230 +++++ tests/Unit/Controller/AdminControllerTest.php | 161 +++ tests/Unit/Controller/EntityApiSerializerTest.php | 116 +++ tests/Unit/Controller/ExportControllerTest.php | 131 +++ tests/Unit/Controller/FeedApiControllerTest.php | 353 +++++++ tests/Unit/Controller/FeedControllerTest.php | 484 +++++++++ tests/Unit/Controller/FolderApiControllerTest.php | 255 +++++ tests/Unit/Controller/FolderControllerTest.php | 306 ++++++ tests/Unit/Controller/ItemApiControllerTest.php | 372 +++++++ tests/Unit/Controller/ItemControllerTest.php | 384 +++++++ tests/Unit/Controller/JSONHttpErrorTest.php | 34 + tests/Unit/Controller/PageControllerTest.php | 322 ++++++ tests/Unit/Controller/UserApiControllerTest.php | 141 +++ tests/Unit/Controller/UtilityApiControllerTest.php | 90 ++ tests/Unit/Db/FeedMapperTest.php | 306 ++++++ tests/Unit/Db/FeedTest.php | 116 +++ tests/Unit/Db/FolderMapperTest.php | 196 ++++ tests/Unit/Db/FolderTest.php | 50 + tests/Unit/Db/ItemMapperTest.php | 550 ++++++++++ tests/Unit/Db/ItemTest.php | 316 ++++++ tests/Unit/Db/MapperFactoryTest.php | 50 + tests/Unit/Db/MapperTestUtility.php | 208 ++++ tests/Unit/Db/Mysql/ItemMapperTest.php | 119 +++ tests/Unit/Fetcher/FeedFetcherTest.php | 386 +++++++ tests/Unit/Fetcher/FetcherTest.php | 137 +++ tests/Unit/Fetcher/YoutubeFetcherTest.php | 68 ++ tests/Unit/Http/TextDownloadResponseTest.php | 32 + tests/Unit/Http/TextResponseTest.php | 44 + tests/Unit/Service/FeedServiceTest.php | 1060 ++++++++++++++++++++ tests/Unit/Service/FolderServiceTest.php | 282 ++++++ tests/Unit/Service/ItemServiceTest.php | 443 ++++++++ tests/Unit/Service/ServiceTest.php | 98 ++ tests/Unit/Service/StatusFlagTest.php | 57 ++ tests/Unit/Service/StatusServiceTest.php | 89 ++ tests/Unit/Upgrade/UpgradeTest.php | 72 ++ tests/Unit/Utility/OPMLExporterTest.php | 123 +++ tests/Unit/Utility/ProxyConfigParserTest.php | 93 ++ tests/Unit/Utility/UpdaterTest.php | 62 ++ 38 files changed, 8336 insertions(+) create mode 100644 tests/Unit/Config/ConfigTest.php create mode 100644 tests/Unit/Controller/AdminControllerTest.php create mode 100644 tests/Unit/Controller/EntityApiSerializerTest.php create mode 100644 tests/Unit/Controller/ExportControllerTest.php create mode 100644 tests/Unit/Controller/FeedApiControllerTest.php create mode 100644 tests/Unit/Controller/FeedControllerTest.php create mode 100644 tests/Unit/Controller/FolderApiControllerTest.php create mode 100644 tests/Unit/Controller/FolderControllerTest.php create mode 100644 tests/Unit/Controller/ItemApiControllerTest.php create mode 100644 tests/Unit/Controller/ItemControllerTest.php create mode 100644 tests/Unit/Controller/JSONHttpErrorTest.php create mode 100644 tests/Unit/Controller/PageControllerTest.php create mode 100644 tests/Unit/Controller/UserApiControllerTest.php create mode 100644 tests/Unit/Controller/UtilityApiControllerTest.php create mode 100644 tests/Unit/Db/FeedMapperTest.php create mode 100644 tests/Unit/Db/FeedTest.php create mode 100644 tests/Unit/Db/FolderMapperTest.php create mode 100644 tests/Unit/Db/FolderTest.php create mode 100644 tests/Unit/Db/ItemMapperTest.php create mode 100644 tests/Unit/Db/ItemTest.php create mode 100644 tests/Unit/Db/MapperFactoryTest.php create mode 100644 tests/Unit/Db/MapperTestUtility.php create mode 100644 tests/Unit/Db/Mysql/ItemMapperTest.php create mode 100644 tests/Unit/Fetcher/FeedFetcherTest.php create mode 100644 tests/Unit/Fetcher/FetcherTest.php create mode 100644 tests/Unit/Fetcher/YoutubeFetcherTest.php create mode 100644 tests/Unit/Http/TextDownloadResponseTest.php create mode 100644 tests/Unit/Http/TextResponseTest.php create mode 100644 tests/Unit/Service/FeedServiceTest.php create mode 100644 tests/Unit/Service/FolderServiceTest.php create mode 100644 tests/Unit/Service/ItemServiceTest.php create mode 100644 tests/Unit/Service/ServiceTest.php create mode 100644 tests/Unit/Service/StatusFlagTest.php create mode 100644 tests/Unit/Service/StatusServiceTest.php create mode 100644 tests/Unit/Upgrade/UpgradeTest.php create mode 100644 tests/Unit/Utility/OPMLExporterTest.php create mode 100644 tests/Unit/Utility/ProxyConfigParserTest.php create mode 100644 tests/Unit/Utility/UpdaterTest.php (limited to 'tests/Unit') diff --git a/tests/Unit/Config/ConfigTest.php b/tests/Unit/Config/ConfigTest.php new file mode 100644 index 000000000..a30601403 --- /dev/null +++ b/tests/Unit/Config/ConfigTest.php @@ -0,0 +1,230 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Config; + +use PHPUnit_Framework_TestCase; + + +class ConfigTest extends PHPUnit_Framework_TestCase { + + private $fileSystem; + private $config; + private $configPath; + private $loggerParams; + + public function setUp() { + $this->logger = $this->getMockBuilder( + 'OCP\ILogger') + ->disableOriginalConstructor() + ->getMock(); + $this->fileSystem = $this->getMockBuilder('OCP\Files\Folder')->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(true, $this->config->getUseCronUpdates()); + $this->assertEquals('', $this->config->getExploreUrl()); + $this->assertEquals(1024*1024*100, $this->config->getMaxSize()); + } + + + public function testRead () { + $file = $this->getMockBuilder('OCP\Files\File')->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('OCP\Files\File')->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('OCP\Files\File')->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('OCP\Files\File')->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('OCP\Files\File')->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" . + 'useCronUpdates = true'; + $this->config->setAutoPurgeCount(3); + $this->config->setMaxSize(399); + $this->config->setExploreUrl('http://google.de'); + + $file = $this->getMockBuilder('OCP\Files\File')->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" . + 'useCronUpdates = false'; + + $this->fileSystem->expects($this->once()) + ->method('newFile') + ->with($this->equalTo($this->configPath)); + $file = $this->getMockBuilder('OCP\Files\File')->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 new file mode 100644 index 000000000..9d5014636 --- /dev/null +++ b/tests/Unit/Controller/AdminControllerTest.php @@ -0,0 +1,161 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + + +class AdminControllerTest extends \PHPUnit_Framework_TestCase { + + private $appName; + private $request; + private $controller; + private $config; + private $configPath; + private $itemService; + + /** + * Gets run before each test + */ + public function setUp(){ + $this->appName = 'news'; + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->config = $this->getMockBuilder( + '\OCA\News\Config\Config') + ->disableOriginalConstructor() + ->getMock(); + $this->itemService = $this->getMockBuilder( + '\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + + $this->configPath = 'my.ini'; + $this->controller = new AdminController($this->appName, $this->request, + $this->config, $this->itemService, $this->configPath); + } + + + public function testIndex() { + $expected = [ + 'autoPurgeMinimumInterval' => 1, + 'autoPurgeCount' => 2, + 'maxRedirects' => 3, + 'feedFetcherTimeout' => 4, + 'useCronUpdates' => 5, + 'maxSize' => 7, + 'exploreUrl' => 'test' + ]; + $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'])); + + $response = $this->controller->index(); + $data = $response->getParams(); + $name = $response->getTemplateName(); + $type = $response->getRenderAs(); + + $this->assertEquals($type, 'blank'); + $this->assertEquals($name, 'admin'); + $this->assertEquals($expected, $data); + } + + + public function testUpdate() { + $expected = [ + 'autoPurgeMinimumInterval' => 1, + 'autoPurgeCount' => 2, + 'maxRedirects' => 3, + 'feedFetcherTimeout' => 4, + 'useCronUpdates' => 5, + 'maxSize' => 7, + 'exploreUrl' => 'test' + ]; + + $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('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'])); + + $response = $this->controller->update( + $expected['autoPurgeMinimumInterval'], + $expected['autoPurgeCount'], + $expected['maxRedirects'], + $expected['feedFetcherTimeout'], + $expected['maxSize'], + $expected['useCronUpdates'], + $expected['exploreUrl'] + ); + + $this->assertEquals($expected, $response); + } + +} diff --git a/tests/Unit/Controller/EntityApiSerializerTest.php b/tests/Unit/Controller/EntityApiSerializerTest.php new file mode 100644 index 000000000..80752889d --- /dev/null +++ b/tests/Unit/Controller/EntityApiSerializerTest.php @@ -0,0 +1,116 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + + +use \OCP\AppFramework\Http\Response; +use \OCP\AppFramework\Db\Entity; + +use \OCA\News\Db\Item; + +class TestEntity extends Entity { + +} + + +class EntityApiSerializerTest extends \PHPUnit_Framework_TestCase { + + + public function testSerializeSingle() { + $item = new Item(); + $item->setUnread(); + + $serializer = new EntityApiSerializer('items'); + $result = $serializer->serialize($item); + + $this->assertTrue($result['items'][0]['unread']); + } + + + public function testSerializeMultiple() { + $item = new Item(); + $item->setUnread(); + + $item2 = new Item(); + $item2->setRead(); + + $serializer = new EntityApiSerializer('items'); + + $result = $serializer->serialize([$item, $item2]); + + $this->assertTrue($result['items'][0]['unread']); + $this->assertFalse($result['items'][1]['unread']); + } + + + public function testResponseNoChange() { + $response = new Response(); + $serializer = new EntityApiSerializer('items'); + + $result = $serializer->serialize($response); + + $this->assertEquals($response, $result); + } + + + public function testCompleteArraysTransformed() { + $item = new Item(); + $item->setUnread(); + + $item2 = new Item(); + $item2->setRead(); + + $serializer = new EntityApiSerializer('items'); + + $in = [ + 'items' => [$item, $item2], + 'test' => 1 + ]; + + $result = $serializer->serialize($in); + + $this->assertTrue($result['items'][0]['unread']); + $this->assertFalse($result['items'][1]['unread']); + $this->assertEquals(1, $result['test']); + } + + + public function testNoEntityNoChange() { + $serializer = new EntityApiSerializer('items'); + + $in = [ + 'items' => ['hi', '2'], + 'test' => 1 + ]; + + $result = $serializer->serialize($in); + + $this->assertEquals('hi', $result['items'][0]); + $this->assertEquals('2', $result['items'][1]); + $this->assertEquals(1, $result['test']); + } + + + public function testEntitiesNoChange() { + $serializer = new EntityApiSerializer('items'); + + $in = [ + 'items' => [new TestEntity()] + ]; + + $result = $serializer->serialize($in); + + $this->assertEquals($in, $result); + } +} \ No newline at end of file diff --git a/tests/Unit/Controller/ExportControllerTest.php b/tests/Unit/Controller/ExportControllerTest.php new file mode 100644 index 000000000..6e7df683e --- /dev/null +++ b/tests/Unit/Controller/ExportControllerTest.php @@ -0,0 +1,131 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use \OCP\AppFramework\Http; + +use \OCA\News\Http\TextDownloadResponse; +use \OCA\News\Utility\OPMLExporter; +use \OCA\News\Db\Item; +use \OCA\News\Db\Feed; + + +class ExportControllerTest extends \PHPUnit_Framework_TestCase { + + private $appName; + private $request; + private $controller; + private $user; + private $feedService; + private $folderService; + private $itemService; + private $opmlExporter; + + /** + * Gets run before each test + */ + public function setUp(){ + $this->appName = 'news'; + $this->user = 'john'; + $this->itemService = $this->getMockBuilder( + '\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + $this->feedService = $this->getMockBuilder( + '\OCA\News\Service\FeedService') + ->disableOriginalConstructor() + ->getMock(); + $this->folderService = $this->getMockBuilder( + '\OCA\News\Service\FolderService') + ->disableOriginalConstructor() + ->getMock(); + $this->request = $this->getMockBuilder('\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->opmlExporter = new OPMLExporter(); + $this->controller = new ExportController($this->appName, $this->request, + $this->folderService, $this->feedService, + $this->itemService, $this->opmlExporter, $this->user); + } + + + public function testOpmlExportNoFeeds(){ + $opml = + "\n" . + "\n" . + " \n" . + " Subscriptions\n" . + " \n" . + " \n" . + "\n"; + + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue([])); + $this->folderService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue([])); + + $return = $this->controller->opml(); + $this->assertTrue($return instanceof TextDownloadResponse); + $this->assertEquals($opml, $return->render()); + } + + + public function testGetAllArticles(){ + $item1 = new Item(); + $item1->setFeedId(3); + $item2 = new Item(); + $item2->setFeedId(5); + + $feed1 = new Feed(); + $feed1->setId(3); + $feed1->setLink('http://goo'); + $feed2 = new Feed(); + $feed2->setId(5); + $feed2->setLink('http://gee'); + $feeds = [$feed1, $feed2]; + + $articles = [$item1, $item2]; + + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + $this->itemService->expects($this->once()) + ->method('getUnreadOrStarred') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($articles)); + + + $return = $this->controller->articles(); + $headers = $return->getHeaders(); + $this->assertEquals( + 'attachment; filename="articles.json"', + $headers ['Content-Disposition'] + ); + + $this->assertEquals('[{"guid":null,"url":null,"title":null,' . + '"author":null,"pubDate":null,"body":null,"enclosureMime":null,' . + '"enclosureLink":null,"unread":false,"starred":false,' . + '"feedLink":"http:\/\/goo","rtl":null},{"guid":null,"url":null,' . + '"title":null,"author":null,"pubDate":null,"body":null,' . + '"enclosureMime":null,"enclosureLink":null,"unread":false,' . + '"starred":false,"feedLink":"http:\/\/gee","rtl":null}]', + $return->render()); + } + +} diff --git a/tests/Unit/Controller/FeedApiControllerTest.php b/tests/Unit/Controller/FeedApiControllerTest.php new file mode 100644 index 000000000..f13e96660 --- /dev/null +++ b/tests/Unit/Controller/FeedApiControllerTest.php @@ -0,0 +1,353 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use \OCP\AppFramework\Http; + +use \OCA\News\Service\ServiceNotFoundException; +use \OCA\News\Service\ServiceConflictException; +use \OCA\News\Db\Folder; +use \OCA\News\Db\Feed; +use \OCA\News\Db\Item; + + +class FeedApiControllerTest extends \PHPUnit_Framework_TestCase { + + private $feedService; + private $itemService; + private $feedAPI; + private $appName; + private $user; + private $request; + private $msg; + private $logger; + private $loggerParams; + + protected function setUp() { + $this->user = 'tom'; + $this->loggerParams = ['hi']; + $this->logger = $this->getMockBuilder( + '\OCP\ILogger') + ->disableOriginalConstructor() + ->getMock(); + $this->appName = 'news'; + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->feedService = $this->getMockBuilder( + '\OCA\News\Service\FeedService') + ->disableOriginalConstructor() + ->getMock(); + $this->itemService = $this->getMockBuilder( + '\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + $this->feedAPI = new FeedApiController( + $this->appName, + $this->request, + $this->feedService, + $this->itemService, + $this->logger, + $this->user, + $this->loggerParams + ); + $this->msg = 'hohoho'; + } + + + public function testIndex() { + $feeds = [new Feed()]; + $starredCount = 3; + $newestItemId = 2; + + $this->itemService->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($starredCount)); + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($newestItemId)); + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + + $response = $this->feedAPI->index(); + + $this->assertEquals([ + 'feeds' => [$feeds[0]->toAPI()], + 'starredCount' => $starredCount, + 'newestItemId' => $newestItemId + ], $response); + } + + + public function testIndexNoNewestItemId() { + $feeds = [new Feed()]; + $starredCount = 3; + + $this->itemService->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($starredCount)); + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->throwException(new ServiceNotFoundException(''))); + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($feeds)); + + $response = $this->feedAPI->index(); + + $this->assertEquals([ + 'feeds' => [$feeds[0]->toAPI()], + 'starredCount' => $starredCount, + ], $response); + } + + + public function testDelete() { + $this->feedService->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo(2), + $this->equalTo($this->user)); + + $this->feedAPI->delete(2); + } + + + public function testDeleteDoesNotExist() { + $this->feedService->expects($this->once()) + ->method('delete') + ->will($this->throwException( + new ServiceNotFoundException($this->msg)) + ); + + $response = $this->feedAPI->delete(2); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testCreate() { + $feeds = [new Feed()]; + + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedService->expects($this->once()) + ->method('create') + ->with( + $this->equalTo('url'), + $this->equalTo(3), + $this->equalTo($this->user)) + ->will($this->returnValue($feeds[0])); + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->will($this->returnValue(3)); + + $response = $this->feedAPI->create('url', 3); + + $this->assertEquals([ + 'feeds' => [$feeds[0]->toAPI()], + 'newestItemId' => 3 + ], $response); + } + + + public function testCreateNoItems() { + $feeds = [new Feed()]; + + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedService->expects($this->once()) + ->method('create') + ->with( + $this->equalTo('ho'), + $this->equalTo(3), + $this->equalTo($this->user)) + ->will($this->returnValue($feeds[0])); + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->will($this->throwException(new ServiceNotFoundException(''))); + + $response = $this->feedAPI->create('ho', 3); + + $this->assertEquals([ + 'feeds' => [$feeds[0]->toAPI()] + ], $response); + } + + + + public function testCreateExists() { + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedService->expects($this->once()) + ->method('create') + ->will( + $this->throwException(new ServiceConflictException($this->msg)) + ); + + $response = $this->feedAPI->create('ho', 3); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_CONFLICT, $response->getStatus()); + } + + + public function testCreateError() { + $this->feedService->expects($this->once()) + ->method('create') + ->will( + $this->throwException(new ServiceNotFoundException($this->msg)) + ); + + $response = $this->feedAPI->create('ho', 3); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testRead() { + $this->itemService->expects($this->once()) + ->method('readFeed') + ->with( + $this->equalTo(3), + $this->equalTo(30), + $this->equalTo($this->user)); + + $this->feedAPI->read(3, 30); + } + + + public function testMove() { + $this->feedService->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo(3), + $this->equalTo($this->user), + $this->equalTo(['folderId' => 30])); + + $this->feedAPI->move(3, 30); + } + + + public function testMoveDoesNotExist() { + $this->feedService->expects($this->once()) + ->method('patch') + ->will( + $this->throwException(new ServiceNotFoundException($this->msg)) + ); + + $response = $this->feedAPI->move(3, 4); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testRename() { + $feedId = 3; + $feedTitle = 'test'; + + $this->feedService->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo($feedId), + $this->equalTo($this->user), + $this->equalTo(['title' => $feedTitle])); + + $this->feedAPI->rename($feedId, $feedTitle); + } + + + public function testRenameError() { + $feedId = 3; + $feedTitle = 'test'; + + $this->feedService->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo($feedId), + $this->equalTo($this->user), + $this->equalTo(['title' => $feedTitle])) + ->will($this->throwException(new ServiceNotFoundException('hi'))); + + $result = $this->feedAPI->rename($feedId, $feedTitle); + $data = $result->getData(); + $code = $result->getStatus(); + + $this->assertSame(Http::STATUS_NOT_FOUND, $code); + $this->assertSame('hi', $data['message']); + } + + + public function testfromAllUsers(){ + $feed = new Feed(); + $feed->setUrl(3); + $feed->setId(1); + $feed->setUserId('john'); + $feeds = [$feed]; + $this->feedService->expects($this->once()) + ->method('findAllFromAllUsers') + ->will($this->returnValue($feeds)); + $response = json_encode($this->feedAPI->fromAllUsers()); + $this->assertEquals('{"feeds":[{"id":1,"userId":"john"}]}', $response); + } + + + public function testUpdate() { + $feedId = 3; + $userId = 'hi'; + + $this->feedService->expects($this->once()) + ->method('update') + ->with($this->equalTo($feedId), $this->equalTo($userId)); + + $this->feedAPI->update($userId, $feedId); + } + + + public function testUpdateError() { + $feedId = 3; + $userId = 'hi'; + $this->feedService->expects($this->once()) + ->method('update') + ->will($this->throwException(new \Exception($this->msg))); + $this->logger->expects($this->once()) + ->method('debug') + ->with($this->equalTo('Could not update feed ' . $this->msg), + $this->equalTo($this->loggerParams)); + + $this->feedAPI->update($userId, $feedId); + + + } + + +} diff --git a/tests/Unit/Controller/FeedControllerTest.php b/tests/Unit/Controller/FeedControllerTest.php new file mode 100644 index 000000000..3630ad2f8 --- /dev/null +++ b/tests/Unit/Controller/FeedControllerTest.php @@ -0,0 +1,484 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use OCP\AppFramework\Http; + +use OCA\News\Db\Feed; +use OCA\News\Db\FeedType; +use OCA\News\Service\ServiceNotFoundException; +use OCA\News\Service\ServiceConflictException; + + +class FeedControllerTest extends \PHPUnit_Framework_TestCase { + + private $appName; + private $feedService; + private $request; + private $controller; + private $folderService; + private $itemService; + private $settings; + private $exampleResult; + + + /** + * Gets run before each test + */ + public function setUp(){ + $this->appName = 'news'; + $this->user = 'jack'; + $this->settings = $this->getMockBuilder( + '\OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->itemService = $this + ->getMockBuilder('\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + $this->feedService = $this + ->getMockBuilder('\OCA\News\Service\FeedService') + ->disableOriginalConstructor() + ->getMock(); + $this->folderService = $this + ->getMockBuilder('\OCA\News\Service\FolderService') + ->disableOriginalConstructor() + ->getMock(); + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->controller = new FeedController($this->appName, $this->request, + $this->folderService, + $this->feedService, + $this->itemService, + $this->settings, + $this->user); + $this->exampleResult = [ + 'activeFeed' => [ + 'id' => 0, + 'type' => FeedType::SUBSCRIPTIONS + ] + ]; + } + + + public function testIndex(){ + $result = [ + 'feeds' => [ + ['a feed'], + ], + 'starred' => 13 + ]; + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($result['feeds'])); + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->throwException(new ServiceNotFoundException(''))); + $this->itemService->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($result['starred'])); + + $response = $this->controller->index(); + + $this->assertEquals($result, $response); + } + + + public function testIndexHighestItemIdExists(){ + $result = [ + 'feeds' => [ + ['a feed'], + ], + 'starred' => 13, + 'newestItemId' => 5 + ]; + $this->feedService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($result['feeds'])); + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($result['newestItemId'])); + $this->itemService->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($result['starred'])); + + $response = $this->controller->index(); + + $this->assertEquals($result, $response); + } + + + + private function activeInitMocks($id, $type){ + $this->settings->expects($this->at(0)) + ->method('getUserValue') + ->with($this->equalTo($this->user), + $this->equalTo($this->appName), + $this->equalTo('lastViewedFeedId')) + ->will($this->returnValue($id)); + $this->settings->expects($this->at(1)) + ->method('getUserValue') + ->with($this->equalTo($this->user), + $this->equalTo($this->appName), + $this->equalTo('lastViewedFeedType')) + ->will($this->returnValue($type)); + } + + + public function testActive(){ + $id = 3; + $type = FeedType::STARRED; + $result = [ + 'activeFeed' => [ + 'id' => $id, + 'type' => $type + ] + ]; + + $this->activeInitMocks($id, $type); + + $response = $this->controller->active(); + + $this->assertEquals($result, $response); + } + + + public function testActiveFeedDoesNotExist(){ + $id = 3; + $type = FeedType::FEED; + $ex = new ServiceNotFoundException('hiu'); + $result = $this->exampleResult; + + $this->feedService->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($this->user)) + ->will($this->throwException($ex)); + + $this->activeInitMocks($id, $type); + + $response = $this->controller->active(); + + $this->assertEquals($result, $response); + } + + + public function testActiveFolderDoesNotExist(){ + $id = 3; + $type = FeedType::FOLDER; + $ex = new ServiceNotFoundException('hiu'); + $result = $this->exampleResult; + + $this->folderService->expects($this->once()) + ->method('find') + ->with($this->equalTo($id), $this->equalTo($this->user)) + ->will($this->throwException($ex)); + + $this->activeInitMocks($id, $type); + + $response = $this->controller->active(); + + $this->assertEquals($result, $response); + } + + + public function testActiveActiveIsNull(){ + $id = 3; + $type = null; + $result = $this->exampleResult; + + + $this->activeInitMocks($id, $type); + + $response = $this->controller->active(); + + $this->assertEquals($result, $response); + } + + + public function testCreate(){ + $result = [ + 'feeds' => [new Feed()], + 'newestItemId' => 3 + ]; + + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->will($this->returnValue($result['newestItemId'])); + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedService->expects($this->once()) + ->method('create') + ->with($this->equalTo('hi'), + $this->equalTo(4), + $this->equalTo($this->user), + $this->equalTo('yo')) + ->will($this->returnValue($result['feeds'][0])); + + $response = $this->controller->create('hi', 4, 'yo'); + + $this->assertEquals($result, $response); + } + + + public function testCreateNoItems(){ + $result = ['feeds' => [new Feed()]]; + + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + + $this->itemService->expects($this->once()) + ->method('getNewestItemId') + ->will($this->throwException(new ServiceNotFoundException(''))); + + $this->feedService->expects($this->once()) + ->method('create') + ->with($this->equalTo('hi'), + $this->equalTo(4), + $this->equalTo($this->user), + $this->equalTo('yo')) + ->will($this->returnValue($result['feeds'][0])); + + $response = $this->controller->create('hi', 4, 'yo'); + + $this->assertEquals($result, $response); + } + + + public function testCreateReturnsErrorForInvalidCreate(){ + $msg = 'except'; + $ex = new ServiceNotFoundException($msg); + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedService->expects($this->once()) + ->method('create') + ->will($this->throwException($ex)); + + $response = $this->controller->create('hi', 4, 'test'); + $params = json_decode($response->render(), true); + + $this->assertEquals($msg, $params['message']); + $this->assertEquals( + $response->getStatus(), Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + + + public function testCreateReturnsErrorForDuplicateCreate(){ + $msg = 'except'; + $ex = new ServiceConflictException($msg); + $this->feedService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->feedService->expects($this->once()) + ->method('create') + ->will($this->throwException($ex)); + + $response = $this->controller->create('hi', 4, 'test'); + $params = json_decode($response->render(), true); + + $this->assertEquals($msg, $params['message']); + $this->assertEquals($response->getStatus(), Http::STATUS_CONFLICT); + } + + + public function testDelete(){ + $this->feedService->expects($this->once()) + ->method('markDeleted') + ->with($this->equalTo(4)); + + $this->controller->delete(4); + } + + + public function testDeleteDoesNotExist(){ + $msg = 'hehe'; + + $this->feedService->expects($this->once()) + ->method('markDeleted') + ->will($this->throwException(new ServiceNotFoundException($msg))); + + $response = $this->controller->delete(4); + $params = json_decode($response->render(), true); + + $this->assertEquals($msg, $params['message']); + $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); + } + + + public function testUpdate(){ + $feed = new Feed(); + $feed->setId(3); + $feed->setUnreadCount(44); + $result = [ + 'feeds' => [ + [ + 'id' => $feed->getId(), + 'unreadCount' => $feed->getUnreadCount() + ] + ] + ]; + + $this->feedService->expects($this->once()) + ->method('update') + ->with($this->equalTo(4), $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + + $response = $this->controller->update(4); + + $this->assertEquals($result, $response); + } + + + public function testUpdateReturnsJSONError(){ + $this->feedService->expects($this->once()) + ->method('update') + ->with($this->equalTo(4), $this->equalTo($this->user)) + ->will($this->throwException(new ServiceNotFoundException('NO!'))); + + $response = $this->controller->update(4); + $render = $response->render(); + + $this->assertEquals('{"message":"NO!"}', $render); + $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); + } + + + public function testImport() { + $feed = new Feed(); + + $expected = [ + 'starred' => 3, + 'feeds' => [$feed] + ]; + + $this->feedService->expects($this->once()) + ->method('importArticles') + ->with($this->equalTo(['json']), + $this->equalTo($this->user)) + ->will($this->returnValue($feed)); + + $this->itemService->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue(3)); + + $response = $this->controller->import(['json']); + + $this->assertEquals($expected, $response); + } + + + public function testImportCreatesNoAdditionalFeed() { + $this->feedService->expects($this->once()) + ->method('importArticles') + ->with($this->equalTo(['json']), + $this->equalTo($this->user)) + ->will($this->returnValue(null)); + + $this->itemService->expects($this->once()) + ->method('starredCount') + ->with($this->equalTo($this->user)) + ->will($this->returnValue(3)); + + $response = $this->controller->import(['json']); + + $this->assertEquals(['starred' => 3], $response); + } + + + public function testReadFeed(){ + $expected = [ + 'feeds' => [ + [ + 'id' => 4, + 'unreadCount' => 0 + ] + ] + ]; + + $this->itemService->expects($this->once()) + ->method('readFeed') + ->with($this->equalTo(4), $this->equalTo(5), $this->user); + + $response = $this->controller->read(4, 5); + $this->assertEquals($expected, $response); + } + + + public function testRestore() { + $this->feedService->expects($this->once()) + ->method('unmarkDeleted') + ->with($this->equalTo(4)); + + $this->controller->restore(4); + } + + + public function testRestoreDoesNotExist(){ + $msg = 'hehe'; + + $this->feedService->expects($this->once()) + ->method('unmarkDeleted') + ->will($this->throwException(new ServiceNotFoundException($msg))); + + $response = $this->controller->restore(4); + $params = json_decode($response->render(), true); + + $this->assertEquals($msg, $params['message']); + $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); + } + + public function testPatch() { + $expected = [ + 'pinned' => true, + 'fullTextEnabled' => true, + 'updateMode' => 1 + ]; + $this->feedService->expects($this->once()) + ->method('patch') + ->with($this->equalTo(4), + $this->equalTo($this->user), + $this->equalTo($expected)) + ->will($this->returnValue(1)); + + $this->controller->patch(4, true, true, 1); + } + + public function testPatchDoesNotExist(){ + $msg = 'hehe'; + + $this->feedService->expects($this->once()) + ->method('patch') + ->will($this->throwException(new ServiceNotFoundException($msg))); + + $response = $this->controller->patch(4, 2); + $params = json_decode($response->render(), true); + + $this->assertEquals($msg, $params['message']); + $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); + } + + +} diff --git a/tests/Unit/Controller/FolderApiControllerTest.php b/tests/Unit/Controller/FolderApiControllerTest.php new file mode 100644 index 000000000..550cd298c --- /dev/null +++ b/tests/Unit/Controller/FolderApiControllerTest.php @@ -0,0 +1,255 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use \OCP\AppFramework\Http; +use \OCP\AppFramework\Http\JSONResponse; + +use \OCA\News\Service\ServiceNotFoundException; +use \OCA\News\Service\ServiceConflictException; +use \OCA\News\Service\ServiceValidationException; + +use \OCA\News\Db\Folder; +use \OCA\News\Db\Feed; +use \OCA\News\Db\Item; + + +class FolderApiControllerTest extends \PHPUnit_Framework_TestCase { + + private $folderService; + private $itemService; + private $folderAPI; + private $appName; + private $user; + private $request; + private $msg; + + protected function setUp() { + $this->appName = 'news'; + $this->user = 'tom'; + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->folderService = $this->getMockBuilder( + '\OCA\News\Service\FolderService') + ->disableOriginalConstructor() + ->getMock(); + $this->itemService = $this->getMockBuilder( + '\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + $this->folderAPI = new FolderApiController( + $this->appName, + $this->request, + $this->folderService, + $this->itemService, + $this->user + ); + $this->msg = 'test'; + } + + + public function testIndex() { + $folders = [new Folder()]; + + $this->folderService->expects($this->once()) + ->method('findAll') + ->with($this->equalTo($this->user)) + ->will($this->returnValue($folders)); + + $response = $this->folderAPI->index(); + + $this->assertEquals([ + 'folders' => [$folders[0]->toAPI()] + ], $response); + } + + + public function testCreate() { + $folderName = 'test'; + $folder = new Folder(); + $folder->setName($folderName); + + $this->folderService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->folderService->expects($this->once()) + ->method('create') + ->with($this->equalTo($folderName), $this->equalTo($this->user)) + ->will($this->returnValue($folder)); + + $response = $this->folderAPI->create($folderName); + + $this->assertEquals([ + 'folders' => [$folder->toAPI()] + ], $response); + } + + + public function testCreateAlreadyExists() { + $msg = 'exists'; + + $this->folderService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->folderService->expects($this->once()) + ->method('create') + ->will($this->throwException(new ServiceConflictException($msg))); + + $response = $this->folderAPI->create('hi'); + + $data = $response->getData(); + $this->assertEquals($msg, $data['message']); + $this->assertEquals(Http::STATUS_CONFLICT, $response->getStatus()); + } + + + public function testCreateInvalidFolderName() { + $msg = 'exists'; + + $this->folderService->expects($this->once()) + ->method('purgeDeleted') + ->with($this->equalTo($this->user), $this->equalTo(false)); + $this->folderService->expects($this->once()) + ->method('create') + ->will($this->throwException(new ServiceValidationException($msg))); + + $response = $this->folderAPI->create('hi'); + + $data = $response->getData(); + $this->assertEquals($msg, $data['message']); + $this->assertEquals( + Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus() + ); + } + + + public function testDelete() { + $folderId = 23; + $this->folderService->expects($this->once()) + ->method('delete') + ->with($this->equalTo($folderId), $this->equalTo($this->user)); + + $this->folderAPI->delete(23); + } + + + public function testDeleteDoesNotExist() { + $folderId = 23; + + $this->folderService->expects($this->once()) + ->method('delete') + ->will( + $this->throwException( + new ServiceNotFoundException($this->msg) + ) + ); + + $response = $this->folderAPI->delete($folderId); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testUpdate() { + $folderId = 23; + $folderName = 'test'; + + $this->folderService->expects($this->once()) + ->method('rename') + ->with($this->equalTo($folderId), + $this->equalTo($folderName), + $this->equalTo($this->user)); + + $this->folderAPI->update($folderId, $folderName); + } + + public function testUpdateDoesNotExist() { + $folderId = 23; + $folderName = 'test'; + + $this->folderService->expects($this->once()) + ->method('rename') + ->will( + $this->throwException( + new ServiceNotFoundException($this->msg) + ) + ); + + $response = $this->folderAPI->update($folderId, $folderName); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + + public function testUpdateExists() { + $folderId = 23; + $folderName = 'test'; + + $this->folderService->expects($this->once()) + ->method('rename') + ->will( + $this->throwException( + new ServiceConflictException($this->msg) + ) + ); + + $response = $this->folderAPI->update($folderId, $folderName); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_CONFLICT, $response->getStatus()); + } + + + public function testUpdateInvalidFolderName() { + $folderId = 23; + $folderName = ''; + + $this->folderService->expects($this->once()) + ->method('rename') + ->will( + $this->throwException( + new ServiceValidationException($this->msg) + ) + ); + + $response = $this->folderAPI->update($folderId, $folderName); + + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals( + Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus() + ); + } + + + public function testRead() { + $this->itemService->expects($this->once()) + ->method('readFolder') + ->with( + $this->equalTo(3), + $this->equalTo(30), + $this->equalTo($this->user)); + + $this->folderAPI->read(3, 30); + } + + +} diff --git a/tests/Unit/Controller/FolderControllerTest.php b/tests/Unit/Controller/FolderControllerTest.php new file mode 100644 index 000000000..1fa915b11 --- /dev/null +++ b/tests/Unit/Controller/FolderControllerTest.php @@ -0,0 +1,306 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + +use \OCP\AppFramework\Http; + +use \OCA\News\Db\Folder; +use \OCA\News\Db\Feed; +use \OCA\News\Service\ServiceNotFoundException; +use \OCA\News\Service\ServiceConflictException; +use \OCA\News\Service\ServiceValidationException; + + +class FolderControllerTest extends \PHPUnit_Framework_TestCase { + + private $appName; + private $folderService; + private $itemService; + private $feedService; + private $request; + private $controller; + private $msg; + + + /** + * Gets run before each test + */ + public function setUp(){ + $this->appName = 'news'; + $this->user = 'jack'; + $this->folderService = $this->getMockBuilder( + '\OCA\News\Service\FolderService') + ->disableOriginalConstructor() + ->getMock(); + $this->feedService = $this->getMockBuilder( + '\OCA\News\Service\FeedService') + ->disableOriginalConstructor() + ->getMock(); + $this->itemService = $this->getMockBuilder( + '\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->controller = new FolderController($this->appName, $this->request, + $this->folderService, + $this->feedService, + $this->itemService, + $this->user); + $this->msg = 'ron'; + } + + + + public function testIndex(){ + $return = [new Folder(), new Folder()]; + $this->folderService->expects($this->once()) + ->method('findAll') + ->will($this->returnValue($return)); + + $response = $this->controller->index(); + $expected = ['folders' => $return]; + $this->assertEquals($expected, $response); + } + + + public function testOpen(){ + $this->folderService->expects($this->once()) + ->method('open') + ->with($this->equalTo(3), + $this->equalTo(true), $this->equalTo($this->user)); + + $this->controller->open(3, true); + + } + + + public function testOpenDoesNotExist(){ + $this->folderService->expects($this->once()) + ->method('open') + ->will($this->throwException( + new ServiceNotFoundException($this->msg)) + ); + + $response = $this->controller->open(5, true); + + $params = json_decode($response->render(), true); + + $this->assertEquals($th