diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2013-04-18 15:56:12 +0200 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2013-04-18 15:56:12 +0200 |
commit | daa2c7dea555b334ffb7516f8af64ad1d090f39b (patch) | |
tree | 9810e03f5a7f8d09043050da7f65f0d94af8466b /tests/unit/utility/FeedFetcherTest.php | |
parent | 821aa0777d37f73ce12588cece1af18fd89113bc (diff) |
added test for fetcher class fix #47
Diffstat (limited to 'tests/unit/utility/FeedFetcherTest.php')
-rw-r--r-- | tests/unit/utility/FeedFetcherTest.php | 231 |
1 files changed, 228 insertions, 3 deletions
diff --git a/tests/unit/utility/FeedFetcherTest.php b/tests/unit/utility/FeedFetcherTest.php index ba4a53db1..bb027a778 100644 --- a/tests/unit/utility/FeedFetcherTest.php +++ b/tests/unit/utility/FeedFetcherTest.php @@ -25,15 +25,89 @@ namespace OCA\News\Utility; +use \OCA\News\Db\Item; +use \OCA\News\Db\Feed; + require_once(__DIR__ . "/../../classloader.php"); class FeedFetcherTest extends \OCA\AppFramework\Utility\TestUtility { private $fetcher; + private $core; + private $coreFactory; + private $faviconFetcher; + private $url; + private $cacheDirectory; + private $cacheDuration; + private $time; + private $item; + + // items + private $permalink; + private $title; + private $guid; + private $pub; + private $body; + private $author; + private $enclosureLink; + + // feed + private $feedTitle; + private $feedLink; + private $feedImage; + private $webFavicon; protected function setUp(){ - $this->fetcher = new FeedFetcher($this->getAPIMock(), 'dir', 300); + $this->core = $this->getMockBuilder( + '\SimplePie_Core') + ->disableOriginalConstructor() + ->getMock(); + $this->coreFactory = $this->getMockBuilder( + '\OCA\AppFramework\Utility\SimplePieAPIFactory') + ->disableOriginalConstructor() + ->getMock(); + $this->coreFactory->expects($this->any()) + ->method('getCore') + ->will($this->returnValue($this->core)); + $this->item = $this->getMockBuilder( + '\SimplePie_Item') + ->disableOriginalConstructor() + ->getMock(); + $this->faviconFetcher = $this->getMockBuilder( + '\OCA\AppFramework\Utility\FaviconFetcher') + ->disableOriginalConstructor() + ->getMock(); + $this->time = 2323; + $timeFactory = $this->getMockBuilder( + '\OCA\AppFramework\Utility\TimeFactory') + ->disableOriginalConstructor() + ->getMock(); + $timeFactory->expects($this->any()) + ->method('getTime') + ->will($this->returnValue($this->time)); + $this->cacheDuration = 100; + $this->cacheDirectory = 'dir/'; + $this->fetcher = new FeedFetcher($this->getAPIMock(), + $this->coreFactory, + $this->faviconFetcher, + $timeFactory, + $this->cacheDirectory, + $this->cacheDuration); + $this->url = 'tests'; + + $this->permalink = 'http://permalink'; + $this->title = 'my title'; + $this->guid = 'hey guid here'; + $this->body = 'let the bodies hit the floor'; + $this->pub = 23111; + $this->author = 'boogieman'; + $this->enclosureLink = 'http://enclosure.you'; + + $this->feedTitle = '<e;its a title'; + $this->feedLink = 'http://goatse'; + $this->feedImage = '/an/image'; + $this->webFavicon = 'http://anon.google.com'; } @@ -43,5 +117,156 @@ class FeedFetcherTest extends \OCA\AppFramework\Utility\TestUtility { $this->assertTrue($this->fetcher->canHandle($url)); } - // TODO: write tests for the remaining methods -}
\ No newline at end of file + + public function testFetchThrowsExceptionWhenInitFailed() { + $this->core->expects($this->once()) + ->method('set_feed_url') + ->with($this->equalTo($this->url)); + $this->core->expects($this->once()) + ->method('enable_cache') + ->with($this->equalTo(true)); + $this->core->expects($this->once()) + ->method('set_cache_location') + ->with($this->equalTo($this->cacheDirectory)); + $this->core->expects($this->once()) + ->method('set_cache_duration') + ->with($this->equalTo($this->cacheDuration)); + $this->setExpectedException('\OCA\News\Utility\FetcherException'); + $this->fetcher->fetch($this->url); + } + + + public function testShouldCatchExceptionsAndThrowOwnException() { + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $this->core->expects($this->once()) + ->method('get_items') + ->will($this->throwException(new \Exception('oh noes!'))); + $this->setExpectedException('\OCA\News\Utility\FetcherException'); + $this->fetcher->fetch($this->url); + } + + + private function expectCore($method, $return) { + $this->core->expects($this->once()) + ->method($method) + ->will($this->returnValue($return)); + } + + private function expectItem($method, $return) { + $this->item->expects($this->once()) + ->method($method) + ->will($this->returnValue($return)); + } + + + private function createItem($author=false, $enclosureType=null) { + $this->expectItem('get_permalink', $this->permalink); + $this->expectItem('get_title', $this->title); + $this->expectItem('get_id', $this->guid); + $this->expectItem('get_content', $this->body); + $this->expectItem('get_date', $this->pub); + + $item = new Item(); + $item->setStatus(0); + $item->setUnread(); + $item->setUrl($this->permalink); + $item->setTitle($this->title); + $item->setGuid($this->guid); + $item->setGuidHash(md5($this->guid)); + $item->setBody($this->body); + $item->setPubDate($this->pub); + $item->setLastModified($this->time); + if($author) { + $mock = $this->getMock('author', array('get_name')); + $mock->expects($this->once()) + ->method('get_name') + ->will($this->returnValue($this->author)); + $this->expectItem('get_author', $mock); + $item->setAuthor($this->author); + } + + if($enclosureType === 'audio/ogg') { + $mock = $this->getMock('enclosure', array('get_type', 'get_link')); + $mock->expects($this->any()) + ->method('get_type') + ->will($this->returnValue($enclosureType)); + $this->expectItem('get_enclosure', $this->mock); + $item->setEnclosureMime($enclosureType); + $item->setEnclosureLink($this->enclosureLink); + } + return $item; + } + + + private function createFeed($hasFavicon=false, $hasWebFavicon=false) { + $this->expectCore('get_title', $this->feedTitle); + $this->expectCore('get_link', $this->feedLink); + + $feed = new Feed(); + $feed->setTitle(html_entity_decode($this->feedTitle)); + $feed->setUrl($this->url); + $feed->setLink($this->feedLink); + $feed->setUrlHash(md5($this->url)); + $feed->setAdded($this->time); + + if($hasFavicon) { + $this->expectCore('get_image_url', $this->feedImage); + $feed->setFaviconLink($this->feedImage); + } else { + $feed->setFaviconLink(null); + $this->expectCore('get_image_url', null); + } + + if($hasWebFavicon) { + $this->faviconFetcher->expects($this->once()) + ->method('fetch') + ->with($this->equalTo($this->feedLink)) + ->will($this->returnValue($this->webFavicon)); + $feed->setFaviconLink($this->webFavicon); + } + + return $feed; + } + + + public function testFetchMapItems(){ + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $item = $this->createItem(); + $feed = $this->createFeed(); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url); + + $this->assertEquals(array($feed, array($item)), $result); + } + + + public function testFetchMapItemsAuthorExists(){ + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $item = $this->createItem(true); + $feed = $this->createFeed(true); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url); + + $this->assertEquals(array($feed, array($item)), $result); + } + + + public function testFetchMapItemsEnclosureExists(){ + $this->core->expects($this->once()) + ->method('init') + ->will($this->returnValue(true)); + $item = $this->createItem(false, true); + $feed = $this->createFeed(false, true); + $this->expectCore('get_items', array($this->item)); + $result = $this->fetcher->fetch($this->url); + + $this->assertEquals(array($feed, array($item)), $result); + } + +} |