From af2fd96825d6ae2c7802e9f3bac9054e85093396 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Tue, 3 Nov 2020 21:56:37 +0100 Subject: Controllers: Use v2 services Signed-off-by: Sean Molenaar --- tests/Unit/Controller/FeedApiControllerTest.php | 119 ++++++++++++---------- tests/Unit/Controller/FolderApiControllerTest.php | 26 ++--- tests/Unit/Controller/ItemApiControllerTest.php | 85 ++++++++-------- 3 files changed, 123 insertions(+), 107 deletions(-) (limited to 'tests') diff --git a/tests/Unit/Controller/FeedApiControllerTest.php b/tests/Unit/Controller/FeedApiControllerTest.php index c87a9ff6c..ace481a66 100644 --- a/tests/Unit/Controller/FeedApiControllerTest.php +++ b/tests/Unit/Controller/FeedApiControllerTest.php @@ -17,7 +17,9 @@ namespace OCA\News\Tests\Unit\Controller; use OCA\News\Controller\FeedApiController; use OCA\News\Service\FeedService; +use OCA\News\Service\FeedServiceV2; use OCA\News\Service\ItemService; +use OCA\News\Service\ItemServiceV2; use OCA\News\Utility\PsrLogger; use \OCP\AppFramework\Http; @@ -34,18 +36,32 @@ use Psr\Log\LoggerInterface; class FeedApiControllerTest extends TestCase { - + /** + * @var \PHPUnit\Framework\MockObject\MockObject|FeedService + */ + private $oldFeedService; + + /** + * @var \PHPUnit\Framework\MockObject\MockObject|FeedServiceV2 + */ private $feedService; + + /** + * @var \PHPUnit\Framework\MockObject\MockObject|ItemService + */ private $itemService; - private $feedAPI; + + /** + * @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface + */ + private $logger; + + private $class; private $user; private $msg; - private $logger; - private $loggerParams; protected function setUp(): void { - $this->loggerParams = ['hi']; $this->logger = $this->getMockBuilder(LoggerInterface::class) ->disableOriginalConstructor() ->getMock(); @@ -65,20 +81,23 @@ class FeedApiControllerTest extends TestCase $this->user->expects($this->any()) ->method('getUID') ->will($this->returnValue('123')); - $this->feedService = $this->getMockBuilder(FeedService::class) + $this->oldFeedService = $this->getMockBuilder(FeedService::class) + ->disableOriginalConstructor() + ->getMock(); + $this->feedService = $this->getMockBuilder(FeedServiceV2::class) ->disableOriginalConstructor() ->getMock(); $this->itemService = $this->getMockBuilder(ItemService::class) ->disableOriginalConstructor() ->getMock(); - $this->feedAPI = new FeedApiController( + $this->class = new FeedApiController( $appName, $request, $userSession, + $this->oldFeedService, $this->feedService, $this->itemService, - $this->logger, - $this->loggerParams + $this->logger ); $this->msg = 'hohoho'; } @@ -103,7 +122,7 @@ class FeedApiControllerTest extends TestCase ->with($this->equalTo($this->user->getUID())) ->will($this->returnValue($feeds)); - $response = $this->feedAPI->index(); + $response = $this->class->index(); $this->assertEquals( [ @@ -133,7 +152,7 @@ class FeedApiControllerTest extends TestCase ->with($this->equalTo($this->user->getUID())) ->will($this->returnValue($feeds)); - $response = $this->feedAPI->index(); + $response = $this->class->index(); $this->assertEquals( [ @@ -153,7 +172,7 @@ class FeedApiControllerTest extends TestCase $this->equalTo(2) ); - $this->feedAPI->delete(2); + $this->class->delete(2); } @@ -167,7 +186,7 @@ class FeedApiControllerTest extends TestCase ) ); - $response = $this->feedAPI->delete(2); + $response = $this->class->delete(2); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -180,21 +199,17 @@ class FeedApiControllerTest extends TestCase $feeds = [new Feed()]; $this->feedService->expects($this->once()) - ->method('purgeDeleted') - ->with($this->equalTo($this->user->getUID()), $this->equalTo(false)); + ->method('purgeDeleted'); + $this->feedService->expects($this->once()) ->method('create') - ->with( - $this->equalTo('url'), - $this->equalTo(3), - $this->equalTo($this->user->getUID()) - ) + ->with($this->user->getUID(), 'url', 3) ->will($this->returnValue($feeds[0])); $this->itemService->expects($this->once()) ->method('getNewestItemId') ->will($this->returnValue(3)); - $response = $this->feedAPI->create('url', 3); + $response = $this->class->create('url', 3); $this->assertEquals( [ @@ -210,21 +225,17 @@ class FeedApiControllerTest extends TestCase $feeds = [new Feed()]; $this->feedService->expects($this->once()) - ->method('purgeDeleted') - ->with($this->equalTo($this->user->getUID()), $this->equalTo(false)); + ->method('purgeDeleted'); + $this->feedService->expects($this->once()) ->method('create') - ->with( - $this->equalTo('ho'), - $this->equalTo(3), - $this->equalTo($this->user->getUID()) - ) + ->with($this->user->getUID(), 'ho', 3) ->will($this->returnValue($feeds[0])); $this->itemService->expects($this->once()) ->method('getNewestItemId') ->will($this->throwException(new ServiceNotFoundException(''))); - $response = $this->feedAPI->create('ho', 3); + $response = $this->class->create('ho', 3); $this->assertEquals( [ @@ -238,15 +249,15 @@ class FeedApiControllerTest extends TestCase public function testCreateExists() { $this->feedService->expects($this->once()) - ->method('purgeDeleted') - ->with($this->equalTo($this->user->getUID()), $this->equalTo(false)); + ->method('purgeDeleted'); + $this->feedService->expects($this->once()) ->method('create') ->will( $this->throwException(new ServiceConflictException($this->msg)) ); - $response = $this->feedAPI->create('ho', 3); + $response = $this->class->create('ho', 3); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -262,7 +273,7 @@ class FeedApiControllerTest extends TestCase $this->throwException(new ServiceNotFoundException($this->msg)) ); - $response = $this->feedAPI->create('ho', 3); + $response = $this->class->create('ho', 3); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -280,13 +291,13 @@ class FeedApiControllerTest extends TestCase $this->equalTo($this->user->getUID()) ); - $this->feedAPI->read(3, 30); + $this->class->read(3, 30); } public function testMove() { - $this->feedService->expects($this->once()) + $this->oldFeedService->expects($this->once()) ->method('patch') ->with( $this->equalTo(3), @@ -294,19 +305,19 @@ class FeedApiControllerTest extends TestCase $this->equalTo(['folderId' => 30]) ); - $this->feedAPI->move(3, 30); + $this->class->move(3, 30); } public function testMoveDoesNotExist() { - $this->feedService->expects($this->once()) + $this->oldFeedService->expects($this->once()) ->method('patch') ->will( $this->throwException(new ServiceNotFoundException($this->msg)) ); - $response = $this->feedAPI->move(3, 4); + $response = $this->class->move(3, 4); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -319,7 +330,7 @@ class FeedApiControllerTest extends TestCase $feedId = 3; $feedTitle = 'test'; - $this->feedService->expects($this->once()) + $this->oldFeedService->expects($this->once()) ->method('patch') ->with( $this->equalTo($feedId), @@ -327,7 +338,7 @@ class FeedApiControllerTest extends TestCase $this->equalTo(['title' => $feedTitle]) ); - $this->feedAPI->rename($feedId, $feedTitle); + $this->class->rename($feedId, $feedTitle); } @@ -336,7 +347,7 @@ class FeedApiControllerTest extends TestCase $feedId = 3; $feedTitle = 'test'; - $this->feedService->expects($this->once()) + $this->oldFeedService->expects($this->once()) ->method('patch') ->with( $this->equalTo($feedId), @@ -345,7 +356,7 @@ class FeedApiControllerTest extends TestCase ) ->will($this->throwException(new ServiceNotFoundException('hi'))); - $result = $this->feedAPI->rename($feedId, $feedTitle); + $result = $this->class->rename($feedId, $feedTitle); $data = $result->getData(); $code = $result->getStatus(); @@ -362,9 +373,9 @@ class FeedApiControllerTest extends TestCase $feed->setUserId('john'); $feeds = [$feed]; $this->feedService->expects($this->once()) - ->method('findAllFromAllUsers') + ->method('findAll') ->will($this->returnValue($feeds)); - $response = json_encode($this->feedAPI->fromAllUsers()); + $response = json_encode($this->class->fromAllUsers()); $this->assertEquals('{"feeds":[{"id":1,"userId":"john"}]}', $response); } @@ -373,12 +384,18 @@ class FeedApiControllerTest extends TestCase { $feedId = 3; $userId = 'hi'; + $feed = new Feed(); + + $this->feedService->expects($this->once()) + ->method('find') + ->with($userId, $feedId) + ->willReturn($feed); $this->feedService->expects($this->once()) - ->method('update') - ->with($userId, $feedId); + ->method('fetch') + ->with($feed); - $this->feedAPI->update($userId, $feedId); + $this->class->update($userId, $feedId); } @@ -387,16 +404,14 @@ class FeedApiControllerTest extends TestCase $feedId = 3; $userId = 'hi'; $this->feedService->expects($this->once()) - ->method('update') + ->method('find') ->will($this->throwException(new \Exception($this->msg))); + $this->logger->expects($this->once()) ->method('debug') ->with('Could not update feed ' . $this->msg); - $this->feedAPI->update($userId, $feedId); - - + $this->class->update($userId, $feedId); } - } diff --git a/tests/Unit/Controller/FolderApiControllerTest.php b/tests/Unit/Controller/FolderApiControllerTest.php index 3c5650e5c..fdf494037 100644 --- a/tests/Unit/Controller/FolderApiControllerTest.php +++ b/tests/Unit/Controller/FolderApiControllerTest.php @@ -17,6 +17,7 @@ namespace OCA\News\Tests\Unit\Controller; use OCA\News\Controller\FolderApiController; use OCA\News\Service\FolderService; +use OCA\News\Service\FolderServiceV2; use OCA\News\Service\ItemService; use \OCP\AppFramework\Http; @@ -59,7 +60,7 @@ class FolderApiControllerTest extends TestCase $this->user->expects($this->any()) ->method('getUID') ->will($this->returnValue('123')); - $this->folderService = $this->getMockBuilder(FolderService::class) + $this->folderService = $this->getMockBuilder(FolderServiceV2::class) ->disableOriginalConstructor() ->getMock(); $this->itemService = $this->getMockBuilder(ItemService::class) @@ -102,11 +103,11 @@ class FolderApiControllerTest extends TestCase $folder->setName($folderName); $this->folderService->expects($this->once()) - ->method('purgeDeleted') - ->with($this->equalTo($this->user->getUID()), $this->equalTo(false)); + ->method('purgeDeleted'); + $this->folderService->expects($this->once()) ->method('create') - ->with($this->equalTo($folderName), $this->equalTo($this->user->getUID())) + ->with($this->user->getUID(), $folderName) ->will($this->returnValue($folder)); $response = $this->folderAPI->create($folderName); @@ -124,8 +125,8 @@ class FolderApiControllerTest extends TestCase $msg = 'exists'; $this->folderService->expects($this->once()) - ->method('purgeDeleted') - ->with($this->equalTo($this->user->getUID()), $this->equalTo(false)); + ->method('purgeDeleted'); + $this->folderService->expects($this->once()) ->method('create') ->will($this->throwException(new ServiceConflictException($msg))); @@ -143,8 +144,8 @@ class FolderApiControllerTest extends TestCase $msg = 'exists'; $this->folderService->expects($this->once()) - ->method('purgeDeleted') - ->with($this->equalTo($this->user->getUID()), $this->equalTo(false)); + ->method('purgeDeleted'); + $this->folderService->expects($this->once()) ->method('create') ->will($this->throwException(new ServiceValidationException($msg))); @@ -161,10 +162,9 @@ class FolderApiControllerTest extends TestCase public function testDelete() { - $folderId = 23; $this->folderService->expects($this->once()) ->method('delete') - ->with($this->equalTo($folderId), $this->equalTo($this->user->getUID())); + ->with($this->user->getUID(), 23); $this->folderAPI->delete(23); } @@ -197,11 +197,7 @@ class FolderApiControllerTest extends TestCase $this->folderService->expects($this->once()) ->method('rename') - ->with( - $this->equalTo($folderId), - $this->equalTo($folderName), - $this->equalTo($this->user->getUID()) - ); + ->with($this->user->getUID(), $folderId, $folderName); $this->folderAPI->update($folderId, $folderName); } diff --git a/tests/Unit/Controller/ItemApiControllerTest.php b/tests/Unit/Controller/ItemApiControllerTest.php index 75fd6bbef..06b60f78c 100644 --- a/tests/Unit/Controller/ItemApiControllerTest.php +++ b/tests/Unit/Controller/ItemApiControllerTest.php @@ -17,6 +17,7 @@ namespace OCA\News\Tests\Unit\Controller; use OCA\News\Controller\ItemApiController; use OCA\News\Service\ItemService; +use OCA\News\Service\ItemServiceV2; use \OCP\AppFramework\Http; use \OCA\News\Service\Exceptions\ServiceNotFoundException; @@ -32,8 +33,8 @@ class ItemApiControllerTest extends TestCase { private $itemService; - private $itemAPI; - private $api; + private $oldItemService; + private $class; private $userSession; private $user; private $request; @@ -58,13 +59,17 @@ class ItemApiControllerTest extends TestCase $this->user->expects($this->any()) ->method('getUID') ->will($this->returnValue('123')); - $this->itemService = $this->getMockBuilder(ItemService::class) + $this->oldItemService = $this->getMockBuilder(ItemService::class) ->disableOriginalConstructor() ->getMock(); - $this->itemAPI = new ItemApiController( + $this->itemService = $this->getMockBuilder(ItemServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + $this->class = new ItemApiController( $this->appName, $this->request, $this->userSession, + $this->oldItemService, $this->itemService ); $this->msg = 'hi'; @@ -79,7 +84,7 @@ class ItemApiControllerTest extends TestCase $item->setGuidHash('guidhash'); $item->setFeedId(123); - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('findAllItems') ->with( $this->equalTo(2), @@ -92,7 +97,7 @@ class ItemApiControllerTest extends TestCase ) ->will($this->returnValue([$item])); - $response = $this->itemAPI->index(1, 2, true, 30, 20, true); + $response = $this->class->index(1, 2, true, 30, 20, true); $this->assertEquals( [ @@ -110,7 +115,7 @@ class ItemApiControllerTest extends TestCase $item->setGuidHash('guidhash'); $item->setFeedId(123); - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('findAllItems') ->with( $this->equalTo(2), @@ -123,7 +128,7 @@ class ItemApiControllerTest extends TestCase ) ->will($this->returnValue([$item])); - $response = $this->itemAPI->index(1, 2, false); + $response = $this->class->index(1, 2, false); $this->assertEquals( [ @@ -141,7 +146,7 @@ class ItemApiControllerTest extends TestCase $item->setGuidHash('guidhash'); $item->setFeedId(123); - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('findAllNew') ->with( $this->equalTo(2), @@ -152,7 +157,7 @@ class ItemApiControllerTest extends TestCase ) ->will($this->returnValue([$item])); - $response = $this->itemAPI->updated(1, 2, 30); + $response = $this->class->updated(1, 2, 30); $this->assertEquals( [ @@ -164,7 +169,7 @@ class ItemApiControllerTest extends TestCase public function testRead() { - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('read') ->with( $this->equalTo(2), @@ -172,13 +177,13 @@ class ItemApiControllerTest extends TestCase $this->equalTo($this->user->getUID()) ); - $this->itemAPI->read(2); + $this->class->read(2); } public function testReadDoesNotExist() { - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('read') ->will( $this->throwException( @@ -186,7 +191,7 @@ class ItemApiControllerTest extends TestCase ) ); - $response = $this->itemAPI->read(2); + $response = $this->class->read(2); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -196,7 +201,7 @@ class ItemApiControllerTest extends TestCase public function testUnread() { - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('read') ->with( $this->equalTo(2), @@ -204,13 +209,13 @@ class ItemApiControllerTest extends TestCase $this->equalTo($this->user->getUID()) ); - $this->itemAPI->unread(2); + $this->class->unread(2); } public function testUnreadDoesNotExist() { - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('read') ->will( $this->throwException( @@ -218,7 +223,7 @@ class ItemApiControllerTest extends TestCase ) ); - $response = $this->itemAPI->unread(2); + $response = $this->class->unread(2); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -228,7 +233,7 @@ class ItemApiControllerTest extends TestCase public function testStar() { - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('star') ->with( $this->equalTo(2), @@ -237,13 +242,13 @@ class ItemApiControllerTest extends TestCase $this->equalTo($this->user->getUID()) ); - $this->itemAPI->star(2, 'hash'); + $this->class->star(2, 'hash'); } public function testStarDoesNotExist() { - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('star') ->will( $this->throwException( @@ -251,7 +256,7 @@ class ItemApiControllerTest extends TestCase ) ); - $response = $this->itemAPI->star(2, 'test'); + $response = $this->class->star(2, 'test'); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -261,7 +266,7 @@ class ItemApiControllerTest extends TestCase public function testUnstar() { - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('star') ->with( $this->equalTo(2), @@ -270,13 +275,13 @@ class ItemApiControllerTest extends TestCase $this->equalTo($this->user->getUID()) ); - $this->itemAPI->unstar(2, 'hash'); + $this->class->unstar(2, 'hash'); } public function testUnstarDoesNotExist() { - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('star') ->will( $this->throwException( @@ -284,7 +289,7 @@ class ItemApiControllerTest extends TestCase ) ); - $response = $this->itemAPI->unstar(2, 'test'); + $response = $this->class->unstar(2, 'test'); $data = $response->getData(); $this->assertEquals($this->msg, $data['message']); @@ -294,52 +299,52 @@ class ItemApiControllerTest extends TestCase public function testReadAll() { - $this->itemService->expects($this->once()) + $this->oldItemService->expects($this->once()) ->method('readAll') ->with( $this->equalTo(30), $this->equalTo($this->user->getUID()) ); - $this->itemAPI->readAll(30); + $this->class->readAll(30); } public function testReadMultiple() { - $this->itemService->expects($this->exactly(2)) + $this->oldItemService->expects($this->exactly(2)) ->method('read') ->withConsecutive( [2, true, $this->user->getUID()], [4, true, $this->user->getUID()] ); - $this->itemAPI->readMultiple([2, 4]); + $this->class->readMultiple([2, 4]); } public function testReadMultipleDoesntCareAboutException() { - $this->itemService->expects($this->exactly(2)) + $this->oldItemService->expects($this->exactly(2)) ->method('read') ->withConsecutive( [2, true, $this->user->getUID()], [4, true, $this->user->getUID()] ) ->willReturnOnConsecutiveCalls($this->throwException(new ServiceNotFoundException('')), null); - $this->itemAPI->readMultiple([2, 4]); + $this->class->readMultiple([2, 4]); } public function testUnreadMultiple() { - $this->itemService->expects($this->exactly(2)) + $this->oldItemService->expects($this->exactly(2)) ->method('read') ->withConsecutive( [2, false, $this->user->getUID()], [4, false, $this->user->getUID()] ); - $this->itemAPI->unreadMultiple([2, 4]); + $this->class->unreadMultiple([2, 4]); } @@ -356,13 +361,13 @@ class ItemApiControllerTest extends TestCase ] ]; - $this->itemService->expects($this->exactly(2)) + $this->oldItemService->expects($this->exactly(2)) ->method('star') ->withConsecutive( [2, 'a', true, $this->user->getUID()], [4, 'b', true, $this->user->getUID()] ); - $this->itemAPI->starMultiple($ids); + $this->class->starMultiple($ids); } @@ -379,7 +384,7 @@ class ItemApiControllerTest extends TestCase ] ]; - $this->itemService->expects($this->exactly(2)) + $this->oldItemService->expects($this->exactly(2)) ->method('star') ->withConsecutive( [2, 'a', true, $this->user->getUID()], @@ -387,7 +392,7 @@ class ItemApiControllerTest extends TestCase ) ->willReturnOnConsecutiveCalls($this->throwException(new ServiceNotFoundException('')), null); - $this->itemAPI->starMultiple($ids); + $this->class->starMultiple($ids); } @@ -404,14 +409,14 @@ class ItemApiControllerTest extends TestCase ] ]; - $this->itemService->expects($this->exactly(2)) + $this->oldItemService->expects($this->exactly(2)) ->method('star') ->withConsecutive( [2, 'a', false, $this->user->getUID()], [4, 'b', false, $this->user->getUID()] ); - $this->itemAPI->unstarMultiple($ids); + $this->class->unstarMultiple($ids); } -- cgit v1.2.3