summaryrefslogtreecommitdiffstats
path: root/tests/Unit/Service/ItemServiceTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Service/ItemServiceTest.php')
-rw-r--r--tests/Unit/Service/ItemServiceTest.php640
1 files changed, 378 insertions, 262 deletions
diff --git a/tests/Unit/Service/ItemServiceTest.php b/tests/Unit/Service/ItemServiceTest.php
index 96d22235a..e82d5eda7 100644
--- a/tests/Unit/Service/ItemServiceTest.php
+++ b/tests/Unit/Service/ItemServiceTest.php
@@ -13,84 +13,63 @@
namespace OCA\News\Tests\Unit\Service;
-use OC\Log;
-use OCA\News\Db\ItemMapper;
use OCA\News\Db\ItemMapperV2;
-use OCA\News\Service\ItemService;
+use OCA\News\Service\Exceptions\ServiceConflictException;
+use OCA\News\Service\Exceptions\ServiceValidationException;
use OCA\News\Service\Exceptions\ServiceNotFoundException;
-use OCA\News\Utility\PsrLogger;
-use OCA\News\Utility\Time;
+use OCA\News\Service\ItemServiceV2;
use \OCP\AppFramework\Db\DoesNotExistException;
use \OCA\News\Db\Item;
use \OCA\News\Db\FeedType;
+use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\IConfig;
+use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
-
+/**
+ * Class ItemServiceTest
+ *
+ * @package OCA\News\Tests\Unit\Service
+ */
class ItemServiceTest extends TestCase
{
/**
- * @var \PHPUnit\Framework\MockObject\MockObject|ItemMapper
- */
- private $oldItemMapper;
-
- /**
- * @var \PHPUnit\Framework\MockObject\MockObject|ItemMapperV2
+ * @var MockObject|ItemMapperV2
*/
private $mapper;
/**
- * @var ItemService
+ * @var ItemServiceV2
*/
- private $itemService;
+ private $class;
/**
- * @var \PHPUnit\Framework\MockObject\MockObject|IConfig
+ * @var MockObject|IConfig
*/
private $config;
/**
- * @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface
+ * @var MockObject|LoggerInterface
*/
private $logger;
-
- /**
- * @var \PHPUnit\Framework\MockObject\MockObject|Time
- */
- private $timeFactory;
-
/**
* @var int
*/
private $newestItemId;
-
/**
* @var string
*/
- private $time;
+ private $user;
protected function setUp(): void
{
- $this->time = '222';
- $this->timeFactory = $this->getMockBuilder(Time::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->timeFactory->expects($this->any())
- ->method('getTime')
- ->will($this->returnValue($this->time));
- $this->timeFactory->expects($this->any())
- ->method('getMicroTime')
- ->will($this->returnValue($this->time));
$this->mapper = $this->getMockBuilder(ItemMapperV2::class)
->disableOriginalConstructor()
->getMock();
- $this->oldItemMapper = $this->getMockBuilder(ItemMapper::class)
- ->disableOriginalConstructor()
- ->getMock();
$this->config = $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor()
->getMock();
@@ -99,10 +78,8 @@ class ItemServiceTest extends TestCase
->disableOriginalConstructor()
->getMock();
- $this->itemService = new ItemService(
+ $this->class = new ItemServiceV2(
$this->mapper,
- $this->oldItemMapper,
- $this->timeFactory,
$this->config,
$this->logger
);
@@ -115,164 +92,126 @@ class ItemServiceTest extends TestCase
$this->newestItemId = 4;
}
-
public function testFindAllNewFeed()
{
- $type = FeedType::FEED;
- $this->oldItemMapper->expects($this->once())
- ->method('findAllNewFeed')
- ->with(
- $this->equalTo(3),
- $this->equalTo(20333),
- $this->equalTo(true),
- $this->equalTo('jack')
- )
+ $this->mapper->expects($this->once())
+ ->method('findAllInFeedAfter')
+ ->with('jack', 2, 20333, true)
->will($this->returnValue([]));
- $result = $this->itemService->findAllNew(3, $type, 20333, true, 'jack');
+ $result = $this->class->findAllInFeedAfter($this->user, 2, 20333, true);
$this->assertEquals([], $result);
}
-
public function testFindAllNewFolder()
{
- $type = FeedType::FOLDER;
- $this->oldItemMapper->expects($this->once())
- ->method('findAllNewFolder')
- ->with(
- $this->equalTo(3),
- $this->equalTo(20333),
- $this->equalTo(true),
- $this->equalTo('jack')
- )
- ->will($this->returnValue(['val']));
+ $this->mapper->expects($this->once())
+ ->method('findAllInFolderAfter')
+ ->with('jack', 2, 20333, true)
+ ->will($this->returnValue([]));
- $result = $this->itemService->findAllNew(3, $type, 20333, true, 'jack');
- $this->assertEquals(['val'], $result);
+ $result = $this->class->findAllInFolderAfter($this->user, 2, 20333, true);
+ $this->assertEquals([], $result);
}
-
- public function testFindAllNew()
+ public function testFindAllNewItem()
{
- $type = FeedType::STARRED;
- $this->oldItemMapper->expects($this->once())
- ->method('findAllNew')
- ->with(
- $this->equalTo(20333),
- $this->equalTo($type),
- $this->equalTo(true),
- $this->equalTo('jack')
- )
- ->will($this->returnValue(['val']));
+ $this->mapper->expects($this->once())
+ ->method('findAllAfter')
+ ->with('jack', 2, 20333)
+ ->will($this->returnValue([]));
- $result = $this->itemService->findAllNew(
- 3, $type, 20333, true,
- 'jack'
- );
- $this->assertEquals(['val'], $result);
+ $result = $this->class->findAllAfter($this->user, 2, 20333);
+ $this->assertEquals([], $result);
}
+ public function testFindAllNewItemWrongType()
+ {
+ $this->expectException(ServiceValidationException::class);
+ $this->expectExceptionMessage('Trying to find in unknown type');
+
+ $this->mapper->expects($this->never())
+ ->method('findAllAfter');
+
+ $result = $this->class->findAllAfter($this->user, 3, 20333);
+ $this->assertEquals([], $result);
+ }
public function testFindAllFeed()
{
- $type = FeedType::FEED;
- $this->oldItemMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('findAllFeed')
- ->with(
- $this->equalTo(3),
- $this->equalTo(20),
- $this->equalTo(5),
- $this->equalTo(true),
- $this->equalTo(false),
- $this->equalTo('jack'),
- $this->equalTo([])
- )
+ ->with('jack', 3, 20, 5, true, false, [])
->will($this->returnValue(['val']));
- $result = $this->itemService->findAllItems(
- 3, $type, 20, 5,
- true, false, 'jack'
+ $result = $this->class->findAllInFeedWithFilters(
+ 'jack',
+ 3,
+ 20,
+ 5,
+ true,
+ false
);
$this->assertEquals(['val'], $result);
}
-
public function testFindAllFolder()
{
- $type = FeedType::FOLDER;
- $this->oldItemMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('findAllFolder')
- ->with(
- $this->equalTo(3),
- $this->equalTo(20),
- $this->equalTo(5),
- $this->equalTo(true),
- $this->equalTo(true),
- $this->equalTo('jack'),
- $this->equalTo([])
- )
+ ->with('jack', 3, 20, 5, true, true, [])
->will($this->returnValue(['val']));
- $result = $this->itemService->findAllItems(
- 3, $type, 20, 5,
- true, true, 'jack'
+ $result = $this->class->findAllInFolderWithFilters(
+ 'jack',
+ 3,
+ 20,
+ 5,
+ true,
+ true,
+ []
);
$this->assertEquals(['val'], $result);
}
-
- public function testFindAll()
+ public function testFindAllItems()
{
$type = FeedType::STARRED;
- $this->oldItemMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('findAllItems')
- ->with(
- $this->equalTo(20),
- $this->equalTo(5),
- $this->equalTo($type),
- $this->equalTo(true),
- $this->equalTo(true),
- $this->equalTo('jack'),
- $this->equalTo([])
- )
+ ->with('jack', $type, 20, 5, true, [])
->will($this->returnValue(['val']));
- $result = $this->itemService->findAllItems(
- 3, $type, 20, 5,
- true, true, 'jack'
- );
+ $result = $this->class->findAllWithFilters('jack', $type, 20, 5, true);
$this->assertEquals(['val'], $result);
}
-
public function testFindAllSearch()
{
$type = FeedType::STARRED;
$search = ['test'];
- $this->oldItemMapper->expects($this->once())
+
+ $this->mapper->expects($this->once())
->method('findAllItems')
- ->with(
- $this->equalTo(20),
- $this->equalTo(5),
- $this->equalTo($type),
- $this->equalTo(true),
- $this->equalTo(true),
- $this->equalTo('jack'),
- $this->equalTo($search)
- )
+ ->with('jack', $type, 20, 5, true, $search)
->will($this->returnValue(['val']));
- $result = $this->itemService->findAllItems(
- 3, $type, 20, 5,
- true, true, 'jack', $search
- );
+ $result = $this->class->findAllWithFilters('jack', $type, 20, 5, true, $search);
$this->assertEquals(['val'], $result);
}
+ public function testFindAll()
+ {
+ $this->mapper->expects($this->once())
+ ->method('findAll')
+ ->will($this->returnValue(['val']));
+ $result = $this->class->findAll();
+ $this->assertEquals(['val'], $result);
+ }
- public function testStar()
+ public function testStarByGuid()
{
$itemId = 3;
$feedId = 5;
@@ -287,20 +226,19 @@ class ItemServiceTest extends TestCase
$expectedItem->setId($itemId);
$this->mapper->expects($this->once())
- ->method('findByGuidHash')
- ->with($feedId, $guidHash)
+ ->method('findForUserByGuidHash')
+ ->with('jack', $feedId, $guidHash)
->will($this->returnValue($item));
$this->mapper->expects($this->once())
->method('update')
->with($this->equalTo($expectedItem));
- $this->itemService->star($feedId, $guidHash, true, 'jack');
+ $this->class->starByGuid('jack', $feedId, $guidHash, true);
$this->assertTrue($item->isStarred());
}
-
public function testUnstar()
{
$itemId = 3;
@@ -317,201 +255,379 @@ class ItemServiceTest extends TestCase
$expectedItem->setId($itemId);
$this->mapper->expects($this->once())
- ->method('findByGuidHash')
- ->with($feedId, $guidHash)
+ ->method('findForUserByGuidHash')
+ ->with('jack', $feedId, $guidHash)
->will($this->returnValue($item));
$this->mapper->expects($this->once())
->method('update')
->with($this->equalTo($expectedItem));
- $this->itemService->star($feedId, $guidHash, false, 'jack');
+ $this->class->starByGuid('jack', $feedId, $guidHash, false);
$this->assertFalse($item->isStarred());
}
public function testRead()
{
- $itemId = 3;
- $item = new Item();
- $item->setId($itemId);
- $item->setUnread(true);
+ $item = $this->getMockBuilder(Item::class)
+ ->getMock();
- $expectedItem = new Item();
- $expectedItem->setUnread(false);
- $expectedItem->setId($itemId);
- $expectedItem->setLastModified($this->time);
-
- $this->oldItemMapper->expects($this->once())
- ->method('readItem')
- ->with(
- $this->equalTo($itemId),
- $this->equalTo(true),
- $this->equalTo($this->time),
- $this->equalTo('jack')
- )
+ $item->expects($this->once())
+ ->method('setUnread')
+ ->with(false);
+
+ $this->mapper->expects($this->once())
+ ->method('findFromUser')
+ ->with('jack', 3)
->will($this->returnValue($item));
- $this->itemService->read($itemId, true, 'jack');
- }
+ $this->mapper->expects($this->once())
+ ->method('update')
+ ->with($item)
+ ->will($this->returnValue($item));
+ $this->class->read('jack', 3, true);
+ }
- public function testReadDoesNotExist()
+ public function testStar()
{
+ $item = $this->getMockBuilder(Item::class)
+ ->getMock();
- $this->expectException(ServiceNotFoundException::class);
- $this->oldItemMapper->expects($this->once())
- ->method('readItem')
- ->will($this->throwException(new DoesNotExistException('')));
+ $item->expects($this->once())
+ ->method('setStarred')
+ ->with(true);
+
+ $this->mapper->expects($this->once())
+ ->method('findFromUser')
+ ->with('jack', 3)
+ ->will($this->returnValue($item));
+
+ $this->mapper->expects($this->once())
+ ->method('update')
+ ->with($item)
+ ->will($this->returnValue($item));
- $this->itemService->read(1, true, 'jack');
+ $this->class->star('jack', 3, true);
}
- public function testStarDoesNotExist()
+ public function testStarByGuidDoesNotExist()
{
$this->expectException(ServiceNotFoundException::class);
$this->mapper->expects($this->once())
- ->method('findByGuidHash')
+ ->method('findForUserByGuidHash')
->will($this->throwException(new DoesNotExistException('')));
- $this->itemService->star(1, 'hash', true, 'jack');
+ $this->class->starByGuid('jack', 1, 'hash', true);
}
+ public function testStarByGuidDuplicate()
+ {
+
+ $this->expectException(ServiceConflictException::class);
+ $this->mapper->expects($this->once())
+ ->method('findForUserByGuidHash')
+ ->will($this->throwException(new MultipleObjectsReturnedException('')));
+
+ $this->class->starByGuid('jack', 1, 'hash', true);
+ }
public function testReadAll()
{
$highestItemId = 6;
- $this->oldItemMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('readAll')
- ->with(
- $this->equalTo($highestItemId),
- $this->equalTo($this->time),
- $this->equalTo('jack')
- );
+ ->with('jack', $highestItemId);
- $this->itemService->readAll($highestItemId, 'jack');
+ $this->class->readAll('jack', $highestItemId);
}
-
- public function testReadFolder()
+ public function testGetNewestItemId()
{
- $folderId = 3;
- $highestItemId = 6;
+ $this->mapper->expects($this->once())
+ ->method('newest')
+ ->with($this->equalTo('jack'))
+ ->will($this->returnValue(Item::fromParams(['id' => 12])));
+
+ $result = $this->class->newest('jack');
+ $this->assertEquals(12, $result->getId());
+ }
- $this->oldItemMapper->expects($this->once())
- ->method('readFolder')
- ->with(
- $this->equalTo($folderId),
- $this->equalTo($highestItemId),
- $this->equalTo($this->time),
- $this->equalTo('jack')
+ public function testGetNewestItemIdDoesNotExist()
+ {
+ $this->mapper->expects($this->once())
+ ->method('newest')
+ ->with($this->equalTo('jack'))
+ ->will(
+ $this->throwException(
+ new DoesNotExistException('There are no items')
+ )
);
- $this->itemService->readFolder($folderId, $highestItemId, 'jack');
+ $this->expectException(ServiceNotFoundException::class);
+ $this->class->newest('jack');
}
-
- public function testReadFeed()
+ public function testGetNewestItemDuplicate()
{
- $feedId = 3;
- $highestItemId = 6;
-
- $this->oldItemMapper->expects($this->once())
- ->method('readFeed')
- ->with(
- $this->equalTo($feedId),
- $this->equalTo($highestItemId),
- $this->equalTo($this->time),
- $this->equalTo('jack')
+ $this->mapper->expects($this->once())
+ ->method('newest')
+ ->with($this->equalTo('jack'))
+ ->will(
+ $this->throwException(
+ new MultipleObjectsReturnedException('There are no items')
+ )
);
- $this->itemService->readFeed($feedId, $highestItemId, 'jack');
+ $this->expectException(ServiceConflictException::class);
+ $this->class->newest('jack');
}
-
- public function testAutoPurgeOldWillPurgeOld()
+ public function testStarredCount()
{
- $this->config->expects($this->once())
- ->method('getAppValue')
- ->with('news', 'autoPurgeCount')
- ->will($this->returnValue(2));
- $this->oldItemMapper->expects($this->once())
- ->method('deleteReadOlderThanThreshold')
- ->with($this->equalTo(2));
+ $this->mapper->expects($this->once())
+ ->method('findAllFromUser')
+ ->with('jack', ['starred' => 1])
+ ->will($this->returnValue([new Item(), new Item()]));
+
+ $result = $this->class->starred('jack');
- $this->itemService->autoPurgeOld();
+ $this->assertEquals(2, count($result));
}
- public function testAutoPurgeOldWontPurgeOld()
+ public function testInsertOrUpdateInserts()
{
- $this->config->expects($this->once())
- ->method('getAppValue')
- ->with('news', 'autoPurgeCount')
- ->will($this->returnValue(-1));
- $this->oldItemMapper->expects($this->never())
- ->method('deleteReadOlderThanThreshold');
+ $item = $this->getMockBuilder(Item::class)
+ ->getMock();
- $this->itemService->autoPurgeOld();
- }
+ $item->expects($this->once())
+ ->method('getFeedId')
+ ->will($this->returnValue(1));
+ $item->expects($this->once())
+ ->method('getGuidHash')
+ ->will($this->returnValue('hash'));
- public function testGetNewestItemId()
- {
- $this->oldItemMapper->expects($this->once())
- ->method('getNewestItemId')
- ->with($this->equalTo('jack'))
- ->will($this->returnValue(12));
+ $this->mapper->expects($this->once())
+ ->method('findByGuidHash')
+ ->with(1, 'hash')
+ ->will($this->throwException(new DoesNotExistException('exception')));
+
+ $this->mapper->expects($this->once())
+ ->method('insert')
+ ->with($item)
+ ->will($this->returnValue($item));
+
+ $result = $this->class->insertOrUpdate($item);
- $result = $this->itemService->getNewestItemId('jack');
- $this->assertEquals(12, $result);
+ $this->assertEquals($item, $result);
}
+ public function testInsertOrUpdateUpdates()
+ {
+ $item = $this->getMockBuilder(Item::class)
+ ->getMock();
+ $db_item = $this->getMockBuilder(Item::class)
+ ->getMock();
+
+ $item->expects($this->once())
+ ->method('getFeedId')
+ ->will($this->returnValue(1));
- public function testGetNewestItemIdDoesNotExist()
+ $item->expects($this->once())
+ ->method('getGuidHash')
+ ->will($this->returnValue('hash'));
+
+ $item->expects($this->once())
+ ->method('setUnread')
+ ->with(true)
+ ->will($this->returnSelf());
+
+ $db_item->expects($this->once())
+ ->method('isUnread')
+ ->will($this->returnValue(true));
+
+ $item->expects($this->once())
+ ->method('setStarred')
+ ->with(true)
+ ->will($this->returnSelf());
+
+ $db_item->expects($this->once())
+ ->method('isStarred')
+ ->will($this->returnValue(true));
+
+ $item->expects($this->once())
+ ->method('generateSearchIndex')
+ ->will($this->returnSelf());
+
+ $item->expects($this->once())
+ ->method('getFingerprint')
+ ->will($this->returnValue('fingerA'));
+
+ $db_item->expects($this->once())
+ ->method('getFingerprint')
+ ->will($this->returnValue('fingerB'));
+
+ $item->expects($this->never())
+ ->method('resetUpdatedFields');
+
+ $this->mapper->expects($this->once())
+ ->method('findByGuidHash')
+ ->with(1, 'hash')
+ ->will($this->returnValue($db_item));
+
+ $this->mapper->expects($this->once())
+ ->method('update')
+ ->with($item)
+ ->will($this->returnValue($item));
+
+ $result = $this->class->insertOrUpdate($item);
+
+ $this->assertEquals($item, $result);
+ }
+
+ public function testInsertOrUpdateSkipsSame()
{
- $this->oldItemMapper->expects($this->once())
- ->method('getNewestItemId')
- ->with($this->equalTo('jack'))
- ->will(
- $this->throwException(
- new DoesNotExistException('There are no items')
- )
- );
+ $item = $this->getMockBuilder(Item::class)
+ ->getMock();
+ $db_item = $this->getMockBuilder(Item::class)
+ ->getMock();
- $this->expectException(ServiceNotFoundException::class);
- $this->itemService->getNewestItemId('jack');
+ $item->expects($this->once())
+ ->method('getFeedId')
+ ->will($this->returnValue(1));
+
+ $item->expects($this->once())
+ ->method('getGuidHash')
+ ->will($this->returnValue('hash'));
+
+ $item->expects($this->once())
+ ->method('setUnread')
+ ->with(true)
+ ->will($this->returnSelf());
+
+ $db_item->expects($this->once())
+ ->method('isUnread')
+ ->will($this->returnValue(true));
+
+ $item->expects($this->once())
+ ->method('setStarred')
+ ->with(true)
+ ->will($this->returnSelf());
+
+ $db_item->expects($this->once())
+ ->method('isStarred')
+ ->will($this->returnValue(true));
+
+ $item->expects($this->once())
+ ->method('generateSearchIndex')
+ ->will($this->returnSelf());
+
+ $item->expects($this->once())
+ ->method('getFingerprint')
+ ->will($this->returnValue('fingerA'));
+
+ $db_item->expects($this->once())
+ ->method('getFingerprint')
+ ->will($this->returnValue('fingerA'));
+
+ $item->expects($this->once())
+ ->method('resetUpdatedFields');
+
+ $this->mapper->expects($this->once())
+ ->method('findByGuidHash')
+ ->with(1, 'hash')
+ ->will($this->returnValue($db_item));
+
+ $this->mapper->expects($this->once())
+ ->method('update')
+ ->with($item)
+ ->will($this->returnValue($item));
+
+ $result = $this->class->insertOrUpdate($item);
+
+ $this->assertEquals($item, $result);
}
+ public function testFindByGuidHash()
+ {
+ $item = $this->getMockBuilder(Item::class)
+ ->getMock();
- public function testStarredCount()
+ $this->mapper->expects($this->once())
+ ->method('findByGuidHash')
+ ->with(1, 'a')
+ ->will($this->returnValue($item));
+
+ $result = $this->class->findByGuidHash(1, 'a');
+
+ $this->assertEquals($item, $result);
+ }
+
+ public function testFindAllInFeed()
{
- $star = 18;
+ $items = [new Item(), new Item()];
- $this->oldItemMapper->expects($this->once())
- ->method('starredCount')
- ->with($this->equalTo('jack'))
- ->will($this->returnValue($star));
+ $this->mapper->expects($this->once())
+ ->method('findAllInFeedAfter')
+ ->with('jack', 1, PHP_INT_MIN, false)
+ ->will($this->returnValue($items));
- $result = $this->itemService->starredCount('jack');
+ $result = $this->class->findAllInFeed('jack', 1);
- $this->assertEquals($star, $result);
+ $this->assertEquals($items, $result);
}
+ public function testPurgeOverThreshold()
+ {
+ $this->mapper->expects($this->once())
+ ->method('deleteOverThreshold')
+ ->with(1, true)
+ ->will($this->returnValue(1));
- public function testGetUnreadOrStarred()
+ $result = $this->class->purgeOverThreshold(1, true);
+
+ $this->assertEquals(1, $result);
+ }
+
+ public function testPurgeOverThresholdWithNegative()
{
- $this->oldItemMapper->expects($this->once())
- ->method('findAllUnreadOrStarred')
- ->with($this->equalTo('jack'))
- ->will($this->returnValue([]));
+ $this->mapper->expects($this->never())
+ ->method('deleteOverThreshold');
- $result = $this->itemService->getUnreadOrStarred('jack');
+ $result = $this->class->purgeOverThreshold(-1, true);
- $this->assertEquals([], $result);
+ $this->assertEquals(null, $result);
}
+ public function testPurgeOverThresholdNull()
+ {
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with('news', 'autoPurgeCount', 200)
+ ->will($this->returnValue(200));
+
+ $this->mapper->expects($this->once())
+ ->method('deleteOverThreshold')
+ ->with(200);
+
+ $this->class->purgeOverThreshold();
+ }
+ public function testPurgeOverThresholdSet()
+ {
+ $this->config->expects($this->never())
+ ->method('getAppValue')
+ ->with('news', 'autoPurgeCount', 200);
+
+ $this->mapper->expects($this->once())
+ ->method('deleteOverThreshold')
+ ->with(5);
+
+ $this->class->purgeOverThreshold(5);
+ }
}