summaryrefslogtreecommitdiffstats
path: root/tests/Unit/Service/FeedServiceTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Service/FeedServiceTest.php')
-rw-r--r--tests/Unit/Service/FeedServiceTest.php1162
1 files changed, 424 insertions, 738 deletions
diff --git a/tests/Unit/Service/FeedServiceTest.php b/tests/Unit/Service/FeedServiceTest.php
index 961d5d5e7..2d45cea70 100644
--- a/tests/Unit/Service/FeedServiceTest.php
+++ b/tests/Unit/Service/FeedServiceTest.php
@@ -14,20 +14,21 @@
namespace OCA\News\Tests\Unit\Service;
+use FeedIo\Explorer;
use FeedIo\Reader\ReadErrorException;
use OC\L10N\L10N;
-use OCA\News\Db\FeedMapper;
-use OCA\News\Db\ItemMapper;
-use OCA\News\Service\FeedService;
+use OCA\News\Db\FeedMapperV2;
+use OCA\News\Fetcher\FeedFetcher;
+use OCA\News\Service\Exceptions\ServiceConflictException;
use OCA\News\Service\Exceptions\ServiceNotFoundException;
+use OCA\News\Service\FeedServiceV2;
+use OCA\News\Service\ItemServiceV2;
use OCA\News\Utility\Time;
use OCP\AppFramework\Db\DoesNotExistException;
use OCA\News\Db\Feed;
use OCA\News\Db\Item;
-use OCA\News\Fetcher\Fetcher;
-use OCA\News\Fetcher\FetcherException;
use OCP\IConfig;
use OCP\IL10N;
@@ -39,25 +40,25 @@ class FeedServiceTest extends TestCase
{
/**
- * @var \PHPUnit\Framework\MockObject\MockObject|FeedMapper
+ * @var \PHPUnit\Framework\MockObject\MockObject|FeedMapperV2
*/
- private $feedMapper;
+ private $mapper;
/**
- * @var \PHPUnit\Framework\MockObject\MockObject|ItemMapper
+ * @var \PHPUnit\Framework\MockObject\MockObject|ItemServiceV2
*/
- private $itemMapper;
+ private $itemService;
- /** @var FeedService */
- private $feedService;
+ /** @var FeedServiceV2 */
+ private $class;
/**
* @var string
*/
- private $user;
+ private $uid;
/**
- * @var \PHPUnit\Framework\MockObject\MockObject|Fetcher
+ * @var \PHPUnit\Framework\MockObject\MockObject|FeedFetcher
*/
private $fetcher;
@@ -86,6 +87,11 @@ class FeedServiceTest extends TestCase
*/
private $logger;
+ /**
+ * @var \PHPUnit\Framework\MockObject\MockObject|Explorer
+ */
+ private $explorer;
+
protected function setUp(): void
{
$this->logger = $this->getMockBuilder(LoggerInterface::class)
@@ -102,16 +108,20 @@ class FeedServiceTest extends TestCase
$this->l10n = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor()
->getMock();
- $this->feedMapper = $this
- ->getMockBuilder(FeedMapper::class)
+ $this->mapper = $this
+ ->getMockBuilder(FeedMapperV2::class)
->disableOriginalConstructor()
->getMock();
$this->fetcher = $this
- ->getMockBuilder(Fetcher::class)
+ ->getMockBuilder(FeedFetcher::class)
->disableOriginalConstructor()
->getMock();
- $this->itemMapper = $this
- ->getMockBuilder(ItemMapper::class)
+ $this->explorer = $this
+ ->getMockBuilder(Explorer::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->itemService = $this
+ ->getMockBuilder(ItemServiceV2::class)
->disableOriginalConstructor()
->getMock();
$this->purifier = $this
@@ -126,26 +136,29 @@ class FeedServiceTest extends TestCase
->with('news', 'autoPurgeMinimumInterval')
->will($this->returnValue($this->autoPurgeMinimumInterval));
- $this->feedService = new FeedService(
- $this->feedMapper,
- $this->fetcher, $this->itemMapper, $this->logger, $this->l10n,
- $timeFactory, $config, $this->purifier
+ $this->class = new FeedServiceV2(
+ $this->mapper,
+ $this->fetcher,
+ $this->itemService,
+ $this->explorer,
+ $this->purifier,
+ $this->logger
);
- $this->user = 'jack';
+ $this->uid = 'jack';
}
/**
- * @covers \OCA\News\Service\FeedService::findAll
+ * @covers \OCA\News\Service\FeedServiceV2::findAll
*/
public function testFindAll()
{
$this->response = [];
- $this->feedMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('findAllFromUser')
- ->with($this->user)
+ ->with($this->uid)
->will($this->returnValue([]));
- $result = $this->feedService->findAllForUser($this->user);
+ $result = $this->class->findAllForUser($this->uid);
$this->assertEquals([], $result);
}
@@ -154,12 +167,20 @@ class FeedServiceTest extends TestCase
{
$ex = new ReadErrorException('hi');
$url = 'test';
- $this->fetcher->expects($this->once())
+
+ $this->mapper->expects($this->once())
+ ->method('findByURL')
+ ->with($this->uid, $url)
+ ->willReturn(new Feed());
+
+ $this->fetcher->expects($this->never())
->method('fetch')
->with($url)
->will($this->throwException($ex));
- $this->expectException(ServiceNotFoundException::class);
- $this->feedService->create($url, 1, $this->user);
+
+ $this->expectException(ServiceConflictException::class);
+ $this->expectExceptionMessage('Feed with this URL exists');
+ $this->class->create($this->uid, $url, 1);
}
public function testCreate()
@@ -167,7 +188,6 @@ class FeedServiceTest extends TestCase
$url = 'http://test';
$folderId = 10;
$createdFeed = new Feed();
- $ex = new DoesNotExistException('yo');
$createdFeed->setUrl($url);
$createdFeed->setUrlHash('hsssi');
$createdFeed->setLink($url);
@@ -185,20 +205,22 @@ class FeedServiceTest extends TestCase
[$item1, $item2]
];
- $this->feedMapper->expects($this->once())
- ->method('findByUrlHash')
- ->with(
- $this->equalTo($createdFeed->getUrlHash()),
- $this->equalTo($this->user)
- )
- ->will($this->throwException($ex));
+ $this->mapper->expects($this->once())
+ ->method('findByURL')
+ ->with($this->uid, $url)
+ ->will($this->throwException(new DoesNotExistException('no')));
+ $this->explorer->expects($this->once())
+ ->method('discover')
+ ->with($url)
+ ->will($this->returnValue([]));
$this->fetcher->expects($this->once())
->method('fetch')
- ->with($this->equalTo($url))
+ ->with($url)
->will($this->returnValue($return));
- $this->feedMapper->expects($this->once())
+
+ $this->mapper->expects($this->once())
->method('insert')
- ->with($this->equalTo($createdFeed))
+ ->with($createdFeed)
->will(
$this->returnCallback(
function () use ($createdFeed) {
@@ -207,31 +229,128 @@ class FeedServiceTest extends TestCase
}
)
);
- $this->itemMapper->expects($this->exactly(2))
- ->method('findByGuidHash')
- ->withConsecutive(
- [$item2->getGuidHash(), $item2->getFeedId(), $this->user],
- [$item1->getGuidHash(), $item1->getFeedId(), $this->user]
- )
- ->will($this->throwException($ex));
- $this->purifier->expects($this->exactly(2))
- ->method('purify')
- ->withConsecutive(
- [$return[1][1]->getBody()],
- [$return[1][0]->getBody()]
- )
- ->willReturnOnConsecutiveCalls(
- $return[1][1]->getBody(),
- $return[1][0]->getBody()
+ $feed = $this->class->create(
+ $this->uid, $url, $folderId, false, null,
+ 'user', 'pass'
+ );
+
+ $this->assertEquals($feed->getFolderId(), $folderId);
+ $this->assertEquals($feed->getUrl(), $url);
+ $this->assertEquals($feed->getArticlesPerUpdate(), 2);
+ $this->assertEquals($feed->getBasicAuthUser(), 'user');
+ $this->assertEquals($feed->getBasicAuthPassword(), 'pass');
+ }
+
+ public function testCreateSetsTitle()
+ {
+ $url = 'http://test';
+ $folderId = 10;
+ $createdFeed = new Feed();
+ $createdFeed->setUrl($url);
+ $createdFeed->setUrlHash('hsssi');
+ $createdFeed->setLink($url);
+ $createdFeed->setTitle('hehoy');
+ $createdFeed->setBasicAuthUser('user');
+ $createdFeed->setBasicAuthPassword('pass');
+ $item1 = new Item();
+ $item1->setFeedId(4);
+ $item1->setGuidHash('hi');
+ $item2 = new Item();
+ $item2->setFeedId(4);
+ $item2->setGuidHash('yo');
+ $return = [
+ $createdFeed,
+ [$item1, $item2]
+ ];
+
+ $this->mapper->expects($this->once())
+ ->method('findByURL')
+ ->with($this->uid, $url)
+ ->will($this->throwException(new DoesNotExistException('no')));
+ $this->explorer->expects($this->once())
+ ->method('discover')
+ ->with($url)
+ ->will($this->returnValue([]));
+ $this->fetcher->expects($this->once())
+ ->method('fetch')
+ ->with($url)
+ ->will($this->returnValue($return));
+
+ $this->mapper->expects($this->once())
+ ->method('insert')
+ ->with($createdFeed)
+ ->will(
+ $this->returnCallback(
+ function () use ($createdFeed) {
+ $createdFeed->setId(4);
+ return $createdFeed;
+ }
+ )
);
- $this->itemMapper->expects($this->exactly(2))
+ $feed = $this->class->create(
+ $this->uid, $url, $folderId, false, 'title',
+ 'user', 'pass'
+ );
+
+ $this->assertEquals($feed->getFolderId(), $folderId);
+ $this->assertEquals($feed->getUrl(), $url);
+ $this->assertEquals($feed->getArticlesPerUpdate(), 2);
+ $this->assertEquals($feed->getTitle(), 'title');
+ $this->assertEquals($feed->getBasicAuthUser(), 'user');
+ $this->assertEquals($feed->getBasicAuthPassword(), 'pass');
+ }
+
+ public function testCreateDiscovers()
+ {
+ $url = 'http://test';
+ $folderId = 10;
+ $createdFeed = new Feed();
+ $createdFeed->setUrl($url);
+ $createdFeed->setUrlHash('hsssi');
+ $createdFeed->setLink($url);
+ $createdFeed->setTitle('hehoy');
+ $createdFeed->setBasicAuthUser('user');
+ $createdFeed->setBasicAuthPassword('pass');
+ $item1 = new Item();
+ $item1->setFeedId(4);
+ $item1->setGuidHash('hi');
+ $item2 = new Item();
+ $item2->setFeedId(4);
+ $item2->setGuidHash('yo');
+ $return = [
+ $createdFeed,
+ [$item1, $item2]
+ ];
+
+ $this->mapper->expects($this->once())
+ ->method('findByURL')
+ ->with($this->uid, $url)
+ ->will($this->throwException(new DoesNotExistException('no')));
+ $this->explorer->expects($this->once())
+ ->method('discover')
+ ->with($url)
+ ->will($this->returnValue(['http://discover.test']));
+ $this->fetcher->expects($this->once())
+ ->method('fetch')
+ ->with('http://discover.test')
+ ->will($this->returnValue($return));
+
+ $this->mapper->expects($this->once())
->method('insert')
- ->withConsecutive([$return[1][1]], [$return[1][0]]);
+ ->with($createdFeed)
+ ->will(
+ $this->returnCallback(
+ function () use ($createdFeed) {
+ $createdFeed->setId(4);
+ return $createdFeed;
+ }
+ )
+ );
- $feed = $this->feedService->create(
- $url, $folderId, $this->user, null,
+ $feed = $this->class->create(
+ $this->uid, $url, $folderId, false, null,
'user', 'pass'
);
@@ -263,18 +382,15 @@ class FeedServiceTest extends TestCase
[$item1, $item2]
];
- $this->feedMapper->expects($this->once())
- ->method('findByUrlHash')
- ->with(
- $this->equalTo($createdFeed->getUrlHash()),
- $this->equalTo($this->user)
- )
+ $this->mapper->expects($this->once())
+ ->method('findByURL')
+ ->with($this->uid, $url)
->will($this->throwException($ex));
$this->fetcher->expects($this->once())
->method('fetch')
->with($this->equalTo($url))
->will($this->returnValue($return));
- $this->feedMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('insert')
->with($this->equalTo($createdFeed))
->will(
@@ -285,716 +401,251 @@ class FeedServiceTest extends TestCase
}
)
);
- $this->itemMapper->expects($this->exactly(2))
- ->method('findByGuidHash')
- ->withConsecutive(
- [$item2->getGuidHash(), $item2->getFeedId(), $this->user],
- [$item1->getGuidHash(), $item1->getFeedId(), $this->user]
- )
- ->willReturnOnConsecutiveCalls($this->throwException($ex), null);
- $this->purifier->expects($this->exactly(1))
- ->method('purify')
- ->withConsecutive([$return[1][1]->getBody()])
- ->willReturnOnConsecutiveCalls($return[1][1]->getBody());
- $this->itemMapper->expects($this->exactly(1))
- ->method('insert')
- ->withConsecutive([$return[1][1]]);
- $feed = $this->feedService->create($url, $folderId, $this->user);
+ $feed = $this->class->create($this->uid, $url, $folderId);
$this->assertEquals($feed->getFolderId(), $folderId);
$this->assertEquals($feed->getUrl(), $url);
- $this->assertEquals(1, $feed->getUnreadCount());
}
- public function testCreateUnableToParseFeed()
+ public function testCreateUnableToFetchFeed()
{
$url = 'http://test';
$folderId = 10;
$this->fetcher->expects($this->once())
->method('fetch')
- ->with($this->equalTo($url))
+ ->with($url)
->willReturn([null, []]);
- $this->l10n->expects($this->once())
- ->method('t')
- ->with($this->equalTo('Can not add feed: Unable to parse feed'))
- ->willReturn('Can not add feed: Unable to parse feed');
+ $this->mapper->expects($this->once())
+ ->method('findByURL')
+ ->with($this->uid, $url)
+ ->will($this->throwException(new DoesNotExistException('no')));
$this->expectException(ServiceNotFoundException::class);
- $this->expectExceptionMessage('Can not add feed: Unable to parse feed');
+ $this->expectExceptionMessage('Failed to fetch feed');
- $this->feedService->create($url, $folderId, $this->user);
+ $this->class->create($this->uid, $url, $folderId);
}
- public function testUpdateCreatesNewEntry()
- {
- $feed = new Feed();
- $feed->setId(3);
- $feed->setArticlesPerUpdate(1);
- $feed->setLink('http://test');
- $feed->setUrl('http://test');
- $feed->setUrlHash('yo');
- $feed->setHttpLastModified(3);
- $feed->setHttpEtag(4);
-
- $item = new Item();
- $item->setGuidHash(md5('hi'));
- $item->setFeedId(3);
- $items = [$item];
-
- $ex = new DoesNotExistException('hi');
-
- $fetchReturn = [$feed, $items];
-
- $this->feedMapper->expects($this->exactly(2))
- ->method('findFromUser')
- ->with($this->user, $feed->getId())
- ->will($this->returnValue($feed));
- $this->fetcher->expects($this->once())
- ->method('fetch')
- ->with(
- $this->equalTo('http://test'),
- $this->equalTo(false),
- $this->equalTo(3),
- $this->equalTo(''),
- $this->equalTo('')
- )
- ->will($this->returnValue($fetchReturn));
- $this->feedMapper->expects($this->exactly(1))
- ->method('update')
- ->with($this->equalTo($feed));
- $this->itemMapper->expects($this->once())
- ->method('findByGuidHash')
- ->with(
- $this->equalTo($items[0]->getGuidHash()),
- $this->equalTo($items[0]->getFeedId()),
- $this->equalTo($this->user)
- )
- ->will($this->throwException($ex));
- $this->purifier->expects($this->exactly(1))
- ->method('purify')
- ->with($this->equalTo($items[0]->getBody()))
- ->will($this->returnValue($items[0]->getBody()));
- $this->itemMapper->expects($this->once())
- ->method('insert')
- ->with($this->equalTo($items[0]));
-
-
- $return = $this->feedService->update($this->user, $feed->getId());
-
- $this->assertEquals($return, $feed);
- }
-
- public function testForceUpdateUpdatesEntry()
+ public function testCreateUnableToParseFeed()
{
- $feed = new Feed();
- $feed->setId(3);
- $feed->setArticlesPerUpdate(1);
- $feed->setLink('http://test');
- $feed->setUrl('http://test');
- $feed->setUrlHash('yo');
- $feed->setHttpLastModified(3);
- $feed->setHttpEtag(4);
-
- $item = new Item();
- $item->setGuidHash(md5('hi'));
- $item->setFeedId(3);
- $items = [$item];
-
- $ex = new DoesNotExistException('hi');
-
- $fetchReturn = [$feed, $items];
+ $url = 'http://test';
+ $folderId = 10;
- $this->feedMapper->expects($this->exactly(2))
- ->method('findFromUser')
- ->with($this->user, $feed->getId())
- ->will($this->returnValue($feed));
$this->fetcher->expects($this->once())
->method('fetch')
- ->with(
- $this->equalTo('http://test'),
- $this->equalTo(false),
- $this->equalTo(3),
- $this->equalTo(''),
- $this->equalTo('')
- )
- ->will($this->returnValue($fetchReturn));
- $this->feedMapper->expects($this->exactly(1))
- ->method('update')
- ->with($this->equalTo($feed));
- $this->itemMapper->expects($this->once())
- ->method('findByGuidHash')
- ->with(
- $this->equalTo($items[0]->getGuidHash()),
- $this->equalTo($items[0]->getFeedId()),
- $this->equalTo($this->user)
- )
- ->will($this->returnValue($items[0]));
- $this->purifier->expects($this->exactly(1))
- ->method('purify')
- ->with($this->equalTo($items[0]->getBody()))
- ->will($this->returnValue($items[0]->getBody()));
- $this->itemMapper->expects($this->once())
- ->method('update')
- ->with($this->equalTo($items[0]));
+ ->with($url)
+ ->will($this->throwException(new ReadErrorException('ERROR')));
+ $this->mapper->expects($this->once())
+ ->method('findByURL')
+ ->with($this->uid, $url)
+ ->will($this->throwException(new DoesNotExistException('no')));
- $return = $this->feedService->update($this->user, $feed->getId(), true);
+ $this->expectException(ServiceNotFoundException::class);
+ $this->expectExceptionMessage('ERROR');
- $this->assertEquals($return, $feed);
+ $this->class->create($this->uid, $url, $folderId);
}
- private function createUpdateFeed()
+ public function testFetchReturnsOnBlock()
{
- $feed = new Feed();
- $feed->setId(3);
- $feed->setArticlesPerUpdate(1);
- $feed->setLink('http://test');
- $feed->setUrl('http://test');
- $feed->setUrlHash('yo');
- $feed->setHttpLastModified(3);
- $feed->setHttpEtag(4);
- return $feed;
- }
+ $feed = $this->getMockBuilder(Feed::class)
+ ->disableOriginalConstructor()
+ ->getMock();
- private function createUpdateItem()
- {
- $item = new Item();
- $item->setGuidHash(md5('hi'));
- $item->setFeedId(3);
- $item->setPubDate(2);
- $item->setUpdatedDate(2);
- $item->setTitle('hey');
- $item->setAuthor('aut');
- $item->setBody('new');
- $item->setUnread(false);
- return $item;
- }
+ $feed->expects($this->once())
+ ->method('getPreventUpdate')
+ ->will($this->returnValue(TRUE));
- private function createUpdateItem2()
- {
- $item = new Item();
- $item->setGuidHash(md5('hi'));
- $item->setFeedId(3);
- $item->setPubDate(1);
- $item->setUpdatedDate(1);
- $item->setTitle('ho');
- $item->setAuthor('auto');
- $item->setBody('old');
- $item->setUnread(false);
- return $item;
+ $this->assertSame($feed, $this->class->fetch($feed));
}
- public function testUpdateUpdatesWhenUpdatedDateIsNewer()
+ public function testFetchAllReturnsOnAllBlock()
{
- $feed = $this->createUpdateFeed();
- $item = $this->createUpdateItem();
- $item2 = $this->createUpdateItem2();
-
- $items = [$item];
-
- $fetchReturn = [$feed, $items];
-
- $this->feedMapper->expects($this->exactly(2))
- ->method('findFromUser')
- ->will($this->returnValue($feed));
- $this->fetcher->expects($this->once())
- ->method('fetch')
- ->will($this->returnValue($fetchReturn));
- $this->feedMapper->expects($this->exactly(1))
- ->method('update');
- $this->itemMapper->expects($this->once())
- ->method('findByGuidHash')
- ->will($this->returnValue($item2));
- $this->purifier->expects($this->exactly(1))
- ->method('purify')
- ->will($this->returnValue($items[0]->getBody()));
- $this->itemMapper->expects($this->once())
- ->method('update')
- ->with($this->equalTo($item2));
+ $feed = $this->getMockBuilder(Feed::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->mapper->expects($this->once())
+ ->method('findAll')
+ ->will($this->returnValue([$feed, $feed]));
- $return = $this->feedService->update($this->user, $feed->getId());
+ $feed->expects($this->exactly(2))
+ ->method('getPreventUpdate')
+ ->will($this->returnValue(TRUE));
- $this->assertEquals($return, $feed);
+ $this->class->fetchAll();
}
-
- public function testUpdateSetsUnreadIfModeIsOne()
+ public function testFetchReturnsOnReadError()
{
- $feed = $this->createUpdateFeed();
- $feed->setUpdateMode(1);
- $item = $this->createUpdateItem();
- $item2 = $this->createUpdateItem2();
- $item3 = $this->createUpdateItem();
- $item3->setUnread(true);
+ $feed = $this->getMockBuilder(Feed::class)
+ ->disableOriginalConstructor()
+ ->getMock();
- $items = [$item];
+ $feed->expects($this->once())
+ ->method('getPreventUpdate')
+ ->will($this->returnValue(FALSE));
- $fetchReturn = [$feed, $items];
+ $feed->expects($this->once())
+ ->method('getLocation')
+ ->will($this->returnValue('location'));
- $this->feedMapper->expects($this->exactly(2))
- ->method('findFromUser')
- ->will($this->returnValue($feed));
$this->fetcher->expects($this->once())
- ->method('fetch')
- ->will($this->returnValue($fetchReturn));
- $this->feedMapper->expects($this->exactly(1))
- ->method('update');
- $this->itemMapper->expects($this->once())
- ->method('findByGuidHash')
- ->will($this->returnValue($item2));
- $this->purifier->expects($this->exactly(1))
- ->method('purify')
- ->will($this->returnValue($items[0]->getBody()));
- $this->itemMapper->expects($this->once())
- ->method('update')
- ->with($this->equalTo($item3));
-
- $return = $this->feedService->update($this->user, $feed->getId());
+ ->method('fetch')
+ ->will($this->throwException(new ReadErrorException('FAIL')));
- $this->assertEquals($return, $feed);
+ $feed->expects($this->once())
+ ->method('getUpdateErrorCount')
+ ->will($this->returnValue(1));
- }
+ $feed->expects($this->once())
+ ->method('setUpdateErrorCount')
+ ->with(2);
- public function testUpdateUpdatesArticlesPerFeedCount()
- {
- $feed = new Feed();
- $feed->setId(3);
- $feed->setUrl('http://example.com');
- $feed->setUrlHash('yo');
-
- $existingFeed = new Feed();
- $existingFeed->setId(3);
- $existingFeed->setUrl('http://example.com');
- $feed->setArticlesPerUpdate(2);
-
- $item = new Item();
- $item->setGuidHash(md5('hi'));
- $item->setFeedId(3);
- $items = [$item];
-
- $this->feedMapper->expects($this->any())
- ->method('findFromUser')
- ->will($this->returnValue($existingFeed));
+ $feed->expects($this->once())
+ ->method('setLastUpdateError')
+ ->with('FAIL');
- $this->fetcher->expects($this->once())
- ->method('fetch')
- ->will($this->returnValue([$feed, $items]));
-
- $this->feedMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('update')
- ->with($this->equalTo($existingFeed));
+ ->with($feed);
- $this->itemMapper->expects($this->any())
- ->method('findByGuidHash')
- ->will($this->returnValue($item));
-
- $this->feedService->update($this->user, $feed->getId());
+ $this->class->fetch($feed);
}
- public function testUpdateFails()
+ public function testFetchReturnsNoUpdate()
{
- $feed = new Feed();
- $feed->setId(3);
- $feed->setUrl('http://example.com');
- $feed->setUpdateErrorCount(0);
- $feed->setLastUpdateError('');
-
- $expectedFeed = new Feed();
- $expectedFeed->setId(3);
- $expectedFeed->setUrl('http://example.com');
- $expectedFeed->setUpdateErrorCount(1);
- $expectedFeed->setLastUpdateError('hi');
+ $feed = $this->getMockBuilder(Feed::class)
+ ->disableOriginalConstructor()
+ ->getMock();
- $ex = new ReadErrorException('hi');
+ $feed->expects($this->once())
+ ->method('getPreventUpdate')
+ ->will($this->returnValue(FALSE));
- $this->feedMapper->expects($this->exactly(2))
- ->method('findFromUser')
- ->with($this->user, $feed->getId())
- ->willReturnOnConsecutiveCalls($feed, $expectedFeed);
- $this->fetcher->expects($this->once())
- ->method('fetch')
- ->will($this->throwException($ex));
- $this->logger->expects($this->any())
- ->method('debug');
+ $feed->expects($this->once())
+ ->method('getLocation')
+ ->will($this->returnValue('location'));
- $this->feedMapper->expects($this->exactly(1))
- ->method('update')
- ->with($expectedFeed)
- ->will($this->returnValue($expectedFeed));
+ $this->fetcher->expects($this->once())
+ ->method('fetch')
+ ->will($this->returnValue([null, []]));
- $return = $this->feedService->update($this->user, $feed->getId());
+ $this->mapper->expects($this->never())
+ ->method('update');
- $this->assertEquals($return, $expectedFeed);
+ $this->assertSame($feed, $this->class->fetch($feed));
}
-
- public function testUpdateDoesNotFindEntry()
+ public function testFetchSucceedsEmptyItems()
{
- $feed = new Feed();
- $feed->setId(3);
+ $feed = $this->getMockBuilder(Feed::class)
+ ->disableOriginalConstructor()
+ ->getMock();
- $ex = new DoesNotExistException('');
+ $feed->expects($this->once())
+ ->method('getPreventUpdate')
+ ->will($this->returnValue(FALSE));
- $this->feedMapper->expects($this->exactly(1))
- ->method('findFromUser')
- ->with($this->user, $feed->getId())
- ->will($this->throwException($ex));
+ $feed->expects($this->once())
+ ->method('getLocation')
+ ->will($this->returnValue('location'));
- $this->expectException(ServiceNotFoundException::class);
- $this->feedService->update($this->user, $feed->getId());
- }
-
-
- public function testUpdatePassesFullText()
- {
- $feed = new Feed();
- $feed->setId(3);
- $feed->setUrl('https://goo.com');
- $feed->setHttpLastModified(123);
- $feed->setFullTextEnabled(true);
+ $feed->expects($this->once())
+ ->method('setUnreadCount')
+ ->with(0)
+ ->will($this->returnSelf());
- $ex = new DoesNotExistException('');
-
- $this->feedMapper->expects($this->exactly(1))
- ->method('findFromUser')
- ->with($this->user, $feed->getId())
- ->will($this->returnValue($feed));
+ $new_feed = $this->getMockBuilder(Feed::class)
+ ->disableOriginalConstructor()
+ ->getMock();
$this->fetcher->expects($this->once())
- ->method('fetch')
- ->with(
- $this->equalTo($feed->getUrl()),
- $this->equalTo(false),
- $this->equalTo($feed->getHttpLastModified()),
- $this->equalTo($feed->getFullTextEnabled())
- )
- ->will($this->throwException($ex));
-
- $this->expectException(DoesNotExistException::class);
- $this->feedService->update($this->user, $feed->getId());
- }
-
-
- public function testUpdateDoesNotFindUpdatedEntry()
- {
- $feed = new Feed();
- $feed->setId(3);
- $feed->setArticlesPerUpdate(1);
- $feed->setUrl('http://example.com');
-
- $item = new Item();
- $item->setGuidHash(md5('hi'));
- $item->setPubDate(3333);
- $item->setId(4);
- $items = [$item];
-
- $item2 = new Item();
- $item2->setPubDate(111);
+ ->method('fetch')
+ ->will($this->returnValue([$new_feed, []]));
- $fetchReturn = [$feed, $items];
- $ex = new DoesNotExistException('');
-
- $this->feedMapper->expects($this->exactly(2))
- ->method('findFromUser')
- ->with($this->user, $feed->getId())
- ->willReturnOnConsecutiveCalls($feed, $this->throwException($ex));
- $this->feedMapper->expects($this->exactly(1))
+ $this->mapper->expects($this->exactly(1))
->method('update')
- ->with($this->equalTo($feed));
- $this->fetcher->expects($this->once())
- ->method('fetch')
- ->will($this->returnValue($fetchReturn));
- $this->itemMapper->expects($this->once())
- ->method('findByGuidHash')
- ->with(
- $this->equalTo($item->getGuidHash()),
- $this->equalTo($feed->getId()),
- $this->equalTo($this->user)
- )
- ->will($this->returnValue($item2));
-
- $this->expectException(ServiceNotFoundException::class);
- $this->feedService->update($this->user, $feed->getId());
- }
-
-
- public function testUpdateDoesntUpdateIfFeedIsPrevented()
- {
- $feedId = 3;
- $feed = new Feed();
- $feed->setFolderId(16);
- $feed->setId($feedId);
- $feed->setPreventUpdate(true);
-
- $this->feedMapper->expects($this->once())
- ->method('findFromUser')
- ->with($this->user, $feed->getId())
+ ->with($feed)
->will($this->returnValue($feed));
- $this->fetcher->expects($this->never())
- ->method('fetch');
- $this->feedSer