summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bl/feedbl.php30
-rw-r--r--bl/folderbl.php8
-rw-r--r--bl/itembl.php7
-rw-r--r--dependencyinjection/dicontainer.php2
-rw-r--r--tests/bl/FeedBlTest.php73
-rw-r--r--tests/bl/ItemBlTest.php11
-rw-r--r--utility/feedfetcher.php17
7 files changed, 116 insertions, 32 deletions
diff --git a/bl/feedbl.php b/bl/feedbl.php
index da6c602bf..5e5ff4fa7 100644
--- a/bl/feedbl.php
+++ b/bl/feedbl.php
@@ -28,16 +28,17 @@ namespace OCA\News\Bl;
use \OCA\News\Db\Feed;
use \OCA\News\Db\FeedMapper;
use \OCA\News\Utility\FeedFetcher;
-
+use \OCA\News\Utility\FetcherException;
class FeedBl extends Bl {
private $feedFetcher;
- public function __construct(FeedMapper $feedMapper,
- FeedFetcher $feedFetcher){
+ public function __construct(FeedMapper $feedMapper,
+ FeedFetcher $feedFetcher, ItemBl $itemBl){
parent::__construct($feedMapper);
$this->feedFetcher = $feedFetcher;
+ $this->itemBl = $itemBl;
}
@@ -53,8 +54,25 @@ class FeedBl extends Bl {
}
- public function create($feedUrl, $parentId, $userId){
- // TODO: download new items of feed
+ public function create($feedUrl, $folderId, $userId){
+ // first try if its possible to reach the feed
+ try {
+ list($feed, $items) = $this->feedFetcher->fetch($feedUrl);
+
+ // insert feed
+ $feed->setFolderId($folderId);
+ $feed = $this->mapper->insert($feed);
+
+ // insert items
+ foreach($items as $item){
+ $item->setFeedId($feed->getId());
+ $this->itemBl->create($item);
+ }
+
+ return $feed;
+ } catch(FetcherException $ex){
+ throw new BLException('Can not add feed: Not found or bad source');
+ }
}
@@ -69,5 +87,5 @@ class FeedBl extends Bl {
$this->mapper->update($feed);
}
-
+ // TODO: delete associated items
}
diff --git a/bl/folderbl.php b/bl/folderbl.php
index 638f10bd3..c17c516ba 100644
--- a/bl/folderbl.php
+++ b/bl/folderbl.php
@@ -49,7 +49,9 @@ class FolderBl extends Bl {
}
}
-
+ /**
+ * @throws BLException if name exists already
+ */
public function create($folderName, $userId, $parentId=0) {
$this->allowNoNameTwice($folderName, $userId);
@@ -68,6 +70,9 @@ class FolderBl extends Bl {
}
+ /**
+ * @throws BLException if name exists already
+ */
public function rename($folderId, $folderName, $userId){
$this->allowNoNameTwice($folderName, $userId);
@@ -76,5 +81,6 @@ class FolderBl extends Bl {
$this->mapper->update($folder);
}
+ // TODO: delete associated items
}
diff --git a/bl/itembl.php b/bl/itembl.php
index f2b1ac1b3..629f9317f 100644
--- a/bl/itembl.php
+++ b/bl/itembl.php
@@ -72,4 +72,11 @@ class ItemBl extends Bl {
$this->mapper->readFeed($feedId, $userId);
}
+
+ // ATTENTION: this does no validation and is only for creating
+ // items from the fetcher
+ public function create($item){
+ $this->mapper->insert($item);
+ }
+
}
diff --git a/dependencyinjection/dicontainer.php b/dependencyinjection/dicontainer.php
index f65108bdb..a199bfc4a 100644
--- a/dependencyinjection/dicontainer.php
+++ b/dependencyinjection/dicontainer.php
@@ -93,7 +93,7 @@ class DIContainer extends BaseContainer {
});
$this['FeedBl'] = $this->share(function($c){
- return new FeedBl($c['FeedMapper'], $c['FeedFetcher']);
+ return new FeedBl($c['FeedMapper'], $c['FeedFetcher'], $c['ItemBl']);
});
$this['ItemBl'] = $this->share(function($c){
diff --git a/tests/bl/FeedBlTest.php b/tests/bl/FeedBlTest.php
index 33bf5704c..bef45bd2e 100644
--- a/tests/bl/FeedBlTest.php
+++ b/tests/bl/FeedBlTest.php
@@ -29,54 +29,97 @@ require_once(__DIR__ . "/../classloader.php");
use \OCA\News\Db\Feed;
-
+use \OCA\News\Db\Item;
+use \OCA\News\Utility\FeedFetcher;
+use \OCA\News\Utility\FetcherException;
class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility {
protected $api;
- protected $feedMapper;
- protected $feedBl;
+ protected $mapper;
+ protected $bl;
protected $user;
protected $response;
- protected $utils;
+ protected $fetcher;
+ protected $itemBl;
protected function setUp(){
$this->api = $this->getAPIMock();
- $this->feedMapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper')
+ $this->mapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->fetcher = $this->getMockBuilder('\OCA\News\Utility\FeedFetcher')
->disableOriginalConstructor()
->getMock();
- $this->utils = $this->getMockBuilder('\OCA\News\Utility\FeedFetcher')
+ $this->itemBl = $this->getMockBuilder('\OCA\News\Bl\ItemBl')
->disableOriginalConstructor()
->getMock();
- $this->feedBl = new FeedBl($this->feedMapper, $this->utils);
+ $this->bl = new FeedBl($this->mapper, $this->fetcher, $this->itemBl);
$this->user = 'jack';
$response = 'hi';
}
public function testFindAll(){
- $this->feedMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('findAll')
->will($this->returnValue($this->response));
- $result = $this->feedBl->findAll();
+ $result = $this->bl->findAll();
$this->assertEquals($this->response, $result);
}
public function testFindAllFromUser(){
- $this->feedMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('findAllFromUser')
->with($this->equalTo($this->user))
->will($this->returnValue($this->response));
- $result = $this->feedBl->findAllFromUser($this->user);
+ $result = $this->bl->findAllFromUser($this->user);
$this->assertEquals($this->response, $result);
}
+ public function testCreateDoesNotFindFeed(){
+ $ex = new FetcherException('hi');
+ $url = 'test';
+ $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);
+ }
+
public function testCreate(){
- // TODO
+ $url = 'test';
+ $folderId = 10;
+ $createdFeed = new Feed();
+ $createdFeed->setUrl($url);
+ $return = array(
+ $createdFeed,
+ array(new Item(), new Item())
+ );
+ $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->itemBl->expects($this->at(0))
+ ->method('create')
+ ->with($this->equalTo($return[1][0]));
+ $this->itemBl->expects($this->at(1))
+ ->method('create')
+ ->with($this->equalTo($return[1][1]));
+
+ $feed = $this->bl->create($url, $folderId, $this->user);
+
+ $this->assertEquals($feed->getFolderId(), $folderId);
+ $this->assertEquals($feed->getUrl(), $url);
}
@@ -92,16 +135,16 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility {
$feed->setFolderId(16);
$feed->setId($feedId);
- $this->feedMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('find')
->with($this->equalTo($feedId), $this->equalTo($this->user))
->will($this->returnValue($feed));
- $this->feedMapper->expects($this->once())
+ $this->mapper->expects($this->once())
->method('update')
->with($this->equalTo($feed));
- $this->feedBl->move($feedId, $folderId, $this->user);
+ $this->bl->move($feedId, $folderId, $this->user);
$this->assertEquals($folderId, $feed->getFolderId());
}
diff --git a/tests/bl/ItemBlTest.php b/tests/bl/ItemBlTest.php
index 5a4070e06..4b4c71219 100644
--- a/tests/bl/ItemBlTest.php
+++ b/tests/bl/ItemBlTest.php
@@ -131,6 +131,17 @@ class ItemBlTest extends \OCA\AppFramework\Utility\TestUtility {
$this->bl->readFeed($feedId, $this->user);
}
+
+ public function testCreate(){
+ $item = new Item();
+
+ $this->mapper->expects($this->once())
+ ->method('insert')
+ ->with($this->equalTo($item));
+
+ $this->bl->create($item, $this->user);
+ }
+
}
diff --git a/utility/feedfetcher.php b/utility/feedfetcher.php
index edaa72b9a..e6ebba5e8 100644
--- a/utility/feedfetcher.php
+++ b/utility/feedfetcher.php
@@ -33,16 +33,16 @@ class FeedFetcher {
* Fetch a feed from remote
* @param string url remote url of the feed
* @throws FetcherException if simple pie fails
- * @returns array an array containing the new feed and its items
+ * @return array an array containing the new feed and its items
*/
public function fetch($url) {
$simplePie = new \SimplePie_Core();
$simplePie->set_feed_url( $url );
$simplePie->enable_cache( false );
- // TODO: throw error
+
if (!$simplePie->init()) {
- return null;
+ throw new FetcherException('Could not initialize simple pie');
}
// temporary try-catch to bypass SimplePie bugs
@@ -64,7 +64,7 @@ class FeedFetcher {
$item->setAuthor( $author->get_name() );
}
- // associated media file, for podcasts
+ // TODO: make it work for video files also
$enclosure = $feedItem->get_enclosure();
if($enclosure !== null) {
$enclosureType = $enclosure->get_type();
@@ -96,7 +96,7 @@ class FeedFetcher {
}
}
return array($feed, $items);
- } catch(Exception $ex){
+ } catch(\Exception $ex){
throw new FetcherException($ex->getMessage());
}
@@ -104,8 +104,7 @@ class FeedFetcher {
/**
* Perform a "slim" fetch of a feed from remote.
- * Differently from Utils::fetch(), it doesn't retrieve items nor a favicon
- *
+ * Differently from fetch(), it doesn't retrieve items nor a favicon
* @param string url remote url of the feed
* @returns \OCA\News\Db\Feed
*/
@@ -116,7 +115,7 @@ class FeedFetcher {
$simplePie->set_stupidly_fast( true );
if (!$simplePie->init()) {
- return null;
+ throw new FetcherException('Could not initialize simple pie');
}
// temporary try-catch to bypass SimplePie bugs
@@ -128,7 +127,7 @@ class FeedFetcher {
$feed->setAdded(time());
return $feed;
- } catch(Exception $ex){
+ } catch(\Exception $ex){
throw new FetcherException($ex->getMessage());
}
}