summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-03-22 13:47:45 +0100
committerBernhard Posselt <nukeawhale@gmail.com>2013-03-22 13:47:45 +0100
commit585667bdf4a06a7bcde1b7d2b9b42059135343dc (patch)
treef7b9b053e70a088280c83073d2fe72c9387bcefc
parent196177a0ca3fc0103d359eec44f46173be685413 (diff)
dont add feed twice
-rw-r--r--bl/feedbl.php6
-rw-r--r--db/feedmapper.php18
-rw-r--r--db/itemmapper.php27
-rw-r--r--tests/bl/FeedBlTest.php23
-rw-r--r--tests/db/FeedMapperTest.php63
-rw-r--r--tests/db/ItemMapperTest.php54
6 files changed, 126 insertions, 65 deletions
diff --git a/bl/feedbl.php b/bl/feedbl.php
index 53a2e6061..8df399788 100644
--- a/bl/feedbl.php
+++ b/bl/feedbl.php
@@ -25,6 +25,8 @@
namespace OCA\News\Bl;
+use \OCA\AppFramework\Db\DoesNotExistException;
+
use \OCA\News\Db\Feed;
use \OCA\News\Db\FeedMapper;
use \OCA\News\Utility\FeedFetcher;
@@ -56,6 +58,10 @@ class FeedBl extends Bl {
public function create($feedUrl, $folderId, $userId){
// first try if the feed exists already
+ try {
+ $this->mapper->findByUrlHash(md5($feedUrl), $userId);
+ throw new BLException('Can not add feed: Exists already');
+ } catch(DoesNotExistException $ex){}
try {
list($feed, $items) = $this->feedFetcher->fetch($feedUrl);
diff --git a/db/feedmapper.php b/db/feedmapper.php
index 0fc8d3114..131eb2090 100644
--- a/db/feedmapper.php
+++ b/db/feedmapper.php
@@ -84,16 +84,18 @@ class FeedMapper extends Mapper implements IMapper {
}
- public function getStarredCount($userId){
- $sql = 'SELECT COUNT(*) AS size FROM `*dbprefix*news_feeds` ' .
- 'AND `user_id` = ? ' .
- 'AND ((`status` & ?) > 0)';
- $params = array($userId, StatusFlag::STARRED);
+ public function findByUrlHash($hash, $userId){
+ $sql = 'SELECT * FROM `*dbprefix*news_feeds` ' .
+ 'WHERE `url_hash` = ? ' .
+ 'AND `user_id` = ?';
+ $params = array($hash, $userId);
- $result = $this->execute($sql, $params)->fetchRow();
+ $row = $this->findQuery($sql, $params);
+ $feed = new Feed();
+ $feed->fromRow($row);
- return $result['size'];
- }
+ return $feed;
+ }
} \ No newline at end of file
diff --git a/db/itemmapper.php b/db/itemmapper.php
index 2efa9b535..ee57e81e9 100644
--- a/db/itemmapper.php
+++ b/db/itemmapper.php
@@ -36,21 +36,6 @@ class ItemMapper extends Mapper implements IMapper {
}
- public function findByUrlHash($urlHash, $userId){
- $sql = 'SELECT `*dbprefix*news_items`.* FROM `*dbprefix*news_items` ' .
- 'JOIN `*dbprefix*news_feeds` ' .
- 'ON `*dbprefix*news_feeds`.`id` = `*dbprefix*news_items`.`feed_id` ' .
- 'WHERE `*dbprefix*news_items`.`url_hash` = ? ' .
- 'AND `*dbprefix*news_feeds`.`user_id` = ? ';
- $params = array($urlHash, $userId);
-
- $row = $this->findQuery($sql, $params);
- $item = new Item();
- $item->fromRow($row);
-
- return $item;
- }
-
protected function findAllRows($sql, $params, $limit=null, $offset=null) {
$result = $this->execute($sql, $params, $limit, $offset);
$items = array();
@@ -161,7 +146,17 @@ class ItemMapper extends Mapper implements IMapper {
public function starredCount($userId){
- // TODO
+ $sql = 'SELECT COUNT(*) AS size FROM `*dbprefix*news_feeds` `feeds` ' .
+ 'JOIN `*dbprefix*news_items` `items` ' .
+ 'ON `items`.`feed_id` = `feeds`.`id`' .
+ 'WHERE `feeds`.`user_id` = ? ' .
+ 'AND ((`items`.`status` & ?) > 0)';
+
+ $params = array($userId, StatusFlag::STARRED);
+
+ $result = $this->execute($sql, $params)->fetchRow();
+
+ return $result['size'];
}
}
diff --git a/tests/bl/FeedBlTest.php b/tests/bl/FeedBlTest.php
index bef45bd2e..3cf528722 100644
--- a/tests/bl/FeedBlTest.php
+++ b/tests/bl/FeedBlTest.php
@@ -27,6 +27,7 @@ namespace OCA\News\Bl;
require_once(__DIR__ . "/../classloader.php");
+use \OCA\AppFramework\Db\DoesNotExistException;
use \OCA\News\Db\Feed;
use \OCA\News\Db\Item;
@@ -84,23 +85,33 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility {
public function testCreateDoesNotFindFeed(){
$ex = new FetcherException('hi');
$url = 'test';
+ $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\Bl\BLException');
- $this->bl->create($url, 1, 2);
+ $this->bl->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))
@@ -122,6 +133,16 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility {
$this->assertEquals($feed->getUrl(), $url);
}
+ public function testCreateFeedExistsAlready(){
+ $url = 'test';
+ $this->mapper->expects($this->once())
+ ->method('findByUrlHash')
+ ->with($this->equalTo(md5($url)), $this->equalTo($this->user));
+ $this->setExpectedException('\OCA\News\Bl\BLException');
+ $this->bl->create($url, 1, $this->user);
+
+ }
+
public function testUpdate(){
// TODO
diff --git a/tests/db/FeedMapperTest.php b/tests/db/FeedMapperTest.php
index a070e5dd9..d1cbf5bff 100644
--- a/tests/db/FeedMapperTest.php
+++ b/tests/db/FeedMapperTest.php
@@ -30,13 +30,13 @@ require_once(__DIR__ . "/../classloader.php");
class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
- private $feedMapper;
+ private $mapper;
private $feeds;
protected function setUp(){
$this->beforeEach();
- $this->feedMapper = new FeedMapper($this->api);
+ $this->mapper = new FeedMapper($this->api);
// create mock feeds
$feed1 = new Feed();
@@ -46,6 +46,7 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$feed1,
$feed2
);
+ $this->user = 'herman';
}
@@ -61,7 +62,7 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$this->setMapperResult($sql, array($id, $userId), $rows);
- $result = $this->feedMapper->find($id, $userId);
+ $result = $this->mapper->find($id, $userId);
$this->assertEquals($this->feeds[0], $result);
}
@@ -77,7 +78,7 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$this->setMapperResult($sql, array($id, $userId));
$this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException');
- $result = $this->feedMapper->find($id, $userId);
+ $result = $this->mapper->find($id, $userId);
}
@@ -95,7 +96,7 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$this->setMapperResult($sql, array($id, $userId), $rows);
$this->setExpectedException('\OCA\AppFramework\Db\MultipleObjectsReturnedException');
- $result = $this->feedMapper->find($id, $userId);
+ $result = $this->mapper->find($id, $userId);
}
@@ -109,7 +110,7 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$this->setMapperResult($sql, array(), $rows);
- $result = $this->feedMapper->findAll();
+ $result = $this->mapper->findAll();
$this->assertEquals($this->feeds, $result);
}
@@ -130,24 +131,54 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$this->setMapperResult($sql, array($userId), $rows);
- $result = $this->feedMapper->findAllFromUser($userId);
+ $result = $this->mapper->findAllFromUser($userId);
$this->assertEquals($this->feeds, $result);
}
- public function testGetStarredCount(){
- $userId = 'john';
+ public function testFindByUrlHash(){
+ $urlHash = md5('hihi');
$row = array(
- array('size' => 9)
+ array('id' => $this->feeds[0]->getId()),
);
- $sql = 'SELECT COUNT(*) AS size FROM `*dbprefix*news_feeds` ' .
- 'AND `user_id` = ? ' .
- 'AND ((`status` & ?) > 0)';
+ $sql = 'SELECT * FROM `*dbprefix*news_feeds` ' .
+ 'WHERE `url_hash` = ? ' .
+ 'AND `user_id` = ?';
+ $this->setMapperResult($sql, array($urlHash, $this->user), $row);
- $this->setMapperResult($sql, array($userId, StatusFlag::STARRED), $row);
+ $result = $this->mapper->findByUrlHash($urlHash, $this->user);
+ $this->assertEquals($this->feeds[0], $result);
+ }
+
+
+ public function testFindByUrlHashNotFound(){
+ $urlHash = md5('hihi');
+ $sql = 'SELECT * FROM `*dbprefix*news_feeds` ' .
+ 'WHERE `url_hash` = ? ' .
+ 'AND `user_id` = ?';
+
+ $this->setMapperResult($sql, array($urlHash, $this->user));
- $result = $this->feedMapper->getStarredCount($userId);
- $this->assertEquals($row[0]['size'], $result);
+ $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException');
+ $result = $this->mapper->findByUrlHash($urlHash, $this->user);
}
+
+
+ public function testFindByUrlHashMoreThanOneResultFound(){
+ $urlHash = md5('hihi');
+ $rows = array(
+ array('id' => $this->feeds[0]->getId()),
+ array('id' => $this->feeds[1]->getId())
+ );
+ $sql = 'SELECT * FROM `*dbprefix*news_feeds` ' .
+ 'WHERE `url_hash` = ? ' .
+ 'AND `user_id` = ?';
+
+ $this->setMapperResult($sql, array($urlHash, $this->user));
+
+ $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException');
+ $result = $this->mapper->findByUrlHash($urlHash, $this->user);
+ }
+
} \ No newline at end of file
diff --git a/tests/db/ItemMapperTest.php b/tests/db/ItemMapperTest.php
index fb37f9bc0..ea9b15815 100644
--- a/tests/db/ItemMapperTest.php
+++ b/tests/db/ItemMapperTest.php
@@ -30,14 +30,14 @@ require_once(__DIR__ . "/../classloader.php");
class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
- private $itemMapper;
+ private $mapper;
private $items;
public function setUp()
{
$this->beforeEach();
- $this->itemMapper = new ItemMapper($this->api);
+ $this->mapper = new ItemMapper($this->api);
// create mock items
$item1 = new Item();
@@ -72,25 +72,12 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$this->setMapperResult($sql, array($this->id, $this->userId), $this->row);
- $result = $this->itemMapper->find($this->id, $this->userId);
+ $result = $this->mapper->find($this->id, $this->userId);
$this->assertEquals($this->items[0], $result);
}
- public function testFindByUrlHash(){
- $urlHash = md5('hihi');
- $sql = 'SELECT `*dbprefix*news_items`.* FROM `*dbprefix*news_items` ' .
- 'JOIN `*dbprefix*news_feeds` ' .
- 'ON `*dbprefix*news_feeds`.`id` = `*dbprefix*news_items`.`feed_id` ' .
- 'WHERE `*dbprefix*news_items`.`url_hash` = ? ' .
- 'AND `*dbprefix*news_feeds`.`user_id` = ? ';
- $this->setMapperResult($sql, array($urlHash, $this->userId), $this->row);
-
- $result = $this->itemMapper->findByUrlHash($urlHash, $this->userId);
- $this->assertEquals($this->items[0], $result);
- }
-
//
// public function testFindNotFound(){
// $sql = 'SELECT `*dbprefix*news_items`.* FROM `*dbprefix*news_items` ' .
@@ -102,7 +89,7 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
// $this->setMapperResult($sql, array($id, $userId));
//
// $this->setExpectedException('\OCA\AppFramework\Db\DoesNotExistException');
-// $result = $this->itemMapper->find($id, $userId);
+// $result = $this->mapper->find($id, $userId);
// }
//
// public function testFindMoreThanOneResultFound(){
@@ -120,7 +107,7 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
// $this->setMapperResult($sql, array($id, $userId), $rows);
//
// $this->setExpectedException('\OCA\AppFramework\Db\MultipleObjectsReturnedException');
-// $result = $this->itemMapper->find($id, $userId);
+// $result = $this->mapper->find($id, $userId);
// }
//
// public function testFindAllFromFeed(){
@@ -135,7 +122,7 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
// 'AND feed_id = ?';
//
// $this->setMapperResult($sql, array($feedId, $userId), $rows);
-// $result = $this->itemMapper->findAllFromFeed($feedId, $userId);
+// $result = $this->mapper->findAllFromFeed($feedId, $userId);
// $this->assertEquals($this->items, $result);
//
// }
@@ -154,7 +141,7 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
// 'AND ((`*dbprefix*news_items`.`status` & ?) > 0)';
//
// $this->setMapperResult($sql, array($feedId, $userId, $status), $rows);
-// $result = $this->itemMapper->findAllFromFeedByStatus($feedId, $userId, $status);
+// $result = $this->mapper->findAllFromFeedByStatus($feedId, $userId, $status);
// $this->assertEquals($this->items, $result);
//
// }
@@ -176,7 +163,7 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$params = array($this->userId, $this->folderId, $status);
$this->setMapperResult($sql, $params, $this->rows);
- $result = $this->itemMapper->findAllFromFolderByOffset($this->userId, $this->folderId, $status);
+ $result = $this->mapper->findAllFromFolderByOffset($this->userId, $this->folderId, $status);
$this->assertEquals($this->items, $result);
}
@@ -190,7 +177,7 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$params = array($this->userId, $this->folderId, $status);
$this->setMapperResult($sql, $params, $this->rows);
- $result = $this->itemMapper->findAllFromFolderByOffset($this->userId, $this->folderId, $status, $limit, $offset);
+ $result = $this->mapper->findAllFromFolderByOffset($this->userId, $this->folderId, $status, $limit, $offset);
$this->assertEquals($this->items, $result);
}
@@ -203,7 +190,7 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
$params = array($this->userId, $this->folderId, $status, $lastModified);
$this->setMapperResult($sql, $params, $this->rows);
- $result = $this->itemMapper->findAllFromFolderByLastMofified($this->userId, $this->folderId, $status, $lastModified);
+ $result = $this->mapper->findAllFromFolderByLastMofified($this->userId, $this->folderId, $status, $lastModified);
$this->assertEquals($this->items, $result);
}
@@ -223,8 +210,27 @@ class ItemMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
// 'AND `*dbprefix*news_items`.last_modified >= ? ';
//
// $this->setMapperResult($sql, array($userId, $folderId, $lastModified));
-// $result = $this->itemMapper->findAllFromFolderByLastMofified($userId, $folderId, $lastModified);
+// $result = $this->mapper->findAllFromFolderByLastMofified($userId, $folderId, $lastModified);
// }
+
+ public function testGetStarredCount(){
+ $userId = 'john';
+ $row = array(
+ array('size' => 9)
+ );
+ $sql = 'SELECT COUNT(*) AS size FROM `*dbprefix*news_feeds` `feeds` ' .
+ 'JOIN `*dbprefix*news_items` `items` ' .
+ 'ON `items`.`feed_id` = `feeds`.`id`' .
+ 'WHERE `feeds`.`user_id` = ? ' .
+ 'AND ((`items`.`status` & ?) > 0)';
+
+ $this->setMapperResult($sql, array($userId, StatusFlag::STARRED), $row);
+
+ $result = $this->mapper->starredCount($userId);
+ $this->assertEquals($row[0]['size'], $result);
+ }
+
+
} \ No newline at end of file