diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2020-11-16 20:49:38 +0100 |
---|---|---|
committer | Sean Molenaar <SMillerDev@users.noreply.github.com> | 2020-12-08 13:58:12 +0100 |
commit | 4ee3fcb78113caff9f2b890cb7d1702dc936d81e (patch) | |
tree | f8fc506f997acdb8e5315c62bb068b0b6db6cd56 /tests/Unit/Controller | |
parent | 8abddeab4f541883721d912f97dec07bffdfc6b8 (diff) |
Refactor User ID fetching and fix non-specific cleanup
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'tests/Unit/Controller')
-rw-r--r-- | tests/Unit/Controller/ExportControllerTest.php | 24 | ||||
-rw-r--r-- | tests/Unit/Controller/FeedApiControllerTest.php | 7 | ||||
-rw-r--r-- | tests/Unit/Controller/FeedControllerTest.php | 18 | ||||
-rw-r--r-- | tests/Unit/Controller/FolderControllerTest.php | 16 | ||||
-rw-r--r-- | tests/Unit/Controller/ItemControllerTest.php | 134 | ||||
-rw-r--r-- | tests/Unit/Controller/PageControllerTest.php | 13 |
6 files changed, 114 insertions, 98 deletions
diff --git a/tests/Unit/Controller/ExportControllerTest.php b/tests/Unit/Controller/ExportControllerTest.php index 563bd987c..276a36abf 100644 --- a/tests/Unit/Controller/ExportControllerTest.php +++ b/tests/Unit/Controller/ExportControllerTest.php @@ -25,6 +25,8 @@ use \OCA\News\Db\Feed; use OCP\AppFramework\Http\DataDownloadResponse; use OCP\IRequest; +use OCP\IUser; +use OCP\IUserSession; use PHPUnit\Framework\TestCase; class ExportControllerTest extends TestCase @@ -33,6 +35,10 @@ class ExportControllerTest extends TestCase private $controller; private $user; /** + * @var \PHPUnit\Framework\MockObject\MockObject|IUserSession + */ + private $userSession; + /** * @var \PHPUnit\Framework\MockObject\MockObject|FeedServiceV2 */ private $feedService; @@ -55,7 +61,6 @@ class ExportControllerTest extends TestCase public function setUp(): void { $appName = 'news'; - $this->user = 'john'; $this->itemService = $this->getMockBuilder(ItemServiceV2::class) ->disableOriginalConstructor() ->getMock(); @@ -68,6 +73,15 @@ class ExportControllerTest extends TestCase $this->opmlService = $this->getMockBuilder(OpmlService::class) ->disableOriginalConstructor() ->getMock(); + $this->user = $this->getMockBuilder(IUser::class)->getMock(); + $this->user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('user')); + $this->userSession = $this->getMockBuilder(IUserSession::class) + ->getMock(); + $this->userSession->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); $request = $this->getMockBuilder(IRequest::class) ->disableOriginalConstructor() ->getMock(); @@ -78,7 +92,7 @@ class ExportControllerTest extends TestCase $this->feedService, $this->itemService, $this->opmlService, - $this->user + $this->userSession ); } @@ -96,7 +110,7 @@ class ExportControllerTest extends TestCase $this->opmlService->expects($this->once()) ->method('export') - ->with($this->user) + ->with('user') ->will($this->returnValue($opml)); $return = $this->controller->opml(); @@ -126,11 +140,11 @@ class ExportControllerTest extends TestCase $this->feedService->expects($this->once()) ->method('findAllForUser') - ->with($this->user) + ->with('user') ->will($this->returnValue($feeds)); $this->itemService->expects($this->once()) ->method('findAllForUser') - ->with($this->user, ['unread' => true, 'starred' => true]) + ->with('user', ['unread' => true, 'starred' => true]) ->will($this->returnValue($articles)); diff --git a/tests/Unit/Controller/FeedApiControllerTest.php b/tests/Unit/Controller/FeedApiControllerTest.php index ace481a66..0ed65f5c5 100644 --- a/tests/Unit/Controller/FeedApiControllerTest.php +++ b/tests/Unit/Controller/FeedApiControllerTest.php @@ -213,9 +213,10 @@ class FeedApiControllerTest extends TestCase $this->assertEquals( [ - 'feeds' => [$feeds[0]->toAPI()], - 'newestItemId' => 3 - ], $response + 'feeds' => [$feeds[0]->toAPI()], + 'newestItemId' => 3 + ], + $response ); } diff --git a/tests/Unit/Controller/FeedControllerTest.php b/tests/Unit/Controller/FeedControllerTest.php index fb5d0b5b5..6285be9dc 100644 --- a/tests/Unit/Controller/FeedControllerTest.php +++ b/tests/Unit/Controller/FeedControllerTest.php @@ -27,6 +27,7 @@ use OCP\IConfig; use OCP\IRequest; use OCP\IUser; +use OCP\IUserSession; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -77,12 +78,6 @@ class FeedControllerTest extends TestCase { $this->appName = 'news'; $this->uid = 'jack'; - $this->user = $this->getMockBuilder(IUser::class) - ->getMock(); - - $this->user->expects($this->once()) - ->method('getUID') - ->willReturn($this->uid); $this->settings = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock(); @@ -98,6 +93,15 @@ class FeedControllerTest extends TestCase ->getMockBuilder(FolderServiceV2::class) ->disableOriginalConstructor() ->getMock(); + $this->user = $this->getMockBuilder(IUser::class)->getMock(); + $this->user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue($this->uid)); + $this->userSession = $this->getMockBuilder(IUserSession::class) + ->getMock(); + $this->userSession->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); $request = $this->getMockBuilder(IRequest::class) ->disableOriginalConstructor() ->getMock(); @@ -108,7 +112,7 @@ class FeedControllerTest extends TestCase $this->feedService, $this->itemService, $this->settings, - $this->user + $this->userSession ); $this->exampleResult = [ 'activeFeed' => [ diff --git a/tests/Unit/Controller/FolderControllerTest.php b/tests/Unit/Controller/FolderControllerTest.php index d0216c70f..ef1b1e965 100644 --- a/tests/Unit/Controller/FolderControllerTest.php +++ b/tests/Unit/Controller/FolderControllerTest.php @@ -27,6 +27,7 @@ use \OCA\News\Service\Exceptions\ServiceValidationException; use OCP\IRequest; use OCP\IUser; +use OCP\IUserSession; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -43,6 +44,7 @@ class FolderControllerTest extends TestCase * @var MockObject|IUser */ private $user; + private $userSession; private $class; private $msg; @@ -55,9 +57,6 @@ class FolderControllerTest extends TestCase $appName = 'news'; $this->user = $this->getMockBuilder(IUser::class) ->getMock(); - $this->user->expects($this->once()) - ->method('getUID') - ->willReturn('jack'); $this->folderService = $this->getMockBuilder(FolderServiceV2::class) ->disableOriginalConstructor() ->getMock(); @@ -70,13 +69,22 @@ class FolderControllerTest extends TestCase $request = $this->getMockBuilder(IRequest::class) ->disableOriginalConstructor() ->getMock(); + $this->user = $this->getMockBuilder(IUser::class)->getMock(); + $this->user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('jack')); + $this->userSession = $this->getMockBuilder(IUserSession::class) + ->getMock(); + $this->userSession->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); $this->class = new FolderController( $appName, $request, $this->folderService, $this->feedService, $this->itemService, - $this->user + $this->userSession ); $this->msg = 'ron'; } diff --git a/tests/Unit/Controller/ItemControllerTest.php b/tests/Unit/Controller/ItemControllerTest.php index c015c33a1..9929cde23 100644 --- a/tests/Unit/Controller/ItemControllerTest.php +++ b/tests/Unit/Controller/ItemControllerTest.php @@ -25,6 +25,8 @@ use \OCA\News\Service\Exceptions\ServiceNotFoundException; use OCP\IConfig; use OCP\IRequest; +use OCP\IUser; +use OCP\IUserSession; use PHPUnit\Framework\TestCase; @@ -36,7 +38,14 @@ class ItemControllerTest extends TestCase private $itemService; private $feedService; private $request; + /** + * @var \PHPUnit\Framework\MockObject\MockObject|IUser + */ private $user; + /** + * @var \PHPUnit\Framework\MockObject\MockObject|IUserSession + */ + private $userSession; private $controller; private $newestItemId; @@ -47,7 +56,6 @@ class ItemControllerTest extends TestCase public function setUp(): void { $this->appName = 'news'; - $this->user = 'jackob'; $this->settings = $this->getMockBuilder(IConfig::class) ->disableOriginalConstructor() ->getMock(); @@ -62,10 +70,22 @@ class ItemControllerTest extends TestCase $this->request = $this->getMockBuilder(IRequest::class) ->disableOriginalConstructor() ->getMock(); + $this->user = $this->getMockBuilder(IUser::class)->getMock(); + $this->user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('user')); + $this->userSession = $this->getMockBuilder(IUserSession::class) + ->getMock(); + $this->userSession->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); $this->controller = new ItemController( - $this->appName, $this->request, - $this->feedService, $this->itemService, $this->settings, - $this->user + $this->appName, + $this->request, + $this->feedService, + $this->itemService, + $this->settings, + $this->userSession ); $this->newestItemId = 12312; } @@ -75,7 +95,7 @@ class ItemControllerTest extends TestCase { $this->itemService->expects($this->once()) ->method('read') - ->with(4, true, $this->user); + ->with(4, true, 'user'); $this->controller->read(4, true); } @@ -92,7 +112,7 @@ class ItemControllerTest extends TestCase $response = $this->controller->read(4); $params = json_decode($response->render(), true); - $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); $this->assertEquals($msg, $params['message']); } @@ -102,8 +122,8 @@ class ItemControllerTest extends TestCase $this->itemService->expects($this->exactly(2)) ->method('read') ->withConsecutive( - [2, true, $this->user], - [4, true, $this->user] + [2, true, 'user'], + [4, true, 'user'] ); $this->controller->readMultiple([2, 4]); @@ -116,8 +136,8 @@ class ItemControllerTest extends TestCase $this->itemService->expects($this->exactly(2)) ->method('read') ->withConsecutive( - [2, true, $this->user], - [4, true, $this->user] + [2, true, 'user'], + [4, true, 'user'] ) ->willReturnOnConsecutiveCalls($this->throwException(new ServiceNotFoundException('yo')), null); $this->controller->readMultiple([2, 4]); @@ -128,12 +148,7 @@ class ItemControllerTest extends TestCase { $this->itemService->expects($this->once()) ->method('star') - ->with( - $this->equalTo(4), - $this->equalTo('test'), - $this->equalTo(true), - $this->equalTo($this->user) - ); + ->with(4, 'test', true, 'user'); $this->controller->star(4, 'test', true); } @@ -150,7 +165,7 @@ class ItemControllerTest extends TestCase $response = $this->controller->star(4, 'test', false); $params = json_decode($response->render(), true); - $this->assertEquals($response->getStatus(), Http::STATUS_NOT_FOUND); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); $this->assertEquals($msg, $params['message']); } @@ -163,13 +178,10 @@ class ItemControllerTest extends TestCase $this->itemService->expects($this->once()) ->method('readAll') - ->with( - $this->equalTo(5), - $this->equalTo($this->user) - ); + ->with(5, 'user'); $this->feedService->expects($this->once()) ->method('findAllForUser') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue([$feed])); $response = $this->controller->readAll(5); @@ -182,15 +194,15 @@ class ItemControllerTest extends TestCase $this->settings->expects($this->exactly(2)) ->method('getUserValue') ->withConsecutive( - [$this->user, $this->appName, 'showAll'], - [$this->user, $this->appName, 'oldestFirst'] + ['user', $this->appName, 'showAll'], + ['user', $this->appName, 'oldestFirst'] ) ->willReturnOnConsecutiveCalls('1', $oldestFirst); $this->settings->expects($this->exactly(2)) ->method('setUserValue') ->withConsecutive( - [$this->user, $this->appName, 'lastViewedFeedId', $id], - [$this->user, $this->appName, 'lastViewedFeedType', $type] + ['user', $this->appName, 'lastViewedFeedId', $id], + ['user', $this->appName, 'lastViewedFeedType', $type] ); } @@ -209,22 +221,22 @@ class ItemControllerTest extends TestCase $this->feedService->expects($this->once()) ->method('findAllForUser') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue($feeds)); $this->itemService->expects($this->once()) ->method('getNewestItemId') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue($this->newestItemId)); $this->itemService->expects($this->once()) ->method('starredCount') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue(3111)); $this->itemService->expects($this->once()) ->method('findAllItems') - ->with(2, FeedType::FEED, 3, 0, true, false, $this->user, []) + ->with(2, FeedType::FEED, 3, 0, true, false, 'user', []) ->will($this->returnValue($result['items'])); $response = $this->controller->index(FeedType::FEED, 2, 3); @@ -246,37 +258,25 @@ class ItemControllerTest extends TestCase $this->feedService->expects($this->once()) ->method('findAllForUser') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue($feeds)); $this->itemService->expects($this->once()) ->method('getNewestItemId') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue($this->newestItemId)); $this->itemService->expects($this->once()) ->method('starredCount') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue(3111)); $this->itemService->expects($this->once()) ->method('findAllItems') - ->with( - $this->equalTo(2), - $this->equalTo(FeedType::FEED), - $this->equalTo(3), - $this->equalTo(0), - $this->equalTo(true), - $this->equalTo(false), - $this->equalTo($this->user), - $this->equalTo(['test', 'search']) - ) + ->with(2, FeedType::FEED, 3, 0, true, false, 'user', ['test', 'search']) ->will($this->returnValue($result['items'])); - $response = $this->controller->index( - FeedType::FEED, 2, 3, - 0, null, null, 'test%20%20search%20' - ); + $response = $this->controller->index(FeedType::FEED, 2, 3, 0, null, null, 'test%20%20search%20'); $this->assertEquals($result, $response); } @@ -289,15 +289,7 @@ class ItemControllerTest extends TestCase $this->itemService->expects($this->once()) ->method('findAllItems') - ->with( - $this->equalTo(2), - $this->equalTo(FeedType::FEED), - $this->equalTo(3), - $this->equalTo(10), - $this->equalTo(true), - $this->equalTo(true), - $this->equalTo($this->user) - ) + ->with(2, FeedType::FEED, 3, 10, true, true, 'user') ->will($this->returnValue($result['items'])); $this->feedService->expects($this->never()) @@ -314,7 +306,7 @@ class ItemControllerTest extends TestCase $this->itemService->expects($this->once()) ->method('getNewestItemId') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->throwException(new ServiceNotFoundException(''))); $response = $this->controller->index(FeedType::FEED, 2, 3); @@ -334,37 +326,27 @@ class ItemControllerTest extends TestCase $this->settings->expects($this->once()) ->method('getUserValue') - ->with( - $this->equalTo($this->user), - $this->equalTo($this->appName), - $this->equalTo('showAll') - ) + ->with('user', $this->appName, 'showAll') ->will($this->returnValue('1')); $this->feedService->expects($this->once()) ->method('findAllForUser') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue($feeds)); $this->itemService->expects($this->once()) ->method('getNewestItemId') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue($this->newestItemId)); $this->itemService->expects($this->once()) ->method('starredCount') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->returnValue(3111)); $this->itemService->expects($this->once()) ->method('findAllNew') - ->with( - $this->equalTo(2), - $this->equalTo(FeedType::FEED), - $this->equalTo(3), - $this->equalTo(true), - $this->equalTo($this->user) - ) + ->with(2, FeedType::FEED, 3, true, 'user') ->will($this->returnValue($result['items'])); $response = $this->controller->newItems(FeedType::FEED, 2, 3); @@ -376,16 +358,12 @@ class ItemControllerTest extends TestCase { $this->settings->expects($this->once()) ->method('getUserValue') - ->with( - $this->equalTo($this->user), - $this->equalTo($this->appName), - $this->equalTo('showAll') - ) + ->with('user', $this->appName, 'showAll') ->will($this->returnValue('1')); $this->itemService->expects($this->once()) ->method('getNewestItemId') - ->with($this->equalTo($this->user)) + ->with('user') ->will($this->throwException(new ServiceNotFoundException(''))); $response = $this->controller->newItems(FeedType::FEED, 2, 3); diff --git a/tests/Unit/Controller/PageControllerTest.php b/tests/Unit/Controller/PageControllerTest.php index 2afe2d590..0c642fe29 100644 --- a/tests/Unit/Controller/PageControllerTest.php +++ b/tests/Unit/Controller/PageControllerTest.php @@ -22,6 +22,8 @@ use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; use OCP\IURLGenerator; +use OCP\IUser; +use OCP\IUserSession; use PHPUnit\Framework\TestCase; @@ -101,6 +103,15 @@ class PageControllerTest extends TestCase $this->status = $this->getMockBuilder(StatusService::class) ->disableOriginalConstructor() ->getMock(); + $this->user = $this->getMockBuilder(IUser::class)->getMock(); + $this->user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('becka')); + $this->userSession = $this->getMockBuilder(IUserSession::class) + ->getMock(); + $this->userSession->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); $this->controller = new PageController( 'news', $this->request, @@ -109,7 +120,7 @@ class PageControllerTest extends TestCase $this->l10n, $this->recommended, $this->status, - 'becka' + $this->userSession ); } |