summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--db/itemmapper.php30
-rw-r--r--tests/db/ItemMapperTest.php53
2 files changed, 75 insertions, 8 deletions
diff --git a/db/itemmapper.php b/db/itemmapper.php
index e79cabb7c..c6ea67209 100644
--- a/db/itemmapper.php
+++ b/db/itemmapper.php
@@ -12,16 +12,17 @@
namespace OCA\News\Db;
+use \OCA\AppFramework\Db\DoesNotExistException;
+use \OCA\AppFramework\Db\MultipleObjectsReturnedException;
use \OCA\AppFramework\Db\Mapper;
use \OCA\AppFramework\Core\API;
class ItemMapper extends Mapper {
public function __construct(API $api){
- parent::__construct($api);
+ parent::__construct($api, 'news_items');
}
-
public function findAllFromFeed($feedId, $userId){
$sql = 'SELECT * FROM `*PREFIX*news_items` ' .
'WHERE user_id = ? ' .
@@ -30,7 +31,7 @@ class ItemMapper extends Mapper {
$result = $this->execute($sql, array($feedId, $userId));
$items = array();
- foreach($result->fetchRow() as $row){
+ while($row = $result->fetchRow()){
$item = new Item();
$item->fromRow($row);
@@ -39,6 +40,29 @@ class ItemMapper extends Mapper {
return $items;
}
+
+ public function find($id, $userId){
+ $sql = 'SELECT * FROM `*PREFIX*news_items` ' .
+ 'WHERE user_id = ? ' .
+ 'AND id = ?';
+
+ $result = $this->execute($sql, array($id, $userId));
+
+ $row = $result->fetchRow();
+ if ($row === false) {
+ throw new DoesNotExistException('Item ' . $id .
+ ' from user ' . $userId . ' not found');
+ } elseif($result->fetchRow() !== false) {
+ throw new MultipleObjectsReturnedException('More than one result for Item with id ' . $id . ' from user ' . $userId . '!');
+ }
+
+ $item = new Item();
+ $item->fromRow($row);
+
+ return $item;
+ }
+
+
}
diff --git a/tests/db/ItemMapperTest.php b/tests/db/ItemMapperTest.php
index 40ff691b6..675af7ba2 100644
--- a/tests/db/ItemMapperTest.php
+++ b/tests/db/ItemMapperTest.php
@@ -40,10 +40,7 @@ class Test extends \OCA\AppFramework\Utility\MapperTestUtility {
// create mock items
$item1 = new Item();
- $item1->test = 1;
-
$item2 = new Item();
- $item2->test = 2;
$this->items = array(
$item1,
@@ -56,8 +53,8 @@ class Test extends \OCA\AppFramework\Utility\MapperTestUtility {
$userId = 'john';
$feedId = 3;
$rows = array(
- array('test' => 1),
- array('test' => 2)
+ array('id' => $this->items[0]->getId()),
+ array('id' => $this->items[1]->getId())
);
$sql = 'SELECT * FROM `*PREFIX*news_items` ' .
'WHERE user_id = ? ' .
@@ -71,4 +68,50 @@ class Test extends \OCA\AppFramework\Utility\MapperTestUtility {
}
+ public function testFind(){
+ $userId = 'john';
+ $id = 3;
+ $rows = array(
+ array('id' => $this->items[0]->getId()),
+ );
+ $sql = 'SELECT * FROM `*PREFIX*news_items` ' .
+ 'WHERE user_id = ? ' .
+ 'AND id = ?';
+
+ $this->setMapperResult($sql, array($id, $userId), $rows);
+
+ $result = $this->itemMapper->find($id, $userId);
+ $this->assertEquals($this->items[0], $result);
+
+ }
+
+ public function testFindNotFound(){
+ $userId = 'john';
+ $id = 3;
+ $sql = 'SELECT * FROM `*PREFIX*news_items` ' .
+ 'WHERE user_id = ? ' .
+ 'AND id = ?';
+
+ $this->setMapperResult($sql, array($id, $userId));
+
+ $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException');
+ $result = $this->itemMapper->find($id, $userId);
+ }
+
+ public function testFindMoreThanOneResultFound(){
+ $userId = 'john';
+ $id = 3;
+ $rows = array(
+ array('id' => $this->items[0]->getId()),
+ array('id' => $this->items[1]->getId())
+ );
+ $sql = 'SELECT * FROM `*PREFIX*news_items` ' .
+ 'WHERE user_id = ? ' .
+ 'AND id = ?';
+
+ $this->setMapperResult($sql, array($id, $userId), $rows);
+
+ $this->setExpectedException('\OCA\AppFramework\Db\MultipleObjectsReturnedException');
+ $result = $this->itemMapper->find($id, $userId);
+ }
} \ No newline at end of file