diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2020-12-26 13:09:41 +0100 |
---|---|---|
committer | Sean Molenaar <SMillerDev@users.noreply.github.com> | 2020-12-30 11:21:44 +0100 |
commit | 05377d023ef4d43818a4b42039c8143cb0f907e4 (patch) | |
tree | c01fc681c42671dad774de7dc7c46524eb9ae015 /tests/Unit/Controller | |
parent | 27bd54058050a70bd1c9ec8cfcdf42d38541f1b0 (diff) |
Remove PHPunit integration tests
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'tests/Unit/Controller')
-rw-r--r-- | tests/Unit/Controller/EntityApiSerializerTest.php | 144 | ||||
-rw-r--r-- | tests/Unit/Controller/FeedApiControllerTest.php | 128 | ||||
-rw-r--r-- | tests/Unit/Controller/FeedControllerTest.php | 265 | ||||
-rw-r--r-- | tests/Unit/Controller/FolderControllerTest.php | 7 | ||||
-rw-r--r-- | tests/Unit/Controller/ItemControllerTest.php | 16 |
5 files changed, 289 insertions, 271 deletions
diff --git a/tests/Unit/Controller/EntityApiSerializerTest.php b/tests/Unit/Controller/EntityApiSerializerTest.php deleted file mode 100644 index 8709275e7..000000000 --- a/tests/Unit/Controller/EntityApiSerializerTest.php +++ /dev/null @@ -1,144 +0,0 @@ -<?php -/** - * Nextcloud - News - * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. - * - * @author Alessandro Cosentino <cosenal@gmail.com> - * @author Bernhard Posselt <dev@bernhard-posselt.com> - * @copyright 2012 Alessandro Cosentino - * @copyright 2012-2014 Bernhard Posselt - */ - -namespace OCA\News\Tests\Unit\Controller; - -use OCA\News\Controller\EntityApiSerializer; -use \OCP\AppFramework\Http\Response; -use \OCP\AppFramework\Db\Entity; - -use \OCA\News\Db\Item; -use PHPUnit\Framework\TestCase; - -class TestEntity extends Entity -{ - -} - -class EntityApiSerializerTest extends TestCase -{ - - - public function testSerializeSingle() - { - $item = new Item(); - $item->setUnread(true); - $item->setId(3); - $item->setGuid('guid'); - $item->setGuidHash('guidhash'); - $item->setFeedId(123); - - $serializer = new EntityApiSerializer('items'); - $result = $serializer->serialize($item); - - $this->assertTrue($result['items'][0]['unread']); - } - - - public function testSerializeMultiple() - { - $item = new Item(); - $item->setUnread(true); - $item->setId(3); - $item->setGuid('guid'); - $item->setGuidHash('guidhash'); - $item->setFeedId(123); - - $item2 = new Item(); - $item2->setUnread(false); - $item2->setId(5); - $item2->setGuid('guid'); - $item2->setGuidHash('guidhash'); - $item2->setFeedId(123); - - $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(true); - $item->setId(3); - $item->setGuid('guid'); - $item->setGuidHash('guidhash'); - $item->setFeedId(123); - - $item2 = new Item(); - $item2->setUnread(false); - $item2->setId(5); - $item2->setGuid('guid'); - $item2->setGuidHash('guidhash'); - $item2->setFeedId(123); - - $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/FeedApiControllerTest.php b/tests/Unit/Controller/FeedApiControllerTest.php index 9cfc47cec..b31d9fd43 100644 --- a/tests/Unit/Controller/FeedApiControllerTest.php +++ b/tests/Unit/Controller/FeedApiControllerTest.php @@ -15,18 +15,15 @@ namespace OCA\News\Tests\Unit\Controller; +use Exception; 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; use \OCA\News\Service\Exceptions\ServiceNotFoundException; use \OCA\News\Service\Exceptions\ServiceConflictException; use \OCA\News\Db\Feed; -use OCP\ILogger; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; @@ -36,10 +33,6 @@ use Psr\Log\LoggerInterface; class FeedApiControllerTest extends TestCase { - /** - * @var \PHPUnit\Framework\MockObject\MockObject|FeedService - */ - private $oldFeedService; /** * @var \PHPUnit\Framework\MockObject\MockObject|FeedServiceV2 @@ -57,7 +50,10 @@ class FeedApiControllerTest extends TestCase private $logger; private $class; - private $user; + /** + * @var string + */ + private $userID = '123'; private $msg; protected function setUp(): void @@ -72,18 +68,15 @@ class FeedApiControllerTest extends TestCase $userSession = $this->getMockBuilder(IUserSession::class) ->disableOriginalConstructor() ->getMock(); - $this->user = $this->getMockBuilder(IUser::class) + $user = $this->getMockBuilder(IUser::class) ->disableOriginalConstructor() ->getMock(); $userSession->expects($this->any()) ->method('getUser') - ->will($this->returnValue($this->user)); - $this->user->expects($this->any()) + ->will($this->returnValue($user)); + $user->expects($this->any()) ->method('getUID') - ->will($this->returnValue('123')); - $this->oldFeedService = $this->getMockBuilder(FeedService::class) - ->disableOriginalConstructor() - ->getMock(); + ->will($this->returnValue($this->userID)); $this->feedService = $this->getMockBuilder(FeedServiceV2::class) ->disableOriginalConstructor() ->getMock(); @@ -93,7 +86,6 @@ class FeedApiControllerTest extends TestCase $this->class = new FeedApiController( $request, $userSession, - $this->oldFeedService, $this->feedService, $this->itemService, $this->logger @@ -110,25 +102,26 @@ class FeedApiControllerTest extends TestCase $this->itemService->expects($this->once()) ->method('starredCount') - ->with($this->equalTo($this->user->getUID())) + ->with($this->equalTo($this->userID)) ->will($this->returnValue($starredCount)); $this->itemService->expects($this->once()) ->method('getNewestItemId') - ->with($this->equalTo($this->user->getUID())) + ->with($this->equalTo($this->userID)) ->will($this->returnValue($newestItemId)); $this->feedService->expects($this->once()) ->method('findAllForUser') - ->with($this->equalTo($this->user->getUID())) + ->with($this->equalTo($this->userID)) ->will($this->returnValue($feeds)); $response = $this->class->index(); $this->assertEquals( [ - 'feeds' => [$feeds[0]->toAPI()], - 'starredCount' => $starredCount, - 'newestItemId' => $newestItemId - ], $response + 'feeds' => [$feeds[0]->toAPI()], + 'starredCount' => $starredCount, + 'newestItemId' => $newestItemId + ], + $response ); } @@ -140,24 +133,25 @@ class FeedApiControllerTest extends TestCase $this->itemService->expects($this->once()) ->method('starredCount') - ->with($this->equalTo($this->user->getUID())) + ->with($this->equalTo($this->userID)) ->will($this->returnValue($starredCount)); $this->itemService->expects($this->once()) ->method('getNewestItemId') - ->with($this->equalTo($this->user->getUID())) + ->with($this->equalTo($this->userID)) ->will($this->throwException(new ServiceNotFoundException(''))); $this->feedService->expects($this->once()) ->method('findAllForUser') - ->with($this->equalTo($this->user->getUID())) + ->with($this->equalTo($this->userID)) ->will($this->returnValue($feeds)); $response = $this->class->index(); $this->assertEquals( [ - 'feeds' => [$feeds[0]->toAPI()], - 'starredCount' => $starredCount, - ], $response + 'feeds' => [$feeds[0]->toAPI()], + 'starredCount' => $starredCount, + ], + $response ); } @@ -167,7 +161,7 @@ class FeedApiControllerTest extends TestCase $this->feedService->expects($this->once()) ->method('delete') ->with( - $this->equalTo($this->user->getUID()), + $this->equalTo($this->userID), $this->equalTo(2) ); @@ -202,7 +196,7 @@ class FeedApiControllerTest extends TestCase $this->feedService->expects($this->once()) ->method('create') - ->with($this->user->getUID(), 'url', 3) + ->with($this->userID, 'url', 3) ->will($this->returnValue($feeds[0])); $this->itemService->expects($this->once()) ->method('getNewestItemId') @@ -229,7 +223,7 @@ class FeedApiControllerTest extends TestCase $this->feedService->expects($this->once()) ->method('create') - ->with($this->user->getUID(), 'ho', 3) + ->with($this->userID, 'ho', 3) ->will($this->returnValue($feeds[0])); $this->itemService->expects($this->once()) ->method('getNewestItemId') @@ -239,8 +233,9 @@ class FeedApiControllerTest extends TestCase $this->assertEquals( [ - 'feeds' => [$feeds[0]->toAPI()] - ], $response + 'feeds' => [$feeds[0]->toAPI()] + ], + $response ); } @@ -288,7 +283,7 @@ class FeedApiControllerTest extends TestCase ->with( $this->equalTo(3), $this->equalTo(30), - $this->equalTo($this->user->getUID()) + $this->equalTo($this->userID) ); $this->class->read(3, 30); @@ -297,13 +292,18 @@ class FeedApiControllerTest extends TestCase public function testMove() { - $this->oldFeedService->expects($this->once()) - ->method('patch') - ->with( - $this->equalTo(3), - $this->equalTo($this->user->getUID()), - $this->equalTo(['folderId' => 30]) - ); + $feed = $this->getMockBuilder(Feed::class)->getMock(); + $feed->expects($this->once()) + ->method('setFolderId') + ->with(30) + ->will($this->returnSelf()); + $this->feedService->expects($this->once()) + ->method('find') + ->with($this->userID, 3) + ->will($this->returnValue($feed)); + $this->feedService->expects($this->once()) + ->method('update') + ->with($this->userID, $feed); $this->class->move(3, 30); } @@ -311,8 +311,8 @@ class FeedApiControllerTest extends TestCase public function testMoveDoesNotExist() { - $this->oldFeedService->expects($this->once()) - ->method('patch') + $this->feedService->expects($this->once()) + ->method('update') ->will( $this->throwException(new ServiceNotFoundException($this->msg)) ); @@ -327,18 +327,20 @@ class FeedApiControllerTest extends TestCase public function testRename() { - $feedId = 3; - $feedTitle = 'test'; - - $this->oldFeedService->expects($this->once()) - ->method('patch') - ->with( - $this->equalTo($feedId), - $this->equalTo($this->user->getUID()), - $this->equalTo(['title' => $feedTitle]) - ); + $feed = $this->getMockBuilder(Feed::class)->getMock(); + $feed->expects($this->once()) + ->method('setTitle') + ->with('test') + ->will($this->returnSelf()); + $this->feedService->expects($this->once()) + ->method('find') + ->with($this->userID, 3) + ->will($this->returnValue($feed)); + $this->feedService->expects($this->once()) + ->method('update') + ->with($this->userID, $feed); - $this->class->rename($feedId, $feedTitle); + $this->class->rename(3, 'test'); } @@ -347,13 +349,9 @@ class FeedApiControllerTest extends TestCase $feedId = 3; $feedTitle = 'test'; - $this->oldFeedService->expects($this->once()) - ->method('patch') - ->with( - $this->equalTo($feedId), - $this->equalTo($this->user->getUID()), - $this->equalTo(['title' => $feedTitle]) - ) + $this->feedService->expects($this->once()) + ->method('find') + ->with($this->userID, 3) ->will($this->throwException(new ServiceNotFoundException('hi'))); $result = $this->class->rename($feedId, $feedTitle); @@ -365,7 +363,7 @@ class FeedApiControllerTest extends TestCase } - public function testfromAllUsers() + public function testFromAllUsers() { $feed = new Feed(); $feed->setUrl(3); @@ -405,7 +403,7 @@ class FeedApiControllerTest extends TestCase $userId = 'hi'; $this->feedService->expects($this->once()) ->method('find') - ->will($this->throwException(new \Exception($this->msg))); + ->will($this->throwException(new Exception($this->msg))); $this->logger->expects($this->once()) ->method('debug') diff --git a/tests/Unit/Controller/FeedControllerTest.php b/tests/Unit/Controller/FeedControllerTest.php index aba8cdc1f..487ef0af1 100644 --- a/tests/Unit/Controller/FeedControllerTest.php +++ b/tests/Unit/Controller/FeedControllerTest.php @@ -14,8 +14,10 @@ namespace OCA\News\Tests\Unit\Controller; use OCA\News\Controller\FeedController; -use OCA\News\Service\FeedService; +use OCA\News\Db\Folder; +use OCA\News\Service\FeedServiceV2; use OCA\News\Service\FolderServiceV2; +use OCA\News\Service\ImportService; use OCA\News\Service\ItemService; use OCP\AppFramework\Http; @@ -45,11 +47,14 @@ class FeedControllerTest extends TestCase */ private $folderService; /** - * TODO: Remove - * @var MockObject|FeedService + * @var MockObject|FeedServiceV2 */ private $feedService; /** + * @var MockObject|ImportService + */ + private $importService; + /** * TODO: Remove * @var MockObject|ItemService */ @@ -86,7 +91,11 @@ class FeedControllerTest extends TestCase ->disableOriginalConstructor() ->getMock(); $this->feedService = $this - ->getMockBuilder(FeedService::class) + ->getMockBuilder(FeedServiceV2::class) + ->disableOriginalConstructor() + ->getMock(); + $this->importService = $this + ->getMockBuilder(ImportService::class) ->disableOriginalConstructor() ->getMock(); $this->folderService = $this @@ -110,6 +119,7 @@ class FeedControllerTest extends TestCase $this->folderService, $this->feedService, $this->itemService, + $this->importService, $this->settings, $this->userSession ); @@ -136,11 +146,11 @@ class FeedControllerTest extends TestCase ->will($this->returnValue($result['feeds'])); $this->itemService->expects($this->once()) ->method('getNewestItemId') - ->with($this->equalTo($this->uid)) + ->with($this->uid) ->will($this->throwException(new ServiceNotFoundException(''))); $this->itemService->expects($this->once()) ->method('starredCount') - ->with($this->equalTo($this->uid)) + ->with($this->uid) ->will($this->returnValue($result['starred'])); $response = $this->class->index(); @@ -160,15 +170,15 @@ class FeedControllerTest extends TestCase ]; $this->feedService->expects($this->once()) ->method('findAllForUser') - ->with($this->equalTo($this->uid)) + ->with($this->uid) ->will($this->returnValue($result['feeds'])); $this->itemService->expects($this->once()) ->method('getNewestItemId') - ->with($this->equalTo($this->uid)) + ->with($this->uid) ->will($this->returnValue($result['newestItemId'])); $this->itemService->expects($this->once()) ->method('starredCount') - ->with($this->equalTo($this->uid)) + ->with($this->uid) ->will($this->returnValue($result['starred'])); $response = $this->class->index(); @@ -229,6 +239,32 @@ class FeedControllerTest extends TestCase } + public function testActiveFolder() + { + $type = FeedType::FOLDER; + $folder = new Folder(); + $folder->setId(3); + + $result = [ + 'activeFeed' => [ + 'id' => 3, + 'type' => 1 + ] + ]; + + $this->folderService->expects($this->once()) + ->method('find') + ->with($this->uid, 3) + ->will($this->returnValue($folder)); + + $this->activeInitMocks(3, $type); + + $response = $this->class->active(); + + $this->assertEquals($result, $response); + } + + public function testActiveFolderDoesNotExist() { $id = 3; @@ -276,15 +312,10 @@ class FeedControllerTest extends TestCase ->will($this->returnValue($result['newestItemId'])); $this->feedService->expects($this->once()) ->method('purgeDeleted') - ->with($this->equalTo($this->uid), $this->equalTo(false)); + ->with($this->uid, false); $this->feedService->expects($this->once()) ->method('create') - ->with( - $this->equalTo('hi'), - $this->equalTo(4), - $this->equalTo($this->uid), - $this->equalTo('yo') - ) + ->with($this->uid, 'hi', 4, false, 'yo') ->will($this->returnValue($result['feeds'][0])); $response = $this->class->create('hi', 4, 'yo'); @@ -293,13 +324,37 @@ class FeedControllerTest extends TestCase } + public function testCreateOldFolderId() + { + $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->uid, false); + $this->feedService->expects($this->once()) + ->method('create') + ->with($this->uid, 'hi', null, false, 'yo') + ->will($this->returnValue($result['feeds'][0])); + + $response = $this->class->create('hi', 0, 'yo'); + + $this->assertEquals($result, $response); + } + + public function testCreateNoItems() { $result = ['feeds' => [new Feed()]]; $this->feedService->expects($this->once()) ->method('purgeDeleted') - ->with($this->equalTo($this->uid), $this->equalTo(false)); + ->with($this->uid, false); $this->itemService->expects($this->once()) ->method('getNewestItemId') @@ -307,12 +362,7 @@ class FeedControllerTest extends TestCase $this->feedService->expects($this->once()) ->method('create') - ->with( - $this->equalTo('hi'), - $this->equalTo(4), - $this->equalTo($this->uid), - $this->equalTo('yo') - ) + ->with($this->uid, 'hi', 4, false, 'yo') ->will($this->returnValue($result['feeds'][0])); $response = $this->class->create('hi', 4, 'yo'); @@ -327,7 +377,7 @@ class FeedControllerTest extends TestCase $ex = new ServiceNotFoundException($msg); $this->feedService->expects($this->once()) ->method('purgeDeleted') - ->with($this->equalTo($this->uid), $this->equalTo(false)); + ->with($this->uid, false); $this->feedService->expects($this->once()) ->method('create') ->will($this->throwException($ex)); @@ -348,7 +398,7 @@ class FeedControllerTest extends TestCase $ex = new ServiceConflictException($msg); $this->feedService->expects($this->once()) ->method('purgeDeleted') - ->with($this->equalTo($this->uid), $this->equalTo(false)); + ->with($this->uid, false); $this->feedService->expects($this->once()) ->method('create') ->will($this->throwException($ex)); @@ -363,9 +413,17 @@ class FeedControllerTest extends TestCase public function testDelete() { + $feed = $this->getMockBuilder(Feed::class) + ->getMock(); + $feed->expects($this->once()) + ->method('setDeletedAt'); + $this->feedService->expects($this->once()) + ->method('find') + ->with('jack', 4) + ->willReturn($feed); $this->feedService->expects($this->once()) - ->method('markDeleted') - ->with($this->equalTo(4)); + ->method('update') + ->with('jack', $feed); $this->class->delete(4); } @@ -376,7 +434,7 @@ class FeedControllerTest extends TestCase $msg = 'hehe'; $this->feedService->expects($this->once()) - ->method('markDeleted') + ->method('find') ->will($this->throwException(new ServiceNotFoundException($msg))); $response = $this->class->delete(4); @@ -402,8 +460,13 @@ class FeedControllerTest extends TestCase ]; $this->feedService->expects($this->once()) - ->method('update') - ->with($this->equalTo($this->uid), $this->equalTo(4)) + ->method('find') + ->with($this->uid, 4) + ->will($this->returnValue($feed)); + + $this->feedService->expects($this->once()) + ->method('fetch') + ->with($feed) ->will($this->returnValue($feed)); $response = $this->class->update(4); @@ -415,8 +478,8 @@ class FeedControllerTest extends TestCase public function testUpdateReturnsJSONError() { $this->feedService->expects($this->once()) - ->method('update') - ->with($this->equalTo($this->uid), $this->equalTo(4)) + ->method('find') + ->with($this->uid, 4) ->will($this->throwException(new ServiceNotFoundException('NO!'))); $response = $this->class->update(4); @@ -436,17 +499,14 @@ class FeedControllerTest extends TestCase 'feeds' => [$feed] ]; - $this->feedService->expects($this->once()) + $this->importService->expects($this->once()) ->method('importArticles') - ->with( - $this->equalTo(['json']), - $this->equalTo($this->uid) - ) + ->with($this->uid, ['json'],) ->will($this->returnValue($feed)); $this->itemService->expects($this->once()) ->method('starredCount') - ->with($this->equalTo($this->uid)) + ->with($this->uid) ->will($this->returnValue(3)); $response = $this->class->import(['json']); @@ -457,17 +517,14 @@ class FeedControllerTest extends TestCase public function testImportCreatesNoAdditionalFeed() { - $this->feedService->expects($this->once()) + $this->importService->expects($this->once()) ->method('importArticles') - ->with( - $this->equalTo(['json']), - $this->equalTo($this->uid) - ) + ->with($this->uid, ['json']) ->will($this->returnValue(null)); $this->itemService->expects($this->once()) ->method('starredCount') - ->with($this->equalTo($this->uid)) + ->with($this->uid) ->will($this->returnValue(3)); $response = $this->class->import(['json']); @@ -489,7 +546,7 @@ class FeedControllerTest extends TestCase $this->itemService->expects($this->once()) ->method('readFeed') - ->with($this->equalTo(4), $this->equalTo(5), $this->uid); + ->with(4, 5, $this->uid); $response = $this->class->read(4, 5); $this->assertEquals($expected, $response); @@ -498,9 +555,21 @@ class FeedControllerTest extends TestCase public function testRestore() { + $feed = $this->getMockBuilder(Feed::class) + ->getMock(); + + $feed->expects($this->once()) + ->method('setDeletedAt') + ->with(null); + $this->feedService->expects($this->once()) - ->method('unmarkDeleted') - ->with($this->equalTo(4)); + ->method('find') + ->with($this->uid, 4) + ->willReturn($feed); + + $this->feedService->expects($this->once()) + ->method('update') + ->with($this->uid, $feed); $this->class->restore(4); } @@ -511,7 +580,8 @@ class FeedControllerTest extends TestCase $msg = 'hehe'; $this->feedService->expects($this->once()) - ->method('unmarkDeleted') + ->method('find') + ->with($this->uid, 4) ->will($this->throwException(new ServiceNotFoundException($msg))); $response = $this->class->restore(4); @@ -523,17 +593,46 @@ class FeedControllerTest extends TestCase public function testPatch() { - $expected = [ - 'pinned' => true, - 'fullTextEnabled' => true, - 'updateMode' => 1 - ]; + $feed = $this->getMockBuilder(Feed::class) + ->getMock(); + + $feed->expects($this->once()) + ->method('setFolderId') + ->with(null); + + $feed->expects($this->once()) + ->method('setPinned') + ->with(true); + + $feed->expects($this->once()) + ->method('setFullTextEnabled') + ->with(false); + + + $feed->expects($this->once()) + ->method('setUpdateMode') + ->with(1); + + + $feed->expects($this->never()) + ->method('setOrdering') + ->with(true); + + + $feed->expects($this->never()) + ->method('setTitle') + ->with(true); + $this->feedService->expects($this->once()) - ->meth |