summaryrefslogtreecommitdiffstats
path: root/tests/unit/businesslayer
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-04-15 16:02:32 +0200
committerBernhard Posselt <nukeawhale@gmail.com>2013-04-15 16:02:32 +0200
commit464ff6c4c1bda3edbd0f132c4d3d866539d3a117 (patch)
tree96b8fd57e24ebaab762a190a933cd98e1c7a4881 /tests/unit/businesslayer
parent89c31ab5fcb2f931fecc5ce82608ff7c8129510a (diff)
renamed bl to businesslayer, handle exception in update routine, fix #69
Diffstat (limited to 'tests/unit/businesslayer')
-rw-r--r--tests/unit/businesslayer/BusinessLayerTest.php110
-rw-r--r--tests/unit/businesslayer/FeedBusinessLayerTest.php405
-rw-r--r--tests/unit/businesslayer/FolderBusinessLayerTest.php164
-rw-r--r--tests/unit/businesslayer/ItemBusinessLayerTest.php276
-rw-r--r--tests/unit/businesslayer/StatusFlagTest.php74
5 files changed, 1029 insertions, 0 deletions
diff --git a/tests/unit/businesslayer/BusinessLayerTest.php b/tests/unit/businesslayer/BusinessLayerTest.php
new file mode 100644
index 000000000..7c064b377
--- /dev/null
+++ b/tests/unit/businesslayer/BusinessLayerTest.php
@@ -0,0 +1,110 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Cosentino
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+namespace OCA\News\BusinessLayer;
+
+require_once(__DIR__ . "/../../classloader.php");
+
+
+use \OCA\AppFramework\Db\DoesNotExistException;
+use \OCA\AppFramework\Db\MultipleObjectsReturnedException;
+use \OCA\News\Db\Folder;
+
+
+class TestBusinessLayer extends BusinessLayer {
+ public function __construct($mapper){
+ parent::__construct($mapper);
+ }
+}
+
+class BusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
+
+ protected $api;
+ protected $mapper;
+ protected $newsBusinessLayer;
+
+ protected function setUp(){
+ $this->api = $this->getAPIMock();
+ $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->newsBusinessLayer = new TestBusinessLayer($this->mapper);
+ }
+
+
+ public function testDelete(){
+ $id = 5;
+ $user = 'ken';
+ $folder = new Folder();
+ $folder->setId($id);
+
+ $this->mapper->expects($this->once())
+ ->method('delete')
+ ->with($this->equalTo($folder));
+ $this->mapper->expects($this->once())
+ ->method('find')
+ ->with($this->equalTo($id), $this->equalTo($user))
+ ->will($this->returnValue($folder));
+
+ $result = $this->newsBusinessLayer->delete($id, $user);
+ }
+
+
+ public function testFind(){
+ $id = 3;
+ $user = 'ken';
+
+ $this->mapper->expects($this->once())
+ ->method('find')
+ ->with($this->equalTo($id), $this->equalTo($user));
+
+ $result = $this->newsBusinessLayer->find($id, $user);
+ }
+
+
+ public function testFindDoesNotExist(){
+ $ex = new DoesNotExistException('hi');
+
+ $this->mapper->expects($this->once())
+ ->method('find')
+ ->will($this->throwException($ex));
+
+ $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
+ $this->newsBusinessLayer->find(1, '');
+ }
+
+
+ public function testFindMultiple(){
+ $ex = new MultipleObjectsReturnedException('hi');
+
+ $this->mapper->expects($this->once())
+ ->method('find')
+ ->will($this->throwException($ex));
+
+ $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
+ $this->newsBusinessLayer->find(1, '');
+ }
+
+}
diff --git a/tests/unit/businesslayer/FeedBusinessLayerTest.php b/tests/unit/businesslayer/FeedBusinessLayerTest.php
new file mode 100644
index 000000000..3f1ed9a56
--- /dev/null
+++ b/tests/unit/businesslayer/FeedBusinessLayerTest.php
@@ -0,0 +1,405 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Cosentino
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+
+namespace OCA\News\BusinessLayer;
+
+require_once(__DIR__ . "/../../classloader.php");
+
+use \OCA\AppFramework\Db\DoesNotExistException;
+
+use \OCA\News\Db\Feed;
+use \OCA\News\Db\Item;
+use \OCA\News\Utility\Fetcher;
+use \OCA\News\Utility\FetcherException;
+
+class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
+
+ private $mapper;
+ private $businessLayer;
+ private $user;
+ private $response;
+ private $fetcher;
+ private $itemMapper;
+ private $threshold;
+
+ protected function setUp(){
+ $this->api = $this->getAPIMock();
+ $this->mapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->fetcher = $this->getMockBuilder('\OCA\News\Utility\Fetcher')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->itemMapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->businessLayer = new FeedBusinessLayer($this->mapper,
+ $this->fetcher, $this->itemMapper, $this->api);
+ $this->user = 'jack';
+ $response = 'hi';
+
+ }
+
+
+ public function testFindAll(){
+ $this->mapper->expects($this->once())
+ ->method('findAllFromUser')
+ ->with($this->equalTo($this->user))
+ ->will($this->returnValue($this->response));
+
+ $result = $this->businessLayer->findAll($this->user);
+ $this->assertEquals($this->response, $result);
+ }
+
+
+ public function testCreateDoesNotFindFeed(){
+ $ex = new FetcherException('hi');
+ $url = 'test';
+ $trans = $this->getMock('Trans', array('t'));
+ $trans->expects($this->once())
+ ->method('t');
+ $this->api->expects($this->once())
+ ->method('getTrans')
+ ->will($this->returnValue($trans));
+ $this->mapper->expects($this->once())
+ ->method('findByUrlHash')
+ ->with($this->equalTo(md5($url)), $this->equalTo($this->user))
+ ->will($this->throwException(new DoesNotExistException('yo')));
+ $this->fetcher->expects($this->once())
+ ->method('fetch')
+ ->with($this->equalTo($url))
+ ->will($this->throwException($ex));
+ $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
+ $this->businessLayer->create($url, 1, $this->user);
+ }
+
+ public function testCreate(){
+ $url = 'test';
+ $folderId = 10;
+ $createdFeed = new Feed();
+ $ex = new DoesNotExistException('yo');
+ $createdFeed->setUrl($url);
+ $return = array(
+ $createdFeed,
+ array(new Item(), new Item())
+ );
+
+ $this->mapper->expects($this->once())
+ ->method('findByUrlHash')
+ ->with($this->equalTo(md5($url)), $this->equalTo($this->user))
+ ->will($this->throwException($ex));
+ $this->fetcher->expects($this->once())
+ ->method('fetch')
+ ->with($this->equalTo($url))
+ ->will($this->returnValue($return));
+ $this->mapper->expects($this->once())
+ ->method('insert')
+ ->with($this->equalTo($createdFeed))
+ ->will($this->returnValue($createdFeed));
+ $this->itemMapper->expects($this->at(0))
+ ->method('insert')
+ ->with($this->equalTo($return[1][1]));
+ $this->itemMapper->expects($this->at(1))
+ ->method('insert')
+ ->with($this->equalTo($return[1][0]));
+
+ $feed = $this->businessLayer->create($url, $folderId, $this->user);
+
+ $this->assertEquals($feed->getFolderId(), $folderId);
+ $this->assertEquals($feed->getUrl(), $url);
+ }
+
+ public function testCreateFeedExistsAlready(){
+ $url = 'test';
+ $trans = $this->getMock('Trans', array('t'));
+ $trans->expects($this->once())
+ ->method('t');
+ $this->api->expects($this->once())
+ ->method('getTrans')
+ ->will($this->returnValue($trans));
+ $this->mapper->expects($this->once())
+ ->method('findByUrlHash')
+ ->with($this->equalTo(md5($url)), $this->equalTo($this->user));
+ $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
+ $this->businessLayer->create($url, 1, $this->user);
+ }
+
+
+ public function testUpdateCreatesNewEntry(){
+ $feed = new Feed();
+ $feed->setId(3);
+ $feed->getUrl('test');
+
+ $item = new Item();
+ $item->setGuidHash(md5('hi'));
+ $item->setFeedId(3);
+ $items = array(
+ $item
+ );
+
+ $ex = new DoesNotExistException('hi');
+
+ $fetchReturn = array($feed, $items);
+
+ $this->mapper->expects($this->at(0))
+ ->method('find')
+ ->with($this->equalTo($feed->getId()),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($feed));
+ $this->fetcher->expects($this->once())
+ ->method('fetch')
+ ->will($this->returnValue($fetchReturn));
+ $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->itemMapper->expects($this->once())
+ ->method('insert')
+ ->with($this->equalTo($items[0]));
+
+ $this->mapper->expects($this->at(1))
+ ->method('find')
+ ->with($feed->getId(), $this->user)
+ ->will($this->returnValue($feed));
+
+ $return = $this->businessLayer->update($feed->getId(), $this->user);
+
+ $this->assertEquals($return, $feed);
+ }
+
+
+ public function testUpdateUpdatesEntryNotWhenPubDateSame(){
+ $feed = new Feed();
+ $feed->setId(3);
+ $feed->getUrl('test');
+
+ $item = new Item();
+ $item->setGuidHash(md5('hi'));
+ $item->setPubDate(3333);
+ $items = array(
+ $item
+ );
+
+ $fetchReturn = array($feed, $items);
+
+ $this->mapper->expects($this->at(0))
+ ->method('find')
+ ->with($this->equalTo($feed->getId()),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($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($item));
+ $this->itemMapper->expects($this->never())
+ ->method('insert');
+ $this->itemMapper->expects($this->never())
+ ->method('delete');
+
+ $this->mapper->expects($this->at(1))
+ ->method('find')
+ ->with($feed->getId(), $this->user)
+ ->will($this->returnValue($feed));
+
+ $return = $this->businessLayer->update($feed->getId(), $this->user);
+
+ $this->assertEquals($return, $feed);
+ }
+
+
+
+ public function testUpdateUpdatesEntry(){
+ $feed = new Feed();
+ $feed->setId(3);
+ $feed->getUrl('test');
+
+ $item = new Item();
+ $item->setGuidHash(md5('hi'));
+ $item->setPubDate(3333);
+ $items = array(
+ $item
+ );
+
+ $item2 = new Item();
+ $item2->setPubDate(111);
+
+ $fetchReturn = array($feed, $items);
+
+ $this->mapper->expects($this->at(0))
+ ->method('find')
+ ->with($this->equalTo($feed->getId()),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($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->itemMapper->expects($this->at(1))
+ ->method('delete')
+ ->with($this->equalTo($item2));
+ $this->itemMapper->expects($this->at(2))
+ ->method('insert')
+ ->with($this->equalTo($item));
+
+ $this->mapper->expects($this->at(1))
+ ->method('find')
+ ->with($feed->getId(), $this->user)
+ ->will($this->returnValue($feed));
+
+ $return = $this->businessLayer->update($feed->getId(), $this->user);
+
+ $this->assertEquals($return, $feed);
+ $this->assertTrue($item->isUnread());
+ }
+
+
+ public function testCreateUpdateFails(){
+ $feed = new Feed();
+ $feed->setId(3);
+ $feed->getUrl('test');
+ $ex = new FetcherException('');
+
+ $this->mapper->expects($this->at(0))
+ ->method('find')
+ ->with($this->equalTo($feed->getId()),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($feed));
+ $this->fetcher->expects($this->once())
+ ->method('fetch')
+ ->will($this->throwException($ex));
+ $this->api->expects($this->once())
+ ->method('log');
+
+ $this->mapper->expects($this->at(1))
+ ->method('find')
+ ->with($feed->getId(), $this->user)
+ ->will($this->returnValue($feed));
+
+ $return = $this->businessLayer->update($feed->getId(), $this->user);
+
+ $this->assertEquals($return, $feed);
+ }
+
+
+ public function testUpdateDoesNotFindEntry() {
+ $feed = new Feed();
+ $feed->setId(3);
+ $feed->getUrl('test');
+
+ $ex = new DoesNotExistException('');
+
+ $this->mapper->expects($this->at(0))
+ ->method('find')
+ ->with($this->equalTo($feed->getId()),
+ $this->equalTo($this->user))
+ ->will($this->throwException($ex));
+
+ $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
+ $return = $this->businessLayer->update($feed->getId(), $this->user);
+ }
+
+
+ public function testUpdateDoesNotFindUpdatedEntry() {
+ $feed = new Feed();
+ $feed->setId(3);
+ $feed->getUrl('test');
+
+ $item = new Item();
+ $item->setGuidHash(md5('hi'));
+ $item->setPubDate(3333);
+ $item->setId(4);
+ $items = array(
+ $item
+ );
+
+ $item2 = new Item();
+ $item2->setPubDate(111);
+
+ $fetchReturn = array($feed, $items);
+ $ex = new DoesNotExistException('');
+
+ $this->mapper->expects($this->at(0))
+ ->method('find')
+ ->with($this->equalTo($feed->getId()),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($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->mapper->expects($this->at(1))
+ ->method('find')
+ ->with($this->equalTo($feed->getId()),
+ $this->equalTo($this->user))
+ ->will($this->throwException($ex));
+
+ $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
+ $return = $this->businessLayer->update($feed->getId(), $this->user);
+ }
+
+
+ public function testMove(){
+ $feedId = 3;
+ $folderId = 4;
+ $feed = new Feed();
+ $feed->setFolderId(16);
+ $feed->setId($feedId);
+
+ $this->mapper->expects($this->once())
+ ->method('find')
+ ->with($this->equalTo($feedId), $this->equalTo($this->user))
+ ->will($this->returnValue($feed));
+
+ $this->mapper->expects($this->once())
+ ->method('update')
+ ->with($this->equalTo($feed));
+
+ $this->businessLayer->move($feedId, $folderId, $this->user);
+
+ $this->assertEquals($folderId, $feed->getFolderId());
+ }
+
+
+
+}
diff --git a/tests/unit/businesslayer/FolderBusinessLayerTest.php b/tests/unit/businesslayer/FolderBusinessLayerTest.php
new file mode 100644
index 000000000..ad4e188e4
--- /dev/null
+++ b/tests/unit/businesslayer/FolderBusinessLayerTest.php
@@ -0,0 +1,164 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Cosentino
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+namespace OCA\News\BusinessLayer;
+
+require_once(__DIR__ . "/../../classloader.php");
+
+
+use \OCA\News\Db\Folder;
+
+
+class FolderBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
+
+ protected $api;
+ protected $folderMapper;
+ protected $folderBusinessLayer;
+
+ protected function setUp(){
+ $this->api = $this->getAPIMock();
+ $this->folderMapper = $this->getMockBuilder(
+ '\OCA\News\Db\FolderMapper')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->folderBusinessLayer = new FolderBusinessLayer($this->folderMapper, $this->api);
+ }
+
+
+ function testFindAll(){
+ $userId = 'jack';
+ $return = 'hi';
+ $this->folderMapper->expects($this->once())
+ ->method('findAllFromUser')
+ ->with($this->equalTo($userId))
+ ->will($this->returnValue($return));
+
+ $result = $this->folderBusinessLayer->findAll($userId);
+
+ $this->assertEquals($return, $result);
+ }
+
+
+ public function testCreate(){
+ $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));
+
+ $result = $this->folderBusinessLayer->create('hey', 'john', 5);
+
+ $this->assertEquals($folder, $result);
+ }
+
+
+ public function testCreateThrowsExWhenFolderNameExists(){
+ $folderName = 'hihi';
+ $rows = array(
+ array('id' => 1)
+ );
+
+ $trans = $this->getMock('Trans', array('t'));
+ $trans->expects($this->once())
+ ->method('t');
+ $this->api->expects($this->once())
+ ->method('getTrans')
+ ->will($this->returnValue($trans));
+ $this->folderMapper->expects($this->once())
+ ->method('findByName')
+ ->with($this->equalTo($folderName))
+ ->will($this->returnValue($rows));
+
+ $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
+ $result = $this->folderBusinessLayer->create($folderName, 'john', 3);
+ }
+
+
+ public function testOpen(){
+ $folder = new Folder();
+
+ $this->folderMapper->expects($this->once())
+ ->method('find')
+ ->with($this->equalTo(3))
+ ->will($this->returnValue($folder));
+
+ $this->folderMapper->expects($this->once())
+ ->method('update')
+ ->with($this->equalTo($folder));
+
+ $this->folderBusinessLayer->open(3, false, '');
+
+ $this->assertFalse($folder->getOpened());
+
+ }
+
+
+ public function testRename(){
+ $folder = new Folder();
+ $folder->setName('jooohn');
+
+ $this->folderMapper->expects($this->once())
+ ->method('find')
+ ->with($this->equalTo(3))
+ ->will($this->returnValue($folder));
+
+ $this->folderMapper->expects($this->once())
+ ->method('update')
+ ->with($this->equalTo($folder));
+
+ $this->folderBusinessLayer->rename(3, 'bogus', '');
+
+ $this->assertEquals('bogus', $folder->getName());
+ }
+
+
+ public function testRenameThrowsExWhenFolderNameExists(){
+ $folderName = 'hihi';
+ $rows = array(
+ array('id' => 1)
+ );
+
+ $trans = $this->getMock('Trans', array('t'));
+ $trans->expects($this->once())
+ ->method('t');
+ $this->api->expects($this->once())
+ ->method('getTrans')
+ ->will($this->returnValue($trans));
+ $this->folderMapper->expects($this->once())
+ ->method('findByName')
+ ->with($this->equalTo($folderName))
+ ->will($this->returnValue($rows));
+
+ $this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
+ $result = $this->folderBusinessLayer->rename(3, $folderName, 'john');
+ }
+
+
+}
diff --git a/tests/unit/businesslayer/ItemBusinessLayerTest.php b/tests/unit/businesslayer/ItemBusinessLayerTest.php
new file mode 100644
index 000000000..9bff8acfc
--- /dev/null
+++ b/tests/unit/businesslayer/ItemBusinessLayerTest.php
@@ -0,0 +1,276 @@
+<?php
+
+/**
+* ownCloud - News
+*
+* @author Alessandro Cosentino
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+namespace OCA\News\BusinessLayer;
+
+require_once(__DIR__ . "/../../classloader.php");
+
+
+use \OCA\News\Db\Item;
+use \OCA\News\Db\StatusFlag;
+use \OCA\News\Db\FeedType;
+
+
+class ItemBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
+
+ private $api;
+ private $mapper;
+ private $itemBusinessLayer;
+ private $user;
+ private $response;
+ private $status;
+
+
+ protected function setUp(){
+ $this->api = $this->getAPIMock();
+ $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $statusFlag = $this->getMockBuilder('\OCA\News\Db\StatusFlag')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->status = StatusFlag::STARRED;
+ $statusFlag->expects($this->any())
+ ->method('typeToStatus')
+ ->will($this->returnValue($this->status));
+ $this->threshold = 2;
+ $this->itemBusinessLayer = new ItemBusinessLayer($this->mapper, $statusFlag, $this->threshold);
+ $this->user = 'jack';
+ $response = 'hi';
+ $this->id = 3;
+ $this->updatedSince = 20333;
+ $this->showAll = true;
+ $this->offset = 5;
+ $this->limit = 20;
+ }
+
+
+ public function testFindAllNewFeed(){
+ $type = FeedType::FEED;
+ $this->mapper->expects($this->once())
+ ->method('findAllNewFeed')
+ ->with($this->equalTo($this->id),
+ $this->equalTo($this->updatedSince),
+ $this->equalTo($this->status),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($this->response));
+
+ $result = $this->itemBusinessLayer->findAllNew(
+ $this->id, $type, $this->updatedSince, $this->showAll,
+ $this->user);
+ $this->assertEquals($this->response, $result);
+ }
+
+
+ public function testFindAllNewFolder(){
+ $type = FeedType::FOLDER;
+ $this->mapper->expects($this->once())
+ ->method('findAllNewFolder')
+ ->with($this->equalTo($this->id),
+ $this->equalTo($this->updatedSince),
+ $this->equalTo($this->status),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($this->response));
+
+ $result = $this->itemBusinessLayer->findAllNew(
+ $this->id, $type, $this->updatedSince, $this->showAll,
+ $this->user);
+ $this->assertEquals($this->response, $result);
+ }
+
+
+ public function testFindAllNew(){
+ $type = FeedType::STARRED;
+ $this->mapper->expects($this->once())
+ ->method('findAllNew')
+ ->with( $this->equalTo($this->updatedSince),
+ $this->equalTo($this->status),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($this->response));
+
+ $result = $this->itemBusinessLayer->findAllNew(
+ $this->id, $type, $this->updatedSince, $this->showAll,
+ $this->user);
+ $this->assertEquals($this->response, $result);
+ }
+
+
+ public function testFindAllFeed(){
+ $type = FeedType::FEED;
+ $this->mapper->expects($this->once())
+ ->method('findAllFeed')
+ ->with($this->equalTo($this->id),
+ $this->equalTo($this->limit),
+ $this->equalTo($this->offset),
+ $this->equalTo($this->status),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($this->response));
+
+ $result = $this->itemBusinessLayer->findAll(
+ $this->id, $type, $this->limit,
+ $this->offset, $this->showAll,
+ $this->user);
+ $this->assertEquals($this->response, $result);
+ }
+
+
+ public function testFindAllFolder(){
+ $type = FeedType::FOLDER;
+ $this->mapper->expects($this->once())
+ ->method('findAllFolder')
+ ->with($this->equalTo($this->id),
+ $this->equalTo($this->limit),
+ $this->equalTo($this->offset),
+ $this->equalTo($this->status),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($this->response));
+
+ $result = $this->itemBusinessLayer->findAll(
+ $this->id, $type, $this->limit,
+ $this->offset, $this->showAll,
+ $this->user);
+ $this->assertEquals($this->response, $result);
+ }
+
+
+ public function testFindAll(){
+ $type = FeedType::STARRED;
+ $this->mapper->expects($this->once())
+ ->method('findAll')
+ ->with( $this->equalTo($this->limit),
+ $this->equalTo($this->offset),
+ $this->equalTo($this->status),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($this->response));
+
+ $result = $this->itemBusinessLayer->findAll(
+ $this->id, $type, $this->limit,
+ $this->offset, $this->showAll,
+ $this->user);
+ $this->assertEquals($this->response, $result);
+ }
+
+
+ public function testStarredCount(){
+ $star = 18;
+
+ $this->mapper->expects($this->once())
+ ->method('starredCount')
+ ->with($this->equalTo($this->user))
+ ->will($this->returnValue($star));
+
+ $result = $this->itemBusinessLayer->starredCount($this->user);
+
+ $this->assertEquals($star, $result);
+ }
+
+
+ public function testStar(){
+ $feedId = 3;
+ $guidHash = md5('hihi');
+
+ $item = new Item();
+ $item->setStatus(128);
+ $item->setId($feedId);
+
+ $this->mapper->expects($this->once())
+ ->method('findByGuidHash')
+ ->with(
+ $this->equalTo($guidHash),
+ $this->equalTo($feedId),
+ $this->equalTo($this->user))
+ ->will($this->returnValue($item));
+
+ $this->mapper->expects($this->once())
+ ->method('update')
+ ->with($this->equalTo($item));
+
+ $this->itemBusinessLayer->star($feedId, $guidHash, false, $this->user);
+
+ $this->assertTrue($item->isUnstarred());
+ }
+
+
+ public function testRead(){
+ $itemId = 3;
+ $item = new Item();
+ $item->setStatus(128);
+ $item->setId($itemId);
+
+ $this->mapper->expects($this->once())
+ ->method('find')
+ ->with($this->equalTo($itemId), $this->equalTo($this->user))
+ ->will($this->returnValue($item));
+
+ $this->mapper->expects($this->once())
+ ->method('update')
+ ->with($this->equalTo($item));
+
+ $this->itemBusinessLayer->read($itemId, false, $this->user);
+
+ $this->assertTrue($item->isUnread());
+ }
+
+
+ public function testReadFeed(){
+ $feedId = 3;
+ $highestItemId = 6;
+
+