From 2bfa9f22b7d97dcdbda5992ad603d9d10482b49f Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 14 May 2014 01:04:57 +0200 Subject: fix unittests --- tests/unit/controller/ExportControllerTest.php | 20 +- tests/unit/controller/FeedApiControllerTest.php | 256 +++----------- tests/unit/controller/FeedControllerTest.php | 268 ++++----------- tests/unit/controller/FolderApiControllerTest.php | 191 +---------- tests/unit/controller/FolderControllerTest.php | 179 +++------- tests/unit/controller/ItemApiControllerTest.php | 379 +++------------------ tests/unit/controller/ItemControllerTest.php | 234 +++---------- tests/unit/controller/PageControllerTest.php | 50 +-- tests/unit/controller/UtilityApiControllerTest.php | 64 +--- tests/unit/db/FeedMapperTest.php | 12 +- tests/unit/db/FolderMapperTest.php | 8 +- tests/unit/db/ItemMapperTest.php | 9 +- tests/unit/db/ItemTest.php | 20 +- tests/unit/db/MapperFactoryTest.php | 2 +- tests/unit/db/postgres/ItemMapperTest.php | 7 +- 15 files changed, 312 insertions(+), 1387 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/controller/ExportControllerTest.php b/tests/unit/controller/ExportControllerTest.php index 820784d18..f41977241 100644 --- a/tests/unit/controller/ExportControllerTest.php +++ b/tests/unit/controller/ExportControllerTest.php @@ -13,11 +13,9 @@ namespace OCA\News\Controller; -use \OCP\IRequest; use \OCP\AppFramework\Http; use \OCA\News\Http\TextDownloadResponse; -use \OCA\News\Utility\ControllerTestUtility; use \OCA\News\Utility\OPMLExporter; use \OCA\News\Db\Item; use \OCA\News\Db\Feed; @@ -25,7 +23,7 @@ use \OCA\News\Db\Feed; require_once(__DIR__ . "/../../classloader.php"); -class ExportControllerTest extends ControllerTestUtility { +class ExportControllerTest extends \PHPUnit_Framework_TestCase { private $appName; private $request; @@ -51,7 +49,9 @@ class ExportControllerTest extends ControllerTestUtility { $this->folderBusinessLayer = $this->getMockBuilder('\OCA\News\BusinessLayer\FolderBusinessLayer') ->disableOriginalConstructor() ->getMock(); - $this->request = $this->getRequest(); + $this->request = $this->getMockBuilder('\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); $this->opmlExporter = new OPMLExporter(); $this->controller = new ExportController($this->appName, $this->request, $this->feedBusinessLayer, $this->folderBusinessLayer, @@ -59,18 +59,6 @@ class ExportControllerTest extends ControllerTestUtility { } - public function testOpmlAnnotations(){ - $annotations = array('NoAdminRequired', 'NoCSRFRequired'); - $this->assertAnnotations($this->controller, 'opml', $annotations); - } - - - public function testArticlesAnnotations(){ - $annotations = array('NoAdminRequired', 'NoCSRFRequired'); - $this->assertAnnotations($this->controller, 'articles', $annotations); - } - - public function testOpmlExportNoFeeds(){ $opml = "\n" . diff --git a/tests/unit/controller/FeedApiControllerTest.php b/tests/unit/controller/FeedApiControllerTest.php index 220e89ea9..1444fa563 100644 --- a/tests/unit/controller/FeedApiControllerTest.php +++ b/tests/unit/controller/FeedApiControllerTest.php @@ -13,10 +13,8 @@ namespace OCA\News\Controller; -use \OCP\IRequest; use \OCP\AppFramework\Http; -use \OCA\News\Utility\ControllerTestUtility; use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\BusinessLayer\BusinessLayerConflictException; use \OCA\News\Db\Folder; @@ -26,7 +24,7 @@ use \OCA\News\Db\Item; require_once(__DIR__ . "/../../classloader.php"); -class FeedApiControllerTest extends ControllerTestUtility { +class FeedApiControllerTest extends \PHPUnit_Framework_TestCase { private $folderBusinessLayer; private $feedBusinessLayer; @@ -37,11 +35,13 @@ class FeedApiControllerTest extends ControllerTestUtility { private $request; private $msg; private $logger; + private $loggerParams; protected function setUp() { $this->user = 'tom'; + $this->loggerParams = array('hi'); $this->logger = $this->getMockBuilder( - '\OCA\News\Core\Logger') + '\OCP\ILogger') ->disableOriginalConstructor() ->getMock(); $this->appName = 'news'; @@ -68,55 +68,13 @@ class FeedApiControllerTest extends ControllerTestUtility { $this->feedBusinessLayer, $this->itemBusinessLayer, $this->logger, - $this->user + $this->user, + $this->loggerParams ); $this->msg = 'hohoho'; } - private function assertDefaultAnnotations($methodName){ - $annotations = array('NoAdminRequired', 'NoCSRFRequired', 'API'); - $this->assertAnnotations($this->feedAPI, $methodName, $annotations); - } - - - public function testGetAllAnnotations(){ - $this->assertDefaultAnnotations('index'); - } - - - public function testCreateAnnotations(){ - $this->assertDefaultAnnotations('create'); - } - - - public function testDeleteAnnotations(){ - $this->assertDefaultAnnotations('delete'); - } - - - public function testMoveAnnotations(){ - $this->assertDefaultAnnotations('move'); - } - - - public function testReadAnnotations(){ - $this->assertDefaultAnnotations('read'); - } - - - public function testFromUsersAnnotations(){ - $annotations = array('NoCSRFRequired', 'API'); - $this->assertAnnotations($this->feedAPI, 'fromAllUsers', $annotations); - } - - - public function testUpdateAnnotations(){ - $annotations = array('NoCSRFRequired'); - $this->assertAnnotations($this->feedAPI, 'update', $annotations); - } - - public function testIndex() { $feeds = array( new Feed() @@ -143,7 +101,7 @@ class FeedApiControllerTest extends ControllerTestUtility { 'feeds' => array($feeds[0]->toAPI()), 'starredCount' => $starredCount, 'newestItemId' => $newestItemId - ), $response->getData()); + ), $response); } @@ -171,56 +129,27 @@ class FeedApiControllerTest extends ControllerTestUtility { $this->assertEquals(array( 'feeds' => array($feeds[0]->toAPI()), 'starredCount' => $starredCount, - ), $response->getData()); + ), $response); } public function testDelete() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2 - ))); - $this->feedAPI = new FeedApiController( - $this->appName, - $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->logger, - $this->user - ); - $this->feedBusinessLayer->expects($this->once()) ->method('delete') ->with( $this->equalTo(2), $this->equalTo($this->user)); - $response = $this->feedAPI->delete(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->feedAPI->delete(2); } public function testDeleteDoesNotExist() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2 - ))); - $this->feedAPI = new FeedApiController( - $this->appName, - $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->logger, - $this->user - ); - $this->feedBusinessLayer->expects($this->once()) ->method('delete') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->feedAPI->delete(); + $response = $this->feedAPI->delete(2); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -232,19 +161,6 @@ class FeedApiControllerTest extends ControllerTestUtility { $feeds = array( new Feed() ); - $request = $this->getRequest(array('params' => array( - 'url' => 'ho', - 'folderId' => 3 - ))); - $this->feedAPI = new FeedApiController( - $this->appName, - $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->logger, - $this->user - ); $this->feedBusinessLayer->expects($this->once()) ->method('purgeDeleted') @@ -252,7 +168,7 @@ class FeedApiControllerTest extends ControllerTestUtility { $this->feedBusinessLayer->expects($this->once()) ->method('create') ->with( - $this->equalTo('ho'), + $this->equalTo('url'), $this->equalTo(3), $this->equalTo($this->user)) ->will($this->returnValue($feeds[0])); @@ -260,14 +176,12 @@ class FeedApiControllerTest extends ControllerTestUtility { ->method('getNewestItemId') ->will($this->returnValue(3)); - $response = $this->feedAPI->create(); + $response = $this->feedAPI->create('url', 3); $this->assertEquals(array( 'feeds' => array($feeds[0]->toAPI()), 'newestItemId' => 3 - ), $response->getData()); - - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + ), $response); } @@ -275,19 +189,6 @@ class FeedApiControllerTest extends ControllerTestUtility { $feeds = array( new Feed() ); - $request = $this->getRequest(array('params' => array( - 'url' => 'ho', - 'folderId' => 3 - ))); - $this->feedAPI = new FeedApiController( - $this->appName, - $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->logger, - $this->user - ); $this->feedBusinessLayer->expects($this->once()) ->method('purgeDeleted') @@ -303,13 +204,11 @@ class FeedApiControllerTest extends ControllerTestUtility { ->method('getNewestItemId') ->will($this->throwException(new BusinessLayerException(''))); - $response = $this->feedAPI->create(); + $response = $this->feedAPI->create('ho', 3); $this->assertEquals(array( 'feeds' => array($feeds[0]->toAPI()) - ), $response->getData()); - - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + ), $response); } @@ -322,7 +221,7 @@ class FeedApiControllerTest extends ControllerTestUtility { ->method('create') ->will($this->throwException(new BusinessLayerConflictException($this->msg))); - $response = $this->feedAPI->create(); + $response = $this->feedAPI->create('ho', 3); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -335,7 +234,7 @@ class FeedApiControllerTest extends ControllerTestUtility { ->method('create') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->feedAPI->create(); + $response = $this->feedAPI->create('ho', 3); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -344,24 +243,6 @@ class FeedApiControllerTest extends ControllerTestUtility { public function testRead() { - $request = $this->getRequest(array( - 'urlParams' => array( - 'feedId' => 3 - ), - 'params' => array( - 'newestItemId' => 30, - ) - )); - $this->feedAPI = new FeedApiController( - $this->appName, - $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->logger, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('readFeed') ->with( @@ -369,32 +250,11 @@ class FeedApiControllerTest extends ControllerTestUtility { $this->equalTo(30), $this->equalTo($this->user)); - $response = $this->feedAPI->read(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->feedAPI->read(3, 30); } public function testMove() { - $request = $this->getRequest(array( - 'urlParams' => array( - 'feedId' => 3 - ), - 'params' => array( - 'folderId' => 30, - ) - )); - $this->feedAPI = new FeedApiController( - $this->appName, - $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->logger, - $this->user - ); - $this->feedBusinessLayer->expects($this->once()) ->method('move') ->with( @@ -402,10 +262,20 @@ class FeedApiControllerTest extends ControllerTestUtility { $this->equalTo(30), $this->equalTo($this->user)); - $response = $this->feedAPI->move(); + $this->feedAPI->move(3, 30); + } + + + public function testMoveDoesNotExist() { + $this->feedBusinessLayer->expects($this->once()) + ->method('move') + ->will($this->throwException(new BusinessLayerException($this->msg))); + + $response = $this->feedAPI->move(3, 4); - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $data = $response->getData(); + $this->assertEquals($this->msg, $data['message']); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); } @@ -413,24 +283,6 @@ class FeedApiControllerTest extends ControllerTestUtility { $feedId = 3; $feedTitle = 'test'; - $request = $this->getRequest(array( - 'urlParams' => array( - 'feedId' => $feedId - ), - 'params' => array( - 'feedTitle' => $feedTitle - ) - )); - $this->feedAPI = new FeedApiController( - $this->appName, - $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->logger, - $this->user - ); - $this->feedBusinessLayer->expects($this->once()) ->method('rename') ->with( @@ -438,23 +290,7 @@ class FeedApiControllerTest extends ControllerTestUtility { $this->equalTo($feedTitle), $this->equalTo($this->user)); - $response = $this->feedAPI->rename(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - } - - - public function testMoveDoesNotExist() { - $this->feedBusinessLayer->expects($this->once()) - ->method('move') - ->will($this->throwException(new BusinessLayerException($this->msg))); - - $response = $this->feedAPI->move(); - - $data = $response->getData(); - $this->assertEquals($this->msg, $data['message']); - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + $this->feedAPI->rename($feedId, $feedTitle); } @@ -467,45 +303,35 @@ class FeedApiControllerTest extends ControllerTestUtility { $this->feedBusinessLayer->expects($this->once()) ->method('findAllFromAllUsers') ->will($this->returnValue($feeds)); - $response = $this->feedAPI->fromAllUsers(); - $this->assertEquals('{"feeds":[{"id":1,"userId":"john"}]}', $response->render()); + $response = json_encode($this->feedAPI->fromAllUsers()); + $this->assertEquals('{"feeds":[{"id":1,"userId":"john"}]}', $response); } public function testUpdate() { $feedId = 3; $userId = 'hi'; - $request = $this->getRequest(array('params' => array( - 'feedId' => $feedId, - 'userId' => $userId - ))); - $this->feedAPI = new FeedApiController( - $this->appName, - $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->logger, - $this->user - ); + $this->feedBusinessLayer->expects($this->once()) ->method('update') ->with($this->equalTo($feedId), $this->equalTo($userId)); - $this->feedAPI->update(); + $this->feedAPI->update($userId, $feedId); } public function testUpdateError() { + $feedId = 3; + $userId = 'hi'; $this->feedBusinessLayer->expects($this->once()) ->method('update') ->will($this->throwException(new \Exception($this->msg))); $this->logger->expects($this->once()) - ->method('log') + ->method('debug') ->with($this->equalTo('Could not update feed ' . $this->msg), - $this->equalTo('debug')); + $this->equalTo($this->loggerParams)); - $this->feedAPI->update(); + $this->feedAPI->update($userId, $feedId); } diff --git a/tests/unit/controller/FeedControllerTest.php b/tests/unit/controller/FeedControllerTest.php index 4238b1faa..ba9d86a8a 100644 --- a/tests/unit/controller/FeedControllerTest.php +++ b/tests/unit/controller/FeedControllerTest.php @@ -13,10 +13,8 @@ namespace OCA\News\Controller; -use \OCP\IRequest; use \OCP\AppFramework\Http; -use \OCA\News\Utility\ControllerTestUtility; use \OCA\News\Db\Feed; use \OCA\News\Db\FeedType; use \OCA\News\BusinessLayer\BusinessLayerException; @@ -25,7 +23,7 @@ use \OCA\News\BusinessLayer\BusinessLayerConflictException; require_once(__DIR__ . "/../../classloader.php"); -class FeedControllerTest extends ControllerTestUtility { +class FeedControllerTest extends \PHPUnit_Framework_TestCase { private $appName; private $feedBusinessLayer; @@ -55,7 +53,10 @@ class FeedControllerTest extends ControllerTestUtility { $this->folderBusinessLayer = $this->getMockBuilder('\OCA\News\BusinessLayer\FolderBusinessLayer') ->disableOriginalConstructor() ->getMock(); - $this->request = $this->getRequest(); + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); $this->controller = new FeedController($this->appName, $this->request, $this->folderBusinessLayer, $this->feedBusinessLayer, @@ -64,70 +65,6 @@ class FeedControllerTest extends ControllerTestUtility { $this->user); } - private function assertFeedControllerAnnotations($methodName){ - $annotations = array('NoAdminRequired'); - $this->assertAnnotations($this->controller, $methodName, $annotations); - } - - - private function getPostController($postValue, $url=array()){ - $post = array( - 'post' => $postValue, - 'urlParams' => $url - ); - - $request = $this->getRequest($post); - return new FeedController($this->appName, $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->settings, - $this->user); - } - - - public function testFeedsAnnotations(){ - $this->assertFeedControllerAnnotations('index'); - } - - - public function testActiveAnnotations(){ - $this->assertFeedControllerAnnotations('active'); - } - - - public function testCreateAnnotations(){ - $this->assertFeedControllerAnnotations('create'); - } - - - public function testDeleteAnnotations(){ - $this->assertFeedControllerAnnotations('delete'); - } - - - public function testRestoreAnnotations(){ - $this->assertFeedControllerAnnotations('restore'); - } - - - public function testUpdateAnnotations(){ - $this->assertFeedControllerAnnotations('update'); - } - - - public function testMoveAnnotations(){ - $this->assertFeedControllerAnnotations('move'); - } - - - public function testImportArticlesAnnotations(){ - $this->assertFeedControllerAnnotations('import'); - } - - public function testReadAnnotations(){ - $this->assertFeedControllerAnnotations('read'); - } public function testIndex(){ $result = array( @@ -151,7 +88,7 @@ class FeedControllerTest extends ControllerTestUtility { $response = $this->controller->index(); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -178,7 +115,7 @@ class FeedControllerTest extends ControllerTestUtility { $response = $this->controller->index(); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -213,7 +150,7 @@ class FeedControllerTest extends ControllerTestUtility { $response = $this->controller->active(); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -236,7 +173,7 @@ class FeedControllerTest extends ControllerTestUtility { $response = $this->controller->active(); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -259,7 +196,7 @@ class FeedControllerTest extends ControllerTestUtility { $response = $this->controller->active(); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -277,7 +214,7 @@ class FeedControllerTest extends ControllerTestUtility { $response = $this->controller->active(); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -287,13 +224,6 @@ class FeedControllerTest extends ControllerTestUtility { 'newestItemId' => 3 ); - $post = array( - 'url' => 'hi', - 'parentFolderId' => 4 - ); - $this->controller = $this->getPostController($post); - - $this->itemBusinessLayer->expects($this->once()) ->method('getNewestItemId') ->will($this->returnValue($result['newestItemId'])); @@ -302,14 +232,14 @@ class FeedControllerTest extends ControllerTestUtility { ->with($this->equalTo($this->user), $this->equalTo(false)); $this->feedBusinessLayer->expects($this->once()) ->method('create') - ->with($this->equalTo($post['url']), - $this->equalTo($post['parentFolderId']), + ->with($this->equalTo('hi'), + $this->equalTo(4), $this->equalTo($this->user)) ->will($this->returnValue($result['feeds'][0])); - $response = $this->controller->create(); + $response = $this->controller->create('hi', 4); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -318,12 +248,6 @@ class FeedControllerTest extends ControllerTestUtility { 'feeds' => array(new Feed()) ); - $post = array( - 'url' => 'hi', - 'parentFolderId' => 4 - ); - $this->controller = $this->getPostController($post); - $this->feedBusinessLayer->expects($this->once()) ->method('purgeDeleted') ->with($this->equalTo($this->user), $this->equalTo(false)); @@ -334,14 +258,14 @@ class FeedControllerTest extends ControllerTestUtility { $this->feedBusinessLayer->expects($this->once()) ->method('create') - ->with($this->equalTo($post['url']), - $this->equalTo($post['parentFolderId']), + ->with($this->equalTo('hi'), + $this->equalTo(4), $this->equalTo($this->user)) ->will($this->returnValue($result['feeds'][0])); - $response = $this->controller->create(); + $response = $this->controller->create('hi', 4); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -355,10 +279,10 @@ class FeedControllerTest extends ControllerTestUtility { ->method('create') ->will($this->throwException($ex)); - $response = $this->controller->create(); + $response = $this->controller->create('hi', 4); $params = json_decode($response->render(), true); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); $this->assertEquals($response->getStatus(), Http::STATUS_UNPROCESSABLE_ENTITY); } @@ -373,43 +297,34 @@ class FeedControllerTest extends ControllerTestUtility { ->method('create') ->will($this->throwException($ex)); - $response = $this->controller->create(); + $response = $this->controller->create('hi', 4); $params = json_decode($response->render(), true); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); $this->assertEquals($response->getStatus(), Http::STATUS_CONFLICT); } public function testDelete(){ - $url = array( - 'feedId' => 4 - ); - $this->controller = $this->getPostController(array(), $url); - $this->feedBusinessLayer->expects($this->once()) ->method('markDeleted') - ->with($this->equalTo($url['feedId'])); + ->with($this->equalTo(4)); - $this->controller->delete(); + $this->controller->delete(4); } public function testDeleteDoesNotExist(){ - $url = array( - 'feedId' => 4 - ); $msg = 'hehe'; - $this->controller = $this->getPostController(array(), $url); $this->feedBusinessLayer->expects($this->once()) ->method('markDeleted') ->will($this->throwException(new BusinessLayerException($msg))); - $response = $this->controller->delete(); + $response = $this->controller->delete(4); $params = json_decode($response->render(), true); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); } @@ -427,125 +342,84 @@ class FeedControllerTest extends ControllerTestUtility { ) ); - $url = array( - 'feedId' => 4 - ); - $this->controller = $this->getPostController(array(), $url); - $this->feedBusinessLayer->expects($this->once()) ->method('update') - ->with($this->equalTo($url['feedId']), $this->equalTo($this->user)) + ->with($this->equalTo(4), $this->equalTo($this->user)) ->will($this->returnValue($feed)); - $response = $this->controller->update(); + $response = $this->controller->update(4); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } public function testUpdateReturnsJSONError(){ - $url = array( - 'feedId' => 4 - ); - $this->controller = $this->getPostController(array(), $url); - $this->feedBusinessLayer->expects($this->once()) ->method('update') - ->with($this->equalTo($url['feedId']), $this->equalTo($this->user)) + ->with($this->equalTo(4), $this->equalTo($this->user)) ->will($this->throwException(new BusinessLayerException('NO!'))); - $response = $this->controller->update(); + $response = $this->controller->update(4); $render = $response->render(); - $this->assertEquals('{"msg":"NO!"}', $render); + $this->assertEquals('{"message":"NO!"}', $render); $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); } public function testMove(){ - $post = array( - 'parentFolderId' => 3 - ); - $url = array( - 'feedId' => 4 - ); - $this->controller = $this->getPostController($post, $url); - $this->feedBusinessLayer->expects($this->once()) ->method('move') - ->with($this->equalTo($url['feedId']), - $this->equalTo($post['parentFolderId']), + ->with($this->equalTo(4), + $this->equalTo(3), $this->equalTo($this->user)); - $this->controller->move(); + $this->controller->move(4, 3); } public function testMoveDoesNotExist(){ - $post = array( - 'parentFolderId' => 3 - ); - $url = array( - 'feedId' => 4 - ); $msg = 'john'; - $this->controller = $this->getPostController($post, $url); $this->feedBusinessLayer->expects($this->once()) ->method('move') ->will($this->throwException(new BusinessLayerException($msg))); - $response = $this->controller->move(); + $response = $this->controller->move(4, 3); $params = json_decode($response->render(), true); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); } public function testRename(){ - $post = array( - 'feedTitle' => "New Feed Title" - ); - $url = array( - 'feedId' => 4 - ); - $this->controller = $this->getPostController($post, $url); - $this->feedBusinessLayer->expects($this->once()) ->method('rename') - ->with($this->equalTo($url['feedId']), - $this->equalTo($post['feedTitle']), + ->with($this->equalTo(4), + $this->equalTo('title'), $this->equalTo($this->user)); - $this->controller->rename(); + $this->controller->rename(4, 'title'); } public function testRenameDoesNotExist(){ - $post = array( - 'feedTitle' => "New Feed Title" - ); - $url = array( - 'feedId' => 4 - ); - $this->controller = $this->getPostController($post, $url); - $msg = 'hi'; $this->feedBusinessLayer->expects($this->once()) ->method('rename') - ->with($this->equalTo($url['feedId']), - $this->equalTo($post['feedTitle']), + ->with($this->equalTo(4), + $this->equalTo('title'), $this->equalTo($this->user)) ->will($this->throwException(new BusinessLayerException($msg))); - $response = $this->controller->rename(); + $response = $this->controller->rename(4, 'title'); $params = $response->getData(); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); } @@ -553,53 +427,36 @@ class FeedControllerTest extends ControllerTestUtility { public function testImport() { $feed = new Feed(); - $post = array( - 'json' => 'the json' - ); $expected = array( 'feeds' => array($feed) ); - $this->controller = $this->getPostController($post); $this->feedBusinessLayer->expects($this->once()) ->method('importArticles') - ->with($this->equalTo($post['json']), + ->with($this->equalTo('json'), $this->equalTo($this->user)) ->will($this->returnValue($feed)); - $response = $this->controller->import(); + $response = $this->controller->import('json'); - $this->assertEquals($expected, $response->getData()); + $this->assertEquals($expected, $response); } public function testImportCreatesNoAdditionalFeed() { - $post = array( - 'json' => 'the json' - ); - $expected = array(); - $this->controller = $this->getPostController($post); - $this->feedBusinessLayer->expects($this->once()) ->method('importArticles') - ->with($this->equalTo($post['json']), + ->with($this->equalTo('json'), $this->equalTo($this->user)) ->will($this->returnValue(null)); - $response = $this->controller->import(); + $response = $this->controller->import('json'); - $this->assertEquals($expected, $response->getData()); + $this->assertEquals(array(), $response); } public function testReadFeed(){ - $url = array( - 'feedId' => 4 - ); - $post = array( - 'highestItemId' => 5 - ); - $this->controller = $this->getPostController($post, $url); $expected = array( 'feeds' => array( array( @@ -611,42 +468,33 @@ class FeedControllerTest extends ControllerTestUtility { $this->itemBusinessLayer->expects($this->once()) ->method('readFeed') - ->with($url['feedId'], $post['highestItemId'], $this->user); + ->with($this->equalTo(4), $this->equalTo(5), $this->user); - $response = $this->controller->read(); - $this->assertEquals($expected, $response->getData()); + $response = $this->controller->read(4, 5); + $this->assertEquals($expected, $response); } public function testRestore() { - $url = array( - 'feedId' => 4 - ); - $this->controller = $this->getPostController(array(), $url); - $this->feedBusinessLayer->expects($this->once()) ->method('unmarkDeleted') - ->with($this->equalTo($url['feedId'])); + ->with($this->equalTo(4)); - $this->controller->restore(); + $this->controller->restore(4); } public function testRestoreDoesNotExist(){ - $url = array( - 'feedId' => 4 - ); $msg = 'hehe'; - $this->controller = $this->getPostController(array(), $url); $this->feedBusinessLayer->expects($this->once()) ->method('unmarkDeleted') ->will($this->throwException(new BusinessLayerException($msg))); - $response = $this->controller->restore(); + $response = $this->controller->restore(4); $params = json_decode($response->render(), true); - $this->assertEquals($msg, $params['msg']); + $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 index aa134f58e..cf23984ce 100644 --- a/tests/unit/controller/FolderApiControllerTest.php +++ b/tests/unit/controller/FolderApiControllerTest.php @@ -13,11 +13,9 @@ namespace OCA\News\Controller; -use \OCP\IRequest; use \OCP\AppFramework\Http; use \OCP\AppFramework\Http\JSONResponse; -use \OCA\News\Utility\ControllerTestUtility; use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\BusinessLayer\BusinessLayerConflictException; use \OCA\News\BusinessLayer\BusinessLayerValidationException; @@ -29,7 +27,7 @@ use \OCA\News\Db\Item; require_once(__DIR__ . "/../../classloader.php"); -class FolderApiControllerTest extends ControllerTestUtility { +class FolderApiControllerTest extends \PHPUnit_Framework_TestCase { private $folderBusinessLayer; private $itemBusinessLayer; @@ -65,37 +63,6 @@ class FolderApiControllerTest extends ControllerTestUtility { } - private function assertDefaultAnnotations($methodName){ - $annotations = array('NoAdminRequired', 'NoCSRFRequired', 'API'); - $this->assertAnnotations($this->folderAPI, $methodName, $annotations); - } - - - public function testIndexAnnotations(){ - $this->assertDefaultAnnotations('index'); - } - - - public function testCreateAnnotations(){ - $this->assertDefaultAnnotations('create'); - } - - - public function testDeleteAnnotations(){ - $this->assertDefaultAnnotations('delete'); - } - - - public function testUpdateAnnotations(){ - $this->assertDefaultAnnotations('update'); - } - - - public function testReadAnnotations(){ - $this->assertDefaultAnnotations('read'); - } - - public function testIndex() { $folders = array( new Folder() @@ -108,9 +75,7 @@ class FolderApiControllerTest extends ControllerTestUtility { $response = $this->folderAPI->index(); - $this->assertEquals(array( - 'folders' => array($folders[0]->toAPI()) - ), $response->getData()); + $this->assertEquals($folders, $response); } @@ -121,16 +86,7 @@ class FolderApiControllerTest extends ControllerTestUtility { $folders = array( $folder ); - $this->folderAPI = new FolderApiController( - $this->appName, - $this->getRequest(array('params' => array( - 'name' => $folderName - ))), - $this->folderBusinessLayer, - $this->itemBusinessLayer, - $this->user - ); - + $this->folderBusinessLayer->expects($this->once()) ->method('purgeDeleted') ->with($this->equalTo($this->user), $this->equalTo(false)); @@ -139,11 +95,9 @@ class FolderApiControllerTest extends ControllerTestUtility { ->with($this->equalTo($folderName), $this->equalTo($this->user)) ->will($this->returnValue($folder)); - $response = $this->folderAPI->create(); + $response = $this->folderAPI->create($folderName); - $this->assertEquals(array( - 'folders' => array($folders[0]->toAPI()) - ), $response->getData()); + $this->assertEquals($folder, $response); } @@ -157,7 +111,7 @@ class FolderApiControllerTest extends ControllerTestUtility { ->method('create') ->will($this->throwException(new BusinessLayerConflictException($msg))); - $response = $this->folderAPI->create(); + $response = $this->folderAPI->create('hi'); $data = $response->getData(); $this->assertEquals($msg, $data['message']); @@ -175,7 +129,7 @@ class FolderApiControllerTest extends ControllerTestUtility { ->method('create') ->will($this->throwException(new BusinessLayerValidationException($msg))); - $response = $this->folderAPI->create(); + $response = $this->folderAPI->create('hi'); $data = $response->getData(); $this->assertEquals($msg, $data['message']); @@ -185,45 +139,22 @@ class FolderApiControllerTest extends ControllerTestUtility { public function testDelete() { $folderId = 23; - - $this->folderAPI = new FolderApiController( - $this->appName, - $this->getRequest(array('urlParams' => array( - 'folderId' => $folderId - ))), - $this->folderBusinessLayer, - $this->itemBusinessLayer, - $this->user - ); - $this->folderBusinessLayer->expects($this->once()) ->method('delete') ->with($this->equalTo($folderId), $this->equalTo($this->user)); - $response = $this->folderAPI->delete(); - - $this->assertEmpty($response->getData()); + $response = $this->folderAPI->delete(23); } public function testDeleteDoesNotExist() { $folderId = 23; - $this->folderAPI = new FolderApiController( - $this->appName, - $this->getRequest(array('urlParams' => array( - 'folderId' => $folderId - ))), - $this->folderBusinessLayer, - $this->itemBusinessLayer, - $this->user - ); - $this->folderBusinessLayer->expects($this->once()) ->method('delete') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->folderAPI->delete(); + $response = $this->folderAPI->delete($folderId); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -235,63 +166,24 @@ class FolderApiControllerTest extends ControllerTestUtility { $folderId = 23; $folderName = 'test'; - $this->folderAPI = new FolderApiController( - $this->appName, - $this->getRequest( - array( - 'urlParams' => array( - 'folderId' => $folderId - ), - - 'params' => array( - 'name' => $folderName - ) - ) - ), - $this->folderBusinessLayer, - $this->itemBusinessLayer, - $this->user - ); - $this->folderBusinessLayer->expects($this->once()) ->method('rename') ->with($this->equalTo($folderId), $this->equalTo($folderName), $this->equalTo($this->user)); - $response = $this->folderAPI->update(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->folderAPI->update($folderId, $folderName); } public function testUpdateDoesNotExist() { $folderId = 23; $folderName = 'test'; - $this->folderAPI = new FolderApiController( - $this->appName, - $this->getRequest( - array( - 'urlParams' => array( - 'folderId' => $folderId - ), - - 'params' => array( - 'name' => $folderName - ) - ) - ), - $this->folderBusinessLayer, - $this->itemBusinessLayer, - $this->user - ); - $this->folderBusinessLayer->expects($this->once()) ->method('rename') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->folderAPI->update(); + $response = $this->folderAPI->update($folderId, $folderName); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -303,29 +195,11 @@ class FolderApiControllerTest extends ControllerTestUtility { $folderId = 23; $folderName = 'test'; - $this->folderAPI = new FolderApiController( - $this->appName, - $this->getRequest( - array( - 'urlParams' => array( - 'folderId' => $folderId - ), - - 'params' => array( - 'name' => $folderName - ) - ) - ), - $this->folderBusinessLayer, - $this->itemBusinessLayer, - $this->user - ); - $this->folderBusinessLayer->expects($this->once()) ->method('rename') ->will($this->throwException(new BusinessLayerConflictException($this->msg))); - $response = $this->folderAPI->update(); + $response = $this->folderAPI->update($folderId, $folderName); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -337,29 +211,11 @@ class FolderApiControllerTest extends ControllerTestUtility { $folderId = 23; $folderName = ''; - $this->folderAPI = new FolderApiController( - $this->appName, - $this->getRequest( - array( - 'urlParams' => array( - 'folderId' => $folderId - ), - - 'params' => array( - 'name' => $folderName - ) - ) - ), - $this->folderBusinessLayer, - $this->itemBusinessLayer, - $this->user - ); - $this->folderBusinessLayer->expects($this->once()) ->method('rename') ->will($this->throwException(new BusinessLayerValidationException($this->msg))); - $response = $this->folderAPI->update(); + $response = $this->folderAPI->update($folderId, $folderName); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -368,22 +224,6 @@ class FolderApiControllerTest extends ControllerTestUtility { public function testRead() { - $request = $this->getRequest(array( - 'urlParams' => array( - 'folderId' => 3 - ), - 'params' => array( - 'newestItemId' => 30, - ) - )); - $this->folderAPI = new FolderApiController( - $this->appName, - $request, - $this->folderBusinessLayer, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('readFolder') ->with( @@ -391,10 +231,7 @@ class FolderApiControllerTest extends ControllerTestUtility { $this->equalTo(30), $this->equalTo($this->user)); - $response = $this->folderAPI->read(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->folderAPI->read(3, 30); } diff --git a/tests/unit/controller/FolderControllerTest.php b/tests/unit/controller/FolderControllerTest.php index 44dc6e14c..332b48bbe 100644 --- a/tests/unit/controller/FolderControllerTest.php +++ b/tests/unit/controller/FolderControllerTest.php @@ -13,10 +13,8 @@ namespace OCA\News\Controller; -use \OCP\IRequest; use \OCP\AppFramework\Http; -use \OCA\News\Utility\ControllerTestUtility; use \OCA\News\Db\Folder; use \OCA\News\Db\Feed; use \OCA\News\BusinessLayer\BusinessLayerException; @@ -26,7 +24,7 @@ use \OCA\News\BusinessLayer\BusinessLayerValidationException; require_once(__DIR__ . "/../../classloader.php"); -class FolderControllerTest extends ControllerTestUtility { +class FolderControllerTest extends \PHPUnit_Framework_TestCase { private $appName; private $folderBusinessLayer; @@ -52,7 +50,10 @@ class FolderControllerTest extends ControllerTestUtility { $this->itemBusinessLayer = $this->getMockBuilder('\OCA\News\BusinessLayer\ItemBusinessLayer') ->disableOriginalConstructor() ->getMock(); - $this->request = $this->getRequest(); + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); $this->controller = new FolderController($this->appName, $this->request, $this->folderBusinessLayer, $this->feedBusinessLayer, @@ -62,64 +63,6 @@ class FolderControllerTest extends ControllerTestUtility { } - private function assertFolderControllerAnnotations($methodName){ - $annotations = array('NoAdminRequired'); - $this->assertAnnotations($this->controller, $methodName, $annotations); - } - - - private function getPostController($postValue, $url=array()){ - $post = array( - 'post' => $postValue, - 'urlParams' => $url - ); - - $request = $this->getRequest($post); - return new FolderController($this->appName, $request, - $this->folderBusinessLayer, - $this->feedBusinessLayer, - $this->itemBusinessLayer, - $this->user); - } - - public function testFoldersAnnotations(){ - $this->assertFolderControllerAnnotations('index'); - } - - - public function testOpenAnnotations(){ - $this->assertFolderControllerAnnotations('open'); - } - - - public function testCollapseAnnotations(){ - $this->assertFolderControllerAnnotations('collapse'); - } - - - public function testCreateAnnotations(){ - $this->assertFolderControllerAnnotations('create'); - } - - - public function testDeleteAnnotations(){ - $this->assertFolderControllerAnnotations('delete'); - } - - - public function testRestoreAnnotations(){ - $this->assertFolderControllerAnnotations('restore'); - } - - - public function testRenameAnnotations(){ - $this->assertFolderControllerAnnotations('rename'); - } - - - public function testReadAnnotations(){ - $this->assertFolderControllerAnnotations('read'); - } public function testIndex(){ $return = array( @@ -134,75 +77,61 @@ class FolderControllerTest extends ControllerTestUtility { $expected = array( 'folders' => $return ); - $this->assertEquals($expected, $response->getData()); + $this->assertEquals($expected, $response); } public function testOpen(){ - $url = array('folderId' => 5); - $this->controller = $this->getPostController(array(), $url); - $this->folderBusinessLayer->expects($this->once()) ->method('open') - ->with($this->equalTo($url['folderId']), + ->with($this->equalTo(3), $this->equalTo(true), $this->equalTo($this->user)); - $this->controller->open(); + $this->controller->open(3); } public function testOpenDoesNotExist(){ - $url = array('folderId' => 5); - $this->controller = $this->getPostController(array(), $url); - $this->folderBusinessLayer->expects($this->once()) ->method('open') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->controller->open(); + $response = $this->controller->open(5); $params = json_decode($response->render(), true); - $this->assertEquals($this->msg, $params['msg']); + $this->assertEquals($this->msg, $params['message']); $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); } public function testCollapse(){ - $url = array('folderId' => 5); - $this->controller = $this->getPostController(array(), $url); - $this->folderBusinessLayer->expects($this->once()) ->method('open') - ->with($this->equalTo($url['folderId']), + ->with($this->equalTo(5), $this->equalTo(false), $this->equalTo($this->user)); - $this->controller->collapse(); + $this->controller->collapse(5); } public function testCollapseDoesNotExist(){ - $url = array('folderId' => 5); - $this->controller = $this->getPostController(array(), $url); - $this->folderBusinessLayer->expects($this->once()) ->method('open') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->controller->collapse(); + $response = $this->controller->collapse(5); $params = json_decode($response->render(), true); - $this->assertEquals($this->msg, $params['msg']); + $this->assertEquals($this->msg, $params['message']); $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); } public function testCreate(){ - $post = array('folderName' => 'tech'); - $this->controller = $this->getPostController($post); $result = array( 'folders' => array(new Folder()) ); @@ -212,13 +141,13 @@ class FolderControllerTest extends ControllerTestUtility { ->with($this->equalTo($this->user), $this->equalTo(false)); $this->folderBusinessLayer->expects($this->once()) ->method('create') - ->with($this->equalTo($post['folderName']), + ->with($this->equalTo('tech'), $this->equalTo($this->user)) ->will($this->returnValue($result['folders'][0])); - $response = $this->controller->create(); + $response = $this->controller->create('tech'); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -232,11 +161,11 @@ class FolderControllerTest extends ControllerTestUtility { ->method('create') ->will($this->throwException($ex)); - $response = $this->controller->create(); + $response = $this->controller->create('tech'); $params = json_decode($response->render(), true); $this->assertEquals($response->getStatus(), Http::STATUS_UNPROCESSABLE_ENTITY); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); } @@ -250,62 +179,53 @@ class FolderControllerTest extends ControllerTestUtility { ->method('create') ->will($this->throwException($ex)); - $response = $this->controller->create(); + $response = $this->controller->create('tech'); $params = json_decode($response->render(), true); $this->assertEquals($response->getStatus(), Http::STATUS_CONFLICT); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); } public function testDelete(){ - $url = array('folderId' => 5); - $this->controller = $this->getPostController(array(), $url); - $this->folderBusinessLayer->expects($this->once()) ->method('markDeleted') - ->with($this->equalTo($url['folderId']), + ->with($this->equalTo(5), $this->equalTo($this->user)); - $this->controller->delete(); + $this->controller->delete(5); } public function testDeleteDoesNotExist(){ - $url = array('folderId' => 5); - $this->controller = $this->getPostController(array(), $url); - $this->folderBusinessLayer->expects($this->once()) ->method('markDeleted') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->controller->delete(); + $response = $this->controller->delete(5); $params = json_decode($response->render(), true); - $this->assertEquals($this->msg, $params['msg']); + $this->assertEquals($this->msg, $params['message']); $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); } public function testRename(){ - $post = array('folderName' => 'tech'); - $url = array('folderId' => 4); - $this->controller = $this->getPostController($post, $url); $result = array( 'folders' => array(new Folder()) ); $this->folderBusinessLayer->expects($this->once()) ->method('rename') - ->with($this->equalTo($url['folderId']), - $this->equalTo($post['folderName']), + ->with($this->equalTo(4), + $this->equalTo('tech'), $this->equalTo($this->user)) ->will($this->returnValue($result['folders'][0])); - $response = $this->controller->rename(); + $response = $this->controller->rename('tech', 4); - $this->assertEquals($result, $response->getData()); + $this->assertEquals($result, $response); } @@ -316,11 +236,11 @@ class FolderControllerTest extends ControllerTestUtility { ->method('rename') ->will($this->throwException($ex)); - $response = $this->controller->rename(); + $response = $this->controller->rename('tech', 4); $params = json_decode($response->render(), true); $this->assertEquals($response->getStatus(), Http::STATUS_UNPROCESSABLE_ENTITY); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); } @@ -331,11 +251,11 @@ class FolderControllerTest extends ControllerTestUtility { ->method('rename') ->will($this->throwException($ex)); - $response = $this->controller->rename(); + $response = $this->controller->rename('tech', 5); $params = json_decode($response->render(), true); $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); } @@ -346,70 +266,57 @@ class FolderControllerTest extends ControllerTestUtility { ->method('rename') ->will($this->throwException($ex)); - $response = $this->controller->rename(); + $response = $this->controller->rename('tech', 1); $params = json_decode($response->render(), true); $this->assertEquals($response->getStatus(), Http::STATUS_CONFLICT); - $this->assertEquals($msg, $params['msg']); + $this->assertEquals($msg, $params['message']); } public function testRead(){ $feed = new Feed(); - $url = array( - 'folderId' => 4 - ); - $post = array( - 'highestItemId' => 5 - ); - $this->controller = $this->getPostController($post, $url); $expected = array( 'feeds' => array($feed) ); $this->itemBusinessLayer->expects($this->once()) ->method('readFolder') - ->with($this->equalTo($url['folderId']), - $this->equalTo($post['highestItemId']), + ->with($this->equalTo(4), + $this->equalTo(5), $this->equalTo($this->user)); $this->feedBusinessLayer->expects($this->once()) ->method('findAll') ->with($this->equalTo($this->user)) ->will($this->returnValue(array($feed))); - $response = $this->controller->read(); - $this->assertEquals($expected, $response->getData()); + $response = $this->controller->read(4, 5); + $this->assertEquals($expected, $response); } public function testRestore(){ - $url = array('folderId' => 5); - $this->controller = $this->getPostController(array(), $url); - $this->folderBusinessLayer->expects($this->once()) ->method('unmarkDeleted') - ->with($this->equalTo($url['folderId']), + ->with($this->equalTo(5), $this->equalTo($this->user)); - $this->controller->restore(); + $this->controller->restore(5); } public function testRestoreDoesNotExist(){ - $url = array('folderId' => 5); - $this->controller = $this->getPostController(array(), $url); - $this->folderBusinessLayer->expects($this->once()) ->method('unmarkDeleted') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->controller->restore(); + $response = $this->controller->restore(5); $params = json_decode($response->render(), true); $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); - $this->assertEquals($this->msg, $params['msg']); + $this->assertEquals($this->msg, $params['message']); } } \ No newline at end of file diff --git a/tests/unit/controller/ItemApiControllerTest.php b/tests/unit/controller/ItemApiControllerTest.php index 8375b8c68..848e00ec3 100644 --- a/tests/unit/controller/ItemApiControllerTest.php +++ b/tests/unit/controller/ItemApiControllerTest.php @@ -13,17 +13,15 @@ namespace OCA\News\Controller; -use \OCP\IRequest; use \OCP\AppFramework\Http; -use \OCA\News\Utility\ControllerTestUtility; use \OCA\News\BusinessLayer\BusinessLayerException; use \OCA\News\Db\Item; require_once(__DIR__ . "/../../classloader.php"); -class ItemApiControllerTest extends ControllerTestUtility { +class ItemApiControllerTest extends \PHPUnit_Framework_TestCase { private $itemBusinessLayer; private $itemAPI; @@ -53,84 +51,10 @@ class ItemApiControllerTest extends ControllerTestUtility { } - private function assertDefaultAnnotations($methodName){ - $annotations = array('NoAdminRequired', 'NoCSRFRequired', 'API'); - $this->assertAnnotations($this->itemAPI, $methodName, $annotations); - } - - - public function testIndexAnnotations(){ - $this->assertDefaultAnnotations('index'); - } - - - public function testUpdatedAnnotations(){ - $this->assertDefaultAnnotations('updated'); - } - - - public function testReadAllAnnotations(){ - $this->assertDefaultAnnotations('readAll'); - } - - - public function testReadAnnotations(){ - $this->assertDefaultAnnotations('read'); - } - - - public function testStarAnnotations(){ - $this->assertDefaultAnnotations('star'); - } - - - public function testUnreadAnnotations(){ - $this->assertDefaultAnnotations('unread'); - } - - - public function testUnstarAnnotations(){ - $this->assertDefaultAnnotations('unstar'); - } - - - public function testReadMultipleAnnotations(){ - $this->assertDefaultAnnotations('readMultiple'); - } - - - public function testStarMultipleAnnotations(){ - $this->assertDefaultAnnotations('starMultiple'); - } - - - public function testUnreadMultipleAnnotations(){ - $this->assertDefaultAnnotations('unreadMultiple'); - } - - - public function testUnstarMultipleAnnotations(){ - $this->assertDefaultAnnotations('unstarMultiple'); - } - - public function testIndex() { $items = array( new Item() ); - $request = $this->getRequest(array('params' => array( - 'batchSize' => 30, - 'offset' => 20, - 'type' => 1, - 'id' => 2, - 'getRead' => 'false' - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); $this->itemBusinessLayer->expects($this->once()) ->method('findAll') @@ -144,11 +68,9 @@ class ItemApiControllerTest extends ControllerTestUtility { ) ->will($this->returnValue($items)); - $response = $this->itemAPI->index(); + $response = $this->itemAPI->index(1, 2, false, 30, 20); - $this->assertEquals(array( - 'items' => array($items[0]->toAPI()) - ), $response->getData()); + $this->assertEquals($items, $response); } @@ -156,18 +78,6 @@ class ItemApiControllerTest extends ControllerTestUtility { $items = array( new Item() ); - $request = $this->getRequest(array('params' => array( - 'offset' => 20, - 'type' => 1, - 'id' => 2, - 'getRead' => 'false' - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); $this->itemBusinessLayer->expects($this->once()) ->method('findAll') @@ -175,17 +85,15 @@ class ItemApiControllerTest extends ControllerTestUtility { $this->equalTo(2), $this->equalTo(1), $this->equalTo(20), - $this->equalTo(20), + $this->equalTo(0), $this->equalTo(false), $this->equalTo($this->user) ) ->will($this->returnValue($items)); - $response = $this->itemAPI->index(); + $response = $this->itemAPI->index(1, 2, false); - $this->assertEquals(array( - 'items' => array($items[0]->toAPI()) - ), $response->getData()); + $this->assertEquals($items, $response); } @@ -193,17 +101,6 @@ class ItemApiControllerTest extends ControllerTestUtility { $items = array( new Item() ); - $request = $this->getRequest(array('params' => array( - 'lastModified' => 30, - 'type' => 1, - 'id' => 2, - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); $this->itemBusinessLayer->expects($this->once()) ->method('findAllNew') @@ -216,25 +113,13 @@ class ItemApiControllerTest extends ControllerTestUtility { ) ->will($this->returnValue($items)); - $response = $this->itemAPI->updated(); + $response = $this->itemAPI->updated(1, 2, 30); - $this->assertEquals(array( - 'items' => array($items[0]->toAPI()) - ), $response->getData()); + $this->assertEquals($items, $response); } public function testRead() { - $request = $this->getRequest(array('urlParams' => array( - 'itemId' => 2 - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('read') ->with( @@ -243,29 +128,16 @@ class ItemApiControllerTest extends ControllerTestUtility { $this->equalTo($this->user) ); - $response = $this->itemAPI->read(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->itemAPI->read(2); } public function testReadDoesNotExist() { - $request = $this->getRequest(array('urlParams' => array( - 'itemId' => 2 - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('read') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->itemAPI->read(); + $response = $this->itemAPI->read(2); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -274,16 +146,6 @@ class ItemApiControllerTest extends ControllerTestUtility { public function testUnread() { - $request = $this->getRequest(array('urlParams' => array( - 'itemId' => 2 - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('read') ->with( @@ -292,29 +154,16 @@ class ItemApiControllerTest extends ControllerTestUtility { $this->equalTo($this->user) ); - $response = $this->itemAPI->unread(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->itemAPI->unread(2); } public function testUnreadDoesNotExist() { - $request = $this->getRequest(array('urlParams' => array( - 'itemId' => 2 - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('read') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->itemAPI->unread(); + $response = $this->itemAPI->unread(2); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -323,17 +172,6 @@ class ItemApiControllerTest extends ControllerTestUtility { public function testStar() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2, - 'guidHash' => 'hash' - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('star') ->with( @@ -343,29 +181,16 @@ class ItemApiControllerTest extends ControllerTestUtility { $this->equalTo($this->user) ); - $response = $this->itemAPI->star(); - - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->itemAPI->star(2, 'hash'); } public function testStarDoesNotExist() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2, - 'guidHash' => 'hash' - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('star') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->itemAPI->star(); + $response = $this->itemAPI->star(2, 'test'); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -374,17 +199,6 @@ class ItemApiControllerTest extends ControllerTestUtility { public function testUnstar() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2, - 'guidHash' => 'hash' - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('star') ->with( @@ -394,30 +208,16 @@ class ItemApiControllerTest extends ControllerTestUtility { $this->equalTo($this->user) ); - $response = $this->itemAPI->unstar(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->itemAPI->unstar(2, 'hash'); } public function testUnstarDoesNotExist() { - $request = $this->getRequest(array('urlParams' => array( - 'feedId' => 2, - 'guidHash' => 'hash' - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('star') ->will($this->throwException(new BusinessLayerException($this->msg))); - $response = $this->itemAPI->unstar(); + $response = $this->itemAPI->unstar(2, 'test'); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -426,43 +226,18 @@ class ItemApiControllerTest extends ControllerTestUtility { public function testReadAll() { - $request = $this->getRequest(array( - 'params' => array( - 'newestItemId' => 30, - ) - )); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->once()) ->method('readAll') ->with( $this->equalTo(30), $this->equalTo($this->user)); - $response = $this->itemAPI->readAll(); - - $this->assertEmpty($response->getData()); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->itemAPI->readAll(30); } public function testReadMultiple() { - $request = $this->getRequest(array('params' => array( - 'items' => array(2, 4) - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->at(0)) ->method('read') ->with($this->equalTo(2), @@ -473,22 +248,11 @@ class ItemApiControllerTest extends ControllerTestUtility { ->with($this->equalTo(4), $this->equalTo(true), $this->equalTo($this->user)); - $response = $this->itemAPI->readMultiple(); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->itemAPI->readMultiple(array(2, 4)); } public function testReadMultipleDoesntCareAboutException() { - $request = $this->getRequest(array('params' => array( - 'items' => array(2, 4) - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->at(0)) ->method('read') ->will($this->throwException(new BusinessLayerException(''))); @@ -497,21 +261,11 @@ class ItemApiControllerTest extends ControllerTestUtility { ->with($this->equalTo(4), $this->equalTo(true), $this->equalTo($this->user)); - $this->itemAPI->readMultiple(); + $this->itemAPI->readMultiple(array(2, 4)); } public function testUnreadMultiple() { - $request = $this->getRequest(array('params' => array( - 'items' => array(2, 4) - ))); - $this->itemAPI = new ItemApiController( - $this->appName, - $request, - $this->itemBusinessLayer, - $this->user - ); - $this->itemBusinessLayer->expects($this->at(0)) ->method('read') ->with($this->equalTo(2), @@ -522,30 +276,21 @@ class ItemApiControllerTest extends ControllerTestUtility { ->with($this->equalTo(4), $this->equalTo(false), $this-