diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2013-03-19 19:20:42 +0100 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2013-03-19 19:20:42 +0100 |
commit | 4e077697a1fd0cf15a6eac22cb6e3d6ec4b10ff2 (patch) | |
tree | fa6ad4cab5d6c9a3ac2686171d00341d57729a89 | |
parent | 3cacc9c90d40f385786482faa51c308afdf54cec (diff) |
added itemmapper test
-rw-r--r-- | db/item.php | 19 | ||||
-rw-r--r-- | db/itemmapper.php | 65 | ||||
-rw-r--r-- | tests/db/ItemMapperTest.php | 92 |
3 files changed, 150 insertions, 26 deletions
diff --git a/db/item.php b/db/item.php index bda17204e..7d07b337b 100644 --- a/db/item.php +++ b/db/item.php @@ -10,14 +10,20 @@ * */ -namespace OCA\News; +namespace OCA\News\Db; + +class Item extends Entity { + + +} /** * This class models an item. * * It encapsulate a SimplePie_Item object and adds a status flag to it */ -class Item { + +/*class Item { private $url; private $title; @@ -92,10 +98,6 @@ class Item { return ($this->status & StatusFlag::IMPORTANT); } - /** - * NOTE: this is needed to store items in the database, otherwise - * the status of an item should be retrieved with methods: isRead(), isImportant(), ... - */ public function getStatus() { return $this->status; } @@ -104,10 +106,6 @@ class Item { $this->status = $status; } - /* change the following method with set/get magic methods - * http://www.php.net/manual/en/language.oop5.overloading.php#object.get - */ - public function getTitle() { return $this->title; } @@ -166,3 +164,4 @@ class Item { } } +*/
\ No newline at end of file diff --git a/db/itemmapper.php b/db/itemmapper.php index 3791c3a49..7dca01dcc 100644 --- a/db/itemmapper.php +++ b/db/itemmapper.php @@ -10,12 +10,44 @@ * */ -namespace OCA\News; +namespace OCA\News\Db; + +use \OCA\AppFramework\Db\Mapper; +use \OCA\AppFramework\Core\API; + +class ItemMapper extends Mapper { + + public function __construct(API $api){ + parent::__construct($api); + } + + + public function findAllFromFeed($feedId, $userId){ + $sql = 'SELECT * FROM `*PREFIX*news_items` + WHERE user_id = ? + AND feed_id = ?'; + + $result = $this->execute($sql, array($feedId, $userId)); + $items = array(); + + foreach($result->fetchRow() as $row){ + $item = new Item(); + $item->fromRow($row); + + array_push($items, $item); + } + + return $items; + } + +} + /** * This class maps an item to a row of the items table in the database. * It follows the Data Mapper pattern (see http://martinfowler.com/eaaCatalog/dataMapper.html). */ +/* class ItemMapper { const tableName = '*PREFIX*news_items'; @@ -30,11 +62,11 @@ class ItemMapper { } } - /** + * @brief * @param row a row from the items table of the database * @returns an object of the class OC_News_Item - */ + * public function fromRow($row) { $url = $row['url']; $title = $row['title']; @@ -65,7 +97,7 @@ class ItemMapper { /** * @brief Retrieve all the item corresponding to a feed from the database * @param feedid The id of the feed in the database table. - */ + * public function findByFeedId($feedid) { $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE feed_id = ? ORDER BY pub_date DESC'); $result = $stmt->execute(array($feedid)); @@ -84,7 +116,7 @@ class ItemMapper { * @brief Retrieve all the items corresponding to a feed from the database with a particular status * @param feedid The id of the feed in the database table. * @param status one of the constants defined in OCA\News\StatusFlag - */ + * public function findAllStatus($feedid, $status) { $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE feed_id = ? @@ -104,7 +136,7 @@ class ItemMapper { /* * @brief Retrieve all the items from the database with a particular status * @param status one of the constants defined in OCA\News\StatusFlag - */ + * public function findEveryItemByStatus($status) { $stmt = \OCP\DB::prepare('SELECT ' . self::tableName . '.* FROM ' . self::tableName . ' JOIN '. FeedMapper::tableName .' ON @@ -134,7 +166,7 @@ class ItemMapper { /** * @brief Count all the items from the database with a particular status * @param status one of the constants defined in OCA\News\StatusFlag - */ + * public function countEveryItemByStatus($status) { $stmt = \OCP\DB::prepare('SELECT COUNT(*) as size FROM ' . self::tableName . ' JOIN '. FeedMapper::tableName .' ON @@ -168,7 +200,7 @@ class ItemMapper { * @param int $feedId: the id of the feed * @param int $mostRecentItemId: every item with the same or lower id will * be marked read - */ + * public function markAllRead($feedId, $mostRecentItemId){ if($mostRecentItemId === 0){ $stmt = \OCP\DB::prepare(' @@ -204,7 +236,7 @@ class ItemMapper { /** * @brief Update the item after its status has changed * @returns The item whose status has changed. - */ + * public function update(Item $item) { $itemid = $item->getId(); @@ -230,7 +262,7 @@ class ItemMapper { /** * @brief Save the feed and all its items into the database * @returns The id of the feed in the database table. - */ + * public function save(Item $item, $feedid) { $guid = $item->getGuid(); $guid_hash = md5($guid); @@ -297,7 +329,7 @@ class ItemMapper { /** * @brief Retrieve an item from the database * @param id The id of the item in the database table. - */ + * public function findById($id) { $stmt = \OCP\DB::prepare('SELECT ' . self::tableName . '.id AS id, ' . self::tableName . @@ -310,7 +342,7 @@ class ItemMapper { /* $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?'); $result = $stmt->execute(array($id)); - */ + * $row = $result->fetchRow(); $item = $this->fromRow($row); @@ -324,7 +356,7 @@ class ItemMapper { * @brief Permanently delete all items belonging to a feed from the database * @param feedid The id of the feed that we wish to delete * @return - */ + * public function deleteAll($feedid) { if ($feedid == null) { return false; @@ -341,7 +373,7 @@ class ItemMapper { * @param $feedType the type of the feed * @param $feedId the id of the feed or folder * @return the unread count - */ + * public function getUnreadCount($feedType, $feedId){ $unreadCount = 0; @@ -377,7 +409,7 @@ class ItemMapper { * @param $feedId the id of the feed or folder * @param $showAll if true, it will also include unread items * @return an array with all items - */ + * public function getItems($feedType, $feedId, $showAll){ $items = array(); @@ -419,4 +451,5 @@ class ItemMapper { } return $items; } -}
\ No newline at end of file +} +*/
\ No newline at end of file diff --git a/tests/db/ItemMapperTest.php b/tests/db/ItemMapperTest.php new file mode 100644 index 000000000..d5068d1f8 --- /dev/null +++ b/tests/db/ItemMapperTest.php @@ -0,0 +1,92 @@ +<?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\Db; + +require_once(__DIR__ . "/../classloader.php"); + + +class Test extends \PHPUnit_Framework_TestCase { + + private $itemMapper; + private $api; + private $items; + + protected function setUp(){ + $this->api = $this->getMock('\OCA\AppFramework\Core\API', + array('prepareQuery'), + array('a')); + $this->itemMapper = new ItemMapper($this->api); + + // create mock items + $item1 = new Item(); + $item1->test = 1; + + $item2 = new Item(); + $item2->test = 2; + + $this->items = array( + $item1, + $item2 + ); + } + + + public function testFindAllFromFeed(){ + $userId = 'john'; + $feedId = 3; + $rows = array( + array('test' => 1), + array('test' => 2) + ); + $sql = 'SELECT * FROM `*PREFIX*news_items` + WHERE user_id = ? + AND feed_id = ?'; + + $pdoResult = $this->getMock('Result', + array('fetchRow')); + $pdoResult->expects($this->once()) + ->method('fetchRow') + ->will($this->returnValue($rows)); + + $query = $this->getMock('Query', + array('execute')); + $query->expects($this->once()) + ->method('execute') + ->with($this->equalTo(array($feedId, $userId))) + ->will($this->returnValue($pdoResult)); + + $this->api->expects($this->once()) + ->method('prepareQuery') + ->with($this->equalTo($sql)) + ->will(($this->returnValue($query))); + + $result = $this->itemMapper->findAllFromFeed($feedId, $userId); + + $this->assertEquals($this->items, $result); + + } + +}
\ No newline at end of file |