diff options
Diffstat (limited to 'tests/Unit/Service/FolderServiceTest.php')
-rw-r--r-- | tests/Unit/Service/FolderServiceTest.php | 320 |
1 files changed, 148 insertions, 172 deletions
diff --git a/tests/Unit/Service/FolderServiceTest.php b/tests/Unit/Service/FolderServiceTest.php index b1469dc00..69934dd5b 100644 --- a/tests/Unit/Service/FolderServiceTest.php +++ b/tests/Unit/Service/FolderServiceTest.php @@ -13,16 +13,25 @@ namespace OCA\News\Tests\Unit\Service; +use OC\AppFramework\Utility\TimeFactory; use OC\L10N\L10N; +use OCA\News\Db\Feed; use \OCA\News\Db\Folder; use OCA\News\Db\FolderMapper; +use OCA\News\Db\FolderMapperV2; +use OCA\News\Service\Exceptions\ServiceNotFoundException; +use OCA\News\Service\FeedServiceV2; use OCA\News\Service\FolderService; use OCA\News\Service\Exceptions\ServiceConflictException; use OCA\News\Service\Exceptions\ServiceValidationException; +use OCA\News\Service\FolderServiceV2; use OCA\News\Utility\Time; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\IConfig; use OCP\IL10N; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; @@ -31,14 +40,19 @@ class FolderServiceTest extends TestCase { /** - * @var \PHPUnit\Framework\MockObject\MockObject|FolderMapper + * @var MockObject|FolderMapperV2 */ - private $folderMapper; + private $mapper; /** - * @var FolderService + * @var MockObject|FeedServiceV2 */ - private $folderService; + private $feedService; + + /** + * @var FolderServiceV2 + */ + private $class; /** * @var int @@ -51,283 +65,245 @@ class FolderServiceTest extends TestCase private $user; /** - * @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface + * @var MockObject|LoggerInterface */ private $logger; - /** - * @var int - */ - private $autoPurgeMinimumInterval; - - /** - * @var \PHPUnit\Framework\MockObject\MockObject|L10N - */ - private $l10n; - protected function setUp(): void { - $this->l10n = $this->getMockBuilder(IL10N::class) - ->disableOriginalConstructor() - ->getMock(); $this->time = 222; - $timeFactory = $this->getMockBuilder(Time::class) + $timeFactory = $this->getMockBuilder(TimeFactory::class) ->disableOriginalConstructor() ->getMock(); $timeFactory->expects($this->any()) ->method('getTime') ->will($this->returnValue($this->time)); - $this->folderMapper = $this->getMockBuilder(FolderMapper::class) + + $this->feedService = $this->getMockBuilder(FeedServiceV2::class) ->disableOriginalConstructor() ->getMock(); - $this->autoPurgeMinimumInterval = 10; - $config = $this->getMockBuilder(IConfig::class) + $this->mapper = $this->getMockBuilder(FolderMapperV2::class) ->disableOriginalConstructor() ->getMock(); $this->logger = $this->getMockBuilder(LoggerInterface::class) ->disableOriginalConstructor() ->getMock(); - $config->expects($this->any()) - ->method('getAppValue') - ->with('news', 'autoPurgeMinimumInterval') - ->will($this->returnValue($this->autoPurgeMinimumInterval)); - $this->folderService = new FolderService( - $this->folderMapper, $this->l10n, $timeFactory, $config, $this->logger - ); - $this->user = 'hi'; + $this->class = new FolderServiceV2($this->mapper, $this->feedService, $timeFactory, $this->logger); } - public function testFindAll() { - $userId = 'jack'; $return = []; - $this->folderMapper->expects($this->once()) - ->method('findAllFromUser') - ->with($this->equalTo($userId)) + $this->mapper->expects($this->once()) + ->method('findAll') ->will($this->returnValue($return)); - $result = $this->folderService->findAllForUser($userId); + $result = $this->class->findAll(); $this->assertEquals($return, $result); } + public function testFindAllForUser() + { + $return = []; + $this->mapper->expects($this->once()) + ->method('findAllFromUser') + ->with('jack') + ->will($this->returnValue($return)); + + $result = $this->class->findAllForUser('jack'); - public function testCreate() + $this->assertEquals($return, $result); + } + + public function testFindAllForUserRecursive() { $folder = new Folder(); - $folder->setName('hey'); - $folder->setParentId(5); - $folder->setUserId('john'); - $folder->setOpened(true); - - $this->folderMapper->expects($this->once()) - ->method('insert') - ->with($this->equalTo($folder)) - ->will($this->returnValue($folder)); + $folder->setId(1); + $this->mapper->expects($this->once()) + ->method('findAllFromUser') + ->with('jack', []) + ->will($this->returnValue([$folder])); - $this->folderMapper->expects($this->once()) - ->method('findByName') - ->with('hey', 'john') - ->will($this->returnValue([])); + $feeds = [new Feed(), new Feed()]; + $this->feedService->expects($this->once()) + ->method('findAllFromFolder') + ->with(1) + ->will($this->returnValue($feeds)); - $result = $this->folderService->create('hey', 'john', 5); + $result = $this->class->findAllForUserRecursive('jack'); - $this->assertEquals($folder, $result); + $folder->feeds = $feeds; + $expected = [$folder]; + $this->assertEquals($expected, $result); + $this->assertEquals($result[0]->feeds, $feeds); } + public function testFindForUser() + { + $return = new Folder(); + $this->mapper->expects($this->once()) + ->method('findFromUser') + ->with('jack', 1) + ->will($this->returnValue($return)); + + $result = $this->class->findForUser('jack', 1); + + $this->assertEquals($return, $result); + } - public function testCreateThrowsExWhenFolderNameExists() + public function testFindForUserEmpty() { - $folderName = 'hihi'; - $rows = [['id' => 1]]; + $this->expectException(ServiceNotFoundException::class); + $this->expectExceptionMessage('Folder not found'); - $this->l10n->expects($this->once()) - ->method('t'); - $this->folderMapper->expects($this->once()) - ->method('findByName') - ->with($this->equalTo($folderName)) - ->will($this->returnValue($rows)); + $this->mapper->expects($this->once()) + ->method('findFromUser') + ->with('jack', 1) + ->will($this->throwException(new DoesNotExistException(''))); - $this->expectException(ServiceConflictException::class); - $this->folderService->create($folderName, 'john', 3); + $this->class->findForUser('jack', 1); } - public function testCreateThrowsExWhenFolderNameEmpty() + public function testFindForUserDupe() { - $this->expectException('\OCA\News\Service\Exceptions\ServiceValidationException'); - $folderName = ''; + $this->expectException(ServiceConflictException::class); + $this->expectExceptionMessage('Multiple folders found'); - $this->folderMapper->expects($this->once()) - ->method('findByName') - ->with($this->equalTo($folderName)) - ->will($this->returnValue([])); + $this->mapper->expects($this->once()) + ->method('findFromUser') + ->with('jack', 1) + ->will($this->throwException(new MultipleObjectsReturnedException(''))); - $this->folderService->create($folderName, 'john', 3); + $this->class->findForUser('jack', 1); } - public function testOpen() + public function testCreate() { $folder = new Folder(); + $folder->setName('hey'); + $folder->setParentId(5); + $folder->setUserId('john'); + $folder->setOpened(true); - $this->folderMapper->expects($this->once()) - ->method('find') - ->with('', 3) + $this->mapper->expects($this->once()) + ->method('insert') + ->with($folder) ->will($this->returnValue($folder)); - $this->folderMapper->expects($this->once()) - ->method('update') - ->with($this->equalTo($folder)); - - $this->folderService->open(3, false, ''); - - $this->assertFalse($folder->getOpened()); + $result = $this->class->create('john', 'hey', 5); + $this->assertEquals($folder, $result); } - - public function testRename() + public function testOpen() { $folder = new Folder(); - $folder->setName('jooohn'); - $this->folderMapper->expects($this->once()) - ->method('find') - ->with('', 3) + $this->mapper->expects($this->once()) + ->method('findFromUser') + ->with('jack', 3) ->will($this->returnValue($folder)); - $this->folderMapper->expects($this->once()) + $this->mapper->expects($this->once()) ->method('update') - ->with($this->equalTo($folder)); - - $this->folderMapper->expects($this->once()) - ->method('findByName') - ->with('bogus', '') - ->will($this->returnValue([])); + ->with($folder); - $this->folderService->rename(3, 'bogus', ''); + $this->class->open('jack', 3, false); - $this->assertEquals('bogus', $folder->getName()); + $this->assertFalse($folder->getOpened()); } - - public function testRenameThrowsExWhenFolderNameExists() + public function testRename() { - $folderName = 'hihi'; - $rows = [['id' => 1]]; - - $this->l10n->expects($this->once()) - ->method('t'); - $this->folderMapper->expects($this->once()) - ->method('findByName') - ->with($this->equalTo($folderName)) - ->will($this->returnValue($rows)); - - $this->expectException(ServiceConflictException::class); - $this->folderService->rename(3, $folderName, 'john'); - } + $folder = new Folder(); + $folder->setName('jooohn'); + $this->mapper->expects($this->once()) + ->method('findFromUser') + ->with('jack', 3) + ->will($this->returnValue($folder)); - public function testRenameThrowsExWhenFolderNameEmpty() - { - $folderName = ''; + $this->mapper->expects($this->once()) + ->method('update') + ->with($folder); - $this->folderMapper->expects($this->once()) - ->method('findByName') - ->with($this->equalTo($folderName)) - ->will($this->returnValue([])); + $this->class->rename('jack', 3, 'newName'); - $this->expectException(ServiceValidationException::class); - $this->folderService->rename(3, $folderName, 'john'); + $this->assertEquals('newName', $folder->getName()); } - public function testMarkDeleted() { - $id = 3; $folder = new Folder(); $folder2 = new Folder(); $folder2->setDeletedAt($this->time); - $this->folderMapper->expects($this->once()) - ->method('find') - ->with($this->equalTo($this->user), $this->equalTo($id)) + $this->mapper->expects($this->once()) + ->method('findFromUser') + ->with('jack', 3) ->will($this->returnValue($folder)); - $this->folderMapper->expects($this->once()) + $this->mapper->expects($this->once()) ->method('update') - ->with($this->equalTo($folder2)); + ->with($folder2); - $this->folderService->markDeleted($id, $this->user); + $this->class->markDelete('jack', 3, true); } - public function testUnmarkDeleted() { - $id = 3; $folder = new Folder(); $folder2 = new Folder(); $folder2->setDeletedAt(0); - $this->folderMapper->expects($this->once()) - ->method('find') - ->with($this->equalTo($this->user), $this->equalTo($id)) + $this->mapper->expects($this->once()) + ->method('findFromUser') + ->with('jack', 3) ->will($this->returnValue($folder)); - $this->folderMapper->expects($this->once()) + $this->mapper->expects($this->once()) ->method('update') - ->with($this->equalTo($folder2)); + ->with($folder2); - $this->folderService->unmarkDeleted($id, $this->user); + $this->class->markDelete('jack', 3, false); } public function testPurgeDeleted() { - $folder1 = new Folder(); - $folder1->setId(3); - $folder2 = new Folder(); - $folder2->setId(5); - $feeds = [$folder1, $folder2]; - - $time = $this->time - $this->autoPurgeMinimumInterval; - $this->folderMapper->expects($this->once()) - ->method('getToDelete') - ->with($this->equalTo($time), $this->equalTo($this->user)) - ->will($this->returnValue($feeds)); - $this->folderMapper->expects($this->exactly(2)) - ->method('delete') - ->withConsecutive([$folder1], [$folder2]); + $this->mapper->expects($this->exactly(1)) + ->method('purgeDeleted'); - $this->folderService->purgeDeleted($this->user); + $this->class->purgeDeleted(); } - - public function testPurgeDeletedNoInterval() + public function testDelete() { - $folder1 = new Folder(); - $folder1->setId(3); - $folder2 = new Folder(); - $folder2->setId(5); - $feeds = [$folder1, $folder2]; - - $this->folderMapper->expects($this->once()) - ->method('getToDelete') - ->with($this->equalTo(null), $this->equalTo($this->user)) - ->will($this->returnValue($feeds)); - $this->folderMapper->expects($this->exactly(2)) + $folder = new Folder(); + $this->mapper->expects($this->once()) + ->method('findFromUser') + ->with('jack', 1) + ->will($this->returnValue($folder)); + $this->mapper->expects($this->once()) ->method('delete') - ->withConsecutive([$folder1], [$folder2]); + ->with($folder) + ->will($this->returnValue($folder)); - $this->folderService->purgeDeleted($this->user, false); + $this->class->delete('jack', 1); } - public function testDeleteUser() { - $this->folderMapper->expects($this->once()) - ->method('deleteUser') - ->will($this->returnValue($this->user)); + $folder = new Folder(); + $this->mapper->expects($this->once()) + ->method('findAllFromUser') + ->with('jack') + ->will($this->returnValue([$folder])); + $this->mapper->expects($this->once()) + ->method('delete') + ->with($folder) + ->will($this->returnValue($folder)); - $this->folderService->deleteUser($this->user); + $this->class->deleteUser('jack'); } |