diff options
-rw-r--r-- | appinfo/application.php | 22 | ||||
-rw-r--r-- | fetcher/feedfetcher.php | 25 | ||||
-rw-r--r-- | tests/unit/fetcher/FeedFetcherTest.php | 34 | ||||
-rw-r--r-- | utility/picofeedfaviconfactory.php | 38 | ||||
-rw-r--r-- | utility/picofeedreaderfactory.php | 38 |
5 files changed, 124 insertions, 33 deletions
diff --git a/appinfo/application.php b/appinfo/application.php index be903b120..d541a4f81 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -15,10 +15,7 @@ namespace OCA\News\AppInfo; require_once __DIR__ . '/autoload.php'; -use \PicoFeed\Reader as PicoFeedReader; use \PicoFeed\Config as PicoFeedConfig; -use \PicoFeed\Client as PicoFeedClient; -use \PicoFeed\Favicon as PicoFeedFavicon; use \OC\Files\View; use \OCP\AppFramework\App; @@ -51,6 +48,8 @@ use \OCA\News\Db\MapperFactory; use \OCA\News\Utility\OPMLExporter; use \OCA\News\Utility\Updater; use \OCA\News\Utility\PicoFeedClientFactory; +use \OCA\News\Utility\PicoFeedReaderFactory; +use \OCA\News\Utility\PicoFeedFaviconFactory; use \OCA\News\Utility\ProxyConfigParser; use \OCA\News\Fetcher\Fetcher; @@ -447,14 +446,17 @@ class Application extends App { return $pico; }); - $container->registerService('PicoFeedReader', function($c) { - return new PicoFeedReader($c->query('PicoFeedConfig')); + $container->registerService('PicoFeedReaderFactory', function($c) { + return new PicoFeedReaderFactory($c->query('PicoFeedConfig')); }); $container->registerService('PicoFeedClientFactory', function($c) { return new PicoFeedClientFactory($c->query('PicoFeedConfig')); }); + $container->registerService('PicoFeedFaviconFactory', function($c) { + return new PicoFeedFaviconFactory($c->query('PicoFeedConfig')); + }); $container->registerService('Fetcher', function($c) { $fetcher = new Fetcher(); @@ -468,8 +470,8 @@ class Application extends App { $container->registerService('FeedFetcher', function($c) { return new FeedFetcher( - $c->query('PicoFeedReader'), - $c->query('FaviconFetcher'), + $c->query('PicoFeedReaderFactory'), + $c->query('PicoFeedFaviconFactory'), $c->query('TimeFactory') ); }); @@ -497,12 +499,6 @@ class Application extends App { }); - $container->registerService('FaviconFetcher', function($c) { - return new PicoFeedFavicon( - $c->query('PicoFeedConfig') - ); - }); - } public function getAppConfig() { diff --git a/fetcher/feedfetcher.php b/fetcher/feedfetcher.php index 4efe0215c..b7ddce91a 100644 --- a/fetcher/feedfetcher.php +++ b/fetcher/feedfetcher.php @@ -13,21 +13,22 @@ namespace OCA\News\Fetcher; -use \PicoFeed\Favicon; -use \PicoFeed\Reader; - use \OCA\News\Db\Item; use \OCA\News\Db\Feed; +use \OCA\News\Utility\PicoFeedFaviconFactory; +use \OCA\News\Utility\PicoFeedReaderFactory; class FeedFetcher implements IFeedFetcher { - private $faviconFetcher; - private $reader; + private $faviconFactory; + private $readerFactory; private $time; - public function __construct(Reader $reader, Favicon $faviconFetcher, $time){ - $this->faviconFetcher = $faviconFetcher; - $this->reader = $reader; + public function __construct(PicoFeedReaderFactory $readerFactory, + PicoFeedFaviconFactory $faviconFactory, + $time){ + $this->faviconFactory = $faviconFactory; + $this->readerFactory = $readerFactory; $this->time = $time; } @@ -57,7 +58,8 @@ class FeedFetcher implements IFeedFetcher { */ public function fetch($url, $getFavicon=true, $lastModified=null, $etag=null) { - $resource = $this->reader->download($url, $lastModified, $etag); + $reader = $this->readerFactory->build(); + $resource = $reader->download($url, $lastModified, $etag); $modified = $resource->getLastModified(); $etag = $resource->getEtag(); @@ -67,7 +69,7 @@ class FeedFetcher implements IFeedFetcher { } try { - $parser = $this->reader->getParser(); + $parser = $reader->getParser(); if (!$parser) { throw new \Exception( @@ -184,7 +186,8 @@ class FeedFetcher implements IFeedFetcher { $feed->setAdded($this->time->getTime()); if ($getFavicon) { - $favicon = $this->faviconFetcher->find($feed->getLink()); + $faviconFetcher = $this->faviconFactory->build(); + $favicon = $faviconFetcher->find($feed->getLink()); $feed->setFaviconLink($favicon); } diff --git a/tests/unit/fetcher/FeedFetcherTest.php b/tests/unit/fetcher/FeedFetcherTest.php index a0bd0b866..ed697a8cf 100644 --- a/tests/unit/fetcher/FeedFetcherTest.php +++ b/tests/unit/fetcher/FeedFetcherTest.php @@ -25,6 +25,8 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { private $client; private $faviconFetcher; private $parsedFeed; + private $faviconFactory; + private $readerFactory; private $url; private $time; private $item; @@ -71,14 +73,22 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { '\PicoFeed\Favicon') ->disableOriginalConstructor() ->getMock(); + $this->readerFactory = $this->getMockBuilder( + '\OCA\News\Utility\PicoFeedReaderFactory') + ->disableOriginalConstructor() + ->getMock(); + $this->faviconFactory = $this->getMockBuilder( + '\OCA\News\Utility\PicoFeedFaviconFactory') + ->disableOriginalConstructor() + ->getMock(); $this->time = 2323; $timeFactory = $this->getMock('TimeFactory', ['getTime']); $timeFactory->expects($this->any()) ->method('getTime') ->will($this->returnValue($this->time)); - $this->fetcher = new FeedFetcher($this->reader, - $this->faviconFetcher, + $this->fetcher = new FeedFetcher($this->readerFactory, + $this->faviconFactory, $timeFactory); $this->url = 'http://tests'; @@ -110,6 +120,9 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { private function setUpReader($url='', $modified=true, $noParser=false, $noFeed=false) { + $this->readerFactory->expects($this->once()) + ->method('build') + ->will($this->returnValue($this->reader)); $this->reader->expects($this->once()) ->method('download') ->with($this->equalTo($url)) @@ -210,6 +223,9 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $feed->setEtag($this->etag); if($hasFavicon) { + $this->faviconFactory->expects($this->once()) + ->method('build') + ->will($this->returnValue($this->faviconFetcher)); $this->faviconFetcher->expects($this->once()) ->method('find') ->with($this->equalTo($this->feedLink)) @@ -225,7 +241,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $this->setUpReader($this->url, true, false); $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); - $this->fetcher->fetch($this->url); + $this->fetcher->fetch($this->url, false); } @@ -233,12 +249,12 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $this->setUpReader($this->url, true, true, false); $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); - $this->fetcher->fetch($this->url); + $this->fetcher->fetch($this->url, false); } public function testNoFetchIfNotModified(){ $this->setUpReader($this->url, false);; - $result = $this->fetcher->fetch($this->url); + $result = $this->fetcher->fetch($this->url, false); } public function testFetch(){ @@ -246,7 +262,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $item = $this->createItem(); $feed = $this->createFeed(); $this->expectFeed('getItems', [$this->item]); - $result = $this->fetcher->fetch($this->url); + $result = $this->fetcher->fetch($this->url, false); $this->assertEquals([$feed, [$item]], $result); } @@ -268,7 +284,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $item = $this->createItem(); $this->expectFeed('getItems', [$this->item]); - $result = $this->fetcher->fetch($this->url); + $result = $this->fetcher->fetch($this->url, false); $this->assertEquals([$feed, [$item]], $result); } @@ -279,7 +295,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $item = $this->createItem('audio/ogg'); $feed = $this->createFeed(); $this->expectFeed('getItems', [$this->item]); - $result = $this->fetcher->fetch($this->url); + $result = $this->fetcher->fetch($this->url, false); $this->assertEquals([$feed, [$item]], $result); } @@ -290,7 +306,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $item = $this->createItem('video/ogg'); $feed = $this->createFeed(); $this->expectFeed('getItems', [$this->item]); - $result = $this->fetcher->fetch($this->url); + $result = $this->fetcher->fetch($this->url, false); $this->assertEquals([$feed, [$item]], $result); } diff --git a/utility/picofeedfaviconfactory.php b/utility/picofeedfaviconfactory.php new file mode 100644 index 000000000..8438765cd --- /dev/null +++ b/utility/picofeedfaviconfactory.php @@ -0,0 +1,38 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + + +namespace OCA\News\Utility; + +use \PicoFeed\Config; +use \PicoFeed\Favicon; + +class PicoFeedFaviconFactory { + + private $config; + + public function __construct(Config $config) { + $this->config = $config; + } + + + /** + * Returns a new instance of an PicoFeed Http client + * @return \PicoFeed\Favicon instance + */ + public function build() { + return new Favicon($this->config); + } + + +}
\ No newline at end of file diff --git a/utility/picofeedreaderfactory.php b/utility/picofeedreaderfactory.php new file mode 100644 index 000000000..844a0e402 --- /dev/null +++ b/utility/picofeedreaderfactory.php @@ -0,0 +1,38 @@ +<?php +/** + * ownCloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Alessandro Cosentino <cosenal@gmail.com> + * @author Bernhard Posselt <dev@bernhard-posselt.com> + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + + +namespace OCA\News\Utility; + +use \PicoFeed\Config; +use \PicoFeed\Reader; + +class PicoFeedReaderFactory { + + private $config; + + public function __construct(Config $config) { + $this->config = $config; + } + + + /** + * Returns a new instance of an PicoFeed Http Reader + * @return \PicoFeed\Reader instance + */ + public function build() { + return new Reader($this->config); + } + + +}
\ No newline at end of file |