From 3455fe9cb792e584671f465ff6d51732fc1802ff Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sat, 23 Mar 2013 13:24:35 +0100 Subject: finished findall methods in itembl --- README.rst | 2 +- bl/itembl.php | 43 +++++++++++- db/itemmapper.php | 29 ++++++++ db/statusflag.php | 22 +++++++ dependencyinjection/dicontainer.php | 7 +- tests/bl/ItemBlTest.php | 127 +++++++++++++++++++++++++++++++++--- tests/bl/StatusFlagTest.php | 74 +++++++++++++++++++++ 7 files changed, 289 insertions(+), 15 deletions(-) create mode 100644 tests/bl/StatusFlagTest.php diff --git a/README.rst b/README.rst index 39367b96e..56dcaf6e2 100644 --- a/README.rst +++ b/README.rst @@ -9,7 +9,7 @@ TODO * Referential integrity (delete items and feeds when feed or folder with FK was deleted) * Port coffeescript * make export work -> generate xml with template -* implement findAll methods in itemBl and itemMapper +* implement findAll methods in itemMapper * make feed update work * fix search plugin * fix background job \ No newline at end of file diff --git a/bl/itembl.php b/bl/itembl.php index 6f3e5d002..c45ee3a91 100644 --- a/bl/itembl.php +++ b/bl/itembl.php @@ -27,24 +27,61 @@ namespace OCA\News\Bl; use \OCA\News\Db\Item; use \OCA\News\Db\ItemMapper; +use \OCA\News\Db\StatusFlag; +use \OCA\News\Db\FeedType; class ItemBl extends Bl { - public function __construct(ItemMapper $itemMapper){ + private $statusFlag; + + public function __construct(ItemMapper $itemMapper, StatusFlag $statusFlag){ parent::__construct($itemMapper); + $this->statusFlag = $statusFlag; } public function findAllNew($id, $type, $updatedSince, $showAll, $userId){ - // TODO all the crazy finding of items + $status = $this->statusFlag->typeToStatus($type, $showAll); + + switch($type){ + case FeedType::FEED: + $items = $this->mapper->findAllNewFeed($id, $updatedSince, + $status, $userId); + break; + case FeedType::FOLDER: + $items = $this->mapper->findAllNewFolder($id, $updatedSince, + $status, $userId); + break; + default: + $items = $this->mapper->findAllNew($updatedSince, $status, + $userId); + } + + return $items; } public function findAll($id, $type, $limit, $offset, $showAll, $userId){ - // TODO all the crazy finding of items + $status = $this->statusFlag->typeToStatus($type, $showAll); + + switch($type){ + case FeedType::FEED: + $items = $this->mapper->findAllFeed($id, $limit, $offset, + $status, $userId); + break; + case FeedType::FOLDER: + $items = $this->mapper->findAllFolder($id, $limit, $offset, + $status, $userId); + break; + default: + $items = $this->mapper->findAll($limit, $offset, $status, + $userId); + } + + return $items; } diff --git a/db/itemmapper.php b/db/itemmapper.php index 7d0279264..77621be2a 100644 --- a/db/itemmapper.php +++ b/db/itemmapper.php @@ -104,6 +104,35 @@ class ItemMapper extends Mapper implements IMapper { } + public function findAllNewFeed($id, $updatedSince, $status, $userId){ + // TODO + } + + + public function findAllNewFolder($id, $updatedSince, $status, $userId){ + // TODO + } + + + public function findAllNew($updatedSince, $status, $userId){ + // TODO + } + + + public function findAllFeed($id, $limit, $offset, $status, $userId){ + // TODO + } + + + public function findAllFolder($id, $limit, $offset, $status, $userId){ + // TODO + } + + + public function findAll($limit, $offset, $status, $userId){ + // TODO + } + } /** * Queries to find all items from a folder that belongs to a user diff --git a/db/statusflag.php b/db/statusflag.php index eccf1e199..96d64915b 100644 --- a/db/statusflag.php +++ b/db/statusflag.php @@ -30,4 +30,26 @@ class StatusFlag { const STARRED = 0x04; const DELETED = 0x08; const UPDATED = 0x16; + + + /** + * Get status for query + */ + public function typeToStatus($type, $showAll){ + if($type === FeedType::STARRED){ + $status = self::STARRED; + } else { + $status = 0; + } + + if($showAll){ + $status |= self::UNREAD; + } else { + $status &= ~self::UNREAD; + } + + return $status; + } + + } \ No newline at end of file diff --git a/dependencyinjection/dicontainer.php b/dependencyinjection/dicontainer.php index 970900ea5..319517ba1 100644 --- a/dependencyinjection/dicontainer.php +++ b/dependencyinjection/dicontainer.php @@ -41,6 +41,7 @@ use OCA\News\Bl\ItemBl; use OCA\News\Db\FolderMapper; use OCA\News\Db\FeedMapper; use OCA\News\Db\ItemMapper; +use OCA\News\Db\StatusFlag; require_once __DIR__ . '/../3rdparty/SimplePie/autoloader.php'; @@ -99,7 +100,7 @@ class DIContainer extends BaseContainer { }); $this['ItemBl'] = $this->share(function($c){ - return new ItemBl($c['ItemMapper']); + return new ItemBl($c['ItemMapper'], $c['StatusFlag']); }); @@ -126,6 +127,10 @@ class DIContainer extends BaseContainer { return new FeedFetcher(); }); + $this['StatusFlag'] = $this->share(function($c){ + return new StatusFlag(); + }); + } } diff --git a/tests/bl/ItemBlTest.php b/tests/bl/ItemBlTest.php index 4b4c71219..e8c4cc9d7 100644 --- a/tests/bl/ItemBlTest.php +++ b/tests/bl/ItemBlTest.php @@ -29,41 +29,148 @@ require_once(__DIR__ . "/../classloader.php"); use \OCA\News\Db\Item; +use \OCA\News\Db\StatusFlag; +use \OCA\News\Db\FeedType; class ItemBlTest extends \OCA\AppFramework\Utility\TestUtility { - protected $api; - protected $mapper; - protected $bl; - protected $user; - protected $response; + private $api; + private $mapper; + private $bl; + private $user; + private $response; + private $status; + protected function setUp(){ $this->api = $this->getAPIMock(); $this->mapper = $this->getMockBuilder('\OCA\News\Db\ItemMapper') ->disableOriginalConstructor() ->getMock(); - $this->bl = new ItemBl($this->mapper); + $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->bl = new ItemBl($this->mapper, $statusFlag); $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->bl->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->bl->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->bl->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->bl->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->bl->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->user)) + ->with( $this->equalTo($this->limit), + $this->equalTo($this->offset), + $this->equalTo($this->status), + $this->equalTo($this->user)) ->will($this->returnValue($this->response)); - $result = $this->bl->findAllFromUser($this->user); + $result = $this->bl->findAll( + $this->id, $type, $this->limit, + $this->offset, $this->showAll, + $this->user); $this->assertEquals($this->response, $result); } - */ public function testStarredCount(){ $star = 18; diff --git a/tests/bl/StatusFlagTest.php b/tests/bl/StatusFlagTest.php new file mode 100644 index 000000000..9e9be9fca --- /dev/null +++ b/tests/bl/StatusFlagTest.php @@ -0,0 +1,74 @@ +. +* +*/ + +namespace OCA\News\Db; + +use \OCA\AppFramework\Utility\TestUtility; + + +require_once(__DIR__ . "/../classloader.php"); + + +class StatusFlagTest extends TestUtility { + + private $statusFlag; + + protected function setUp(){ + $this->statusFlag = new StatusFlag(); + } + + + public function testTypeToStatusUnreadStarred(){ + $expected = StatusFlag::UNREAD | StatusFlag::STARRED; + $status = $this->statusFlag->typeToStatus(FeedType::STARRED, true); + + $this->assertEquals($expected, $status); + } + + + public function testTypeToStatusUnread(){ + $expected = StatusFlag::UNREAD; + $status = $this->statusFlag->typeToStatus(FeedType::FEED, true); + + $this->assertEquals($expected, $status); + } + + + public function testTypeToStatusReadStarred(){ + $expected = (~StatusFlag::UNREAD) & StatusFlag::STARRED; + $status = $this->statusFlag->typeToStatus(FeedType::STARRED, false); + + $this->assertEquals($expected, $status); + } + + + public function testTypeToStatusRead(){ + $expected = (~StatusFlag::UNREAD) & 0; + $status = $this->statusFlag->typeToStatus(FeedType::FEED, false); + + $this->assertEquals($expected, $status); + } + +} \ No newline at end of file -- cgit v1.2.3