From cc3fa38cee2e24dacb940ec5d7ca41e593aa824a Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 22 Oct 2014 10:49:34 +0200 Subject: fix tests --- tests/unit/fetcher/FeedFetcherTest.php | 260 ++++++++++----------------- tests/unit/utility/ProxyConfigParserTest.php | 93 ++++++++++ 2 files changed, 190 insertions(+), 163 deletions(-) create mode 100644 tests/unit/utility/ProxyConfigParserTest.php (limited to 'tests') diff --git a/tests/unit/fetcher/FeedFetcherTest.php b/tests/unit/fetcher/FeedFetcherTest.php index d87d88952..1f3b3d108 100644 --- a/tests/unit/fetcher/FeedFetcherTest.php +++ b/tests/unit/fetcher/FeedFetcherTest.php @@ -36,7 +36,6 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { private $pub; private $body; private $author; - private $authorMail; private $enclosureLink; // feed @@ -44,6 +43,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { private $feedLink; private $feedImage; private $webFavicon; + private $modified; protected function setUp(){ $this->reader = $this->getMockBuilder( @@ -62,11 +62,15 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { '\PicoFeed\Feed') ->disableOriginalConstructor() ->getMock(); - + $this->item = $this->getMockBuilder( + '\PicoFeed\Item') + ->disableOriginalConstructor() + ->getMock(); $this->faviconFetcher = $this->getMockBuilder( '\PicoFeed\Favicon') ->disableOriginalConstructor() ->getMock(); + $this->time = 2323; $timeFactory = $this->getMock('TimeFactory', ['getTime']); $timeFactory->expects($this->any()) @@ -92,6 +96,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $this->feedImage = '/an/image'; $this->webFavicon = 'http://anon.google.com'; $this->authorMail = 'doe@joes.com'; + $this->modified = 3; } @@ -101,42 +106,45 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->fetcher->canHandle($url)); } - private function setUpReader($url='', $modified=true) { + private function setUpReader($url='', $modified=true, $noParser=false, + $noFeed=false) { $this->reader->expects($this->once()) ->method('download') ->with($this->equalTo($url)) ->will($this->returnValue($this->client)); $this->client->expects($this->once()) ->method('getLastModified') - ->with() - ->will($this->returnValue($modified)); - } - - - public function testFetchThrowsExceptionWhenFetchingFailed() { - $this->setUpReader($this->url); - $this->reader->expects($this->once()) - ->method('getParser') - ->will($this->returnValue(false)); + ->will($this->returnValue($this->modified)); - $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); - $this->fetcher->fetch($this->url); - } - - - public function testFetchThrowsExceptionWhenParsingFailed() { - $this->setUpReader($this->url); - $this->reader->expects($this->once()) - ->method('getParser') - ->will($this->returnValue(false)); + if ($noParser) { + $this->reader->expects($this->once()) + ->method('getParser') + ->will($this->returnValue(false)); + } else { + $this->reader->expects($this->once()) + ->method('getParser') + ->will($this->returnValue($this->parser)); + + if ($noFeed) { + $this->parser->expects($this->once()) + ->method('execute') + ->will($this->returnValue(false)); + } else { + $this->parser->expects($this->once()) + ->method('execute') + ->will($this->returnValue($this->parsedFeed)); + } + } - $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); - $this->fetcher->fetch($this->url); + // uncomment if testing caching + /*$this->client->expects($this->once()) + ->method('isModified') + ->will($this->returnValue($modified));*/ } - private function expectCore($method, $return, $count = 1) { - $this->core->expects($this->exactly($count)) + private function expectFeed($method, $return, $count = 1) { + $this->parsedFeed->expects($this->exactly($count)) ->method($method) ->will($this->returnValue($return)); } @@ -148,22 +156,16 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { } - private function createItem($author=false, $enclosureType=null, - $noPubDate=false) { - $this->expectItem('get_permalink', $this->permalink); - $this->expectItem('get_title', $this->title); - $this->expectItem('get_id', $this->guid); - $this->expectItem('get_content', $this->body); + private function createItem($enclosureType=null) { + $this->expectItem('getUrl', $this->permalink); + $this->expectItem('getTitle', $this->title); + $this->expectItem('getId', $this->guid); + $this->expectItem('getContent', $this->body); $item = new Item(); - if($noPubDate) { - $this->expectItem('get_date', 0); - $item->setPubDate($this->time); - } else { - $this->expectItem('get_date', $this->pub); - $item->setPubDate($this->pub); - } + $this->expectItem('getDate', $this->pub); + $item->setPubDate($this->pub); $item->setStatus(0); $item->setUnread(); @@ -173,46 +175,14 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $item->setGuidHash(md5($this->guid)); $item->setBody($this->body); $item->setLastModified($this->time); - if($author) { - $mock = $this->getMock('author', ['get_name']); - $mock->expects($this->once()) - ->method('get_name') - ->will($this->returnValue($this->author)); - $this->expectItem('get_author', $mock); - $item->setAuthor(html_entity_decode($this->author)); - } else { - $mock = $this->getMock('author', ['get_name', 'get_email']); - $mock->expects($this->any()) - ->method('get_name') - ->will($this->returnValue('')); - $mock->expects($this->any()) - ->method('get_email') - ->will($this->returnValue($this->authorMail)); - - $this->expectItem('get_author', $mock); - $item->setAuthor(html_entity_decode($this->authorMail)); - } - if($enclosureType === 'audio/ogg') { - $mock = $this->getMock('enclosure', ['get_type', 'get_link']); - $mock->expects($this->any()) - ->method('get_type') - ->will($this->returnValue($enclosureType)); - $mock->expects($this->any()) - ->method('get_link') - ->will($this->returnValue($this->enclosureLink)); - $this->expectItem('get_enclosure', $mock); - $item->setEnclosureMime($enclosureType); - $item->setEnclosureLink($this->enclosureLink); - } elseif ($enclosureType === 'video/ogg') { - $mock = $this->getMock('enclosure', ['get_type', 'get_link']); - $mock->expects($this->any()) - ->method('get_type') - ->will($this->returnValue($enclosureType)); - $mock->expects($this->any()) - ->method('get_link') - ->will($this->returnValue($this->enclosureLink)); - $this->expectItem('get_enclosure', $mock); + $this->expectItem('getAuthor', $this->author); + $item->setAuthor(html_entity_decode($this->author)); + + if($enclosureType === 'audio/ogg' || $enclosureType === 'video/ogg') { + $this->expectItem('getEnclosureUrl', $this->enclosureLink); + $this->expectItem('getEnclosureType', $enclosureType); + $item->setEnclosureMime($enclosureType); $item->setEnclosureLink($this->enclosureLink); } @@ -220,9 +190,9 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { } - private function createFeed($hasFeedFavicon=false, $hasWebFavicon=false) { - $this->expectCore('get_title', $this->feedTitle); - $this->expectCore('get_permalink', $this->feedLink, 2); + private function createFeed($hasFavicon=false) { + $this->expectFeed('getTitle', $this->feedTitle); + $this->expectFeed('getUrl', $this->feedLink, 2); $feed = new Feed(); $feed->setTitle('&its a title'); @@ -230,7 +200,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $feed->setLink($this->feedLink); $feed->setAdded($this->time); - if($hasWebFavicon) { + if($hasFavicon) { $this->faviconFetcher->expects($this->once()) ->method('find') ->with($this->equalTo($this->feedLink)) @@ -238,35 +208,40 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $feed->setFaviconLink($this->webFavicon); } - if($hasFeedFavicon) { - $this->expectCore('get_image_url', $this->feedImage); - $feed->setFaviconLink($this->feedImage); - } elseif(!$hasWebFavicon) { - $feed->setFaviconLink(null); - $this->expectCore('get_image_url', null); - } + return $feed; + } - return $feed; + public function testFetchThrowsExceptionWhenFetchingFailed() { + $this->setUpReader($this->url, true, false); + + $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); + $this->fetcher->fetch($this->url); } - public function testFetchMapItems(){ - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - $item = $this->createItem(false, 'audio/ogg'); + public function testFetchThrowsExceptionWhenParsingFailed() { + $this->setUpReader($this->url, true, true, false); + + $this->setExpectedException('\OCA\News\Fetcher\FetcherException'); + $this->fetcher->fetch($this->url); + } + + public function testFetch(){ + $this->setUpReader($this->url); + $item = $this->createItem(); $feed = $this->createFeed(); - $this->expectCore('get_items', [$this->item]); + $this->expectFeed('getItems', [$this->item]); $result = $this->fetcher->fetch($this->url); $this->assertEquals([$feed, [$item]], $result); } - public function testFetchMapItemsNoFeedTitleUsesUrl(){ - $this->expectCore('get_title', ''); - $this->expectCore('get_permalink', $this->feedLink, 2); + public function testNoTitleUsesUrl(){ + $this->setUpReader($this->url); + $this->expectFeed('getTitle', ''); + $this->expectFeed('getUrl', $this->feedLink, 2); $feed = new Feed(); $feed->setTitle($this->url); @@ -275,100 +250,59 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $feed->setAdded($this->time); $feed->setFaviconLink(null); - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); $item = $this->createItem(); - $this->expectCore('get_items', [$this->item]); - $result = $this->fetcher->fetch($this->url); - - $this->assertEquals([$feed, [$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', [$this->item]); + $this->expectFeed('getItems', [$this->item]); $result = $this->fetcher->fetch($this->url); $this->assertEquals([$feed, [$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', [$this->item]); + public function testAudioEnclosure(){ + $this->setUpReader($this->url); + $item = $this->createItem('audio/ogg'); + $feed = $this->createFeed(); + $this->expectFeed('getItems', [$this->item]); $result = $this->fetcher->fetch($this->url); $this->assertEquals([$feed, [$item]], $result); } - public function testFetchMapItemsNoPubdate(){ - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - $item = $this->createItem(false, true, true); - $feed = $this->createFeed(false, true); - $this->expectCore('get_items', [$this->item]); + public function testVideoEnclosure(){ + $this->setUpReader($this->url); + $item = $this->createItem('video/ogg'); + $feed = $this->createFeed(); + $this->expectFeed('getItems', [$this->item]); $result = $this->fetcher->fetch($this->url); $this->assertEquals([$feed, [$item]], $result); } - public function testFetchMapItemsGetFavicon() { - $this->expectCore('get_title', $this->feedTitle); - $this->expectCore('get_permalink', $this->feedLink, 2); - $feed = new Feed(); - $feed->setTitle('&its a title'); - $feed->setUrl($this->url); - $feed->setLink($this->feedLink); - $feed->setAdded($this->time); - $feed->setFaviconLink($this->webFavicon); - - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); - - $this->faviconFetcher->expects($this->once()) - ->method('find') - ->will($this->returnValue($this->webFavicon)); + public function testFavicon() { + $this->setUpReader($this->url); - $item = $this->createItem(false, 'video/ogg'); - $this->expectCore('get_items', [$this->item]); + $feed = $this->createFeed(true); + $item = $this->createItem(); + $this->expectFeed('getItems', [$this->item]); $result = $this->fetcher->fetch($this->url); $this->assertEquals([$feed, [$item]], $result); } - public function testFetchMapItemsNoGetFavicon() { - $this->expectCore('get_title', $this->feedTitle); - $this->expectCore('get_permalink', $this->feedLink, 2); - $feed = new Feed(); - $feed->setTitle('&its a title'); - $feed->setUrl($this->url); - $feed->setLink($this->feedLink); - $feed->setAdded($this->time); + public function testNoFavicon() { + $this->setUpReader($this->url); - $this->core->expects($this->once()) - ->method('init') - ->will($this->returnValue(true)); + $feed = $this->createFeed(false); $this->faviconFetcher->expects($this->never()) - ->method('fetch'); + ->method('find'); - $item = $this->createItem(false, true); - $this->expectCore('get_items', [$this->item]); + $item = $this->createItem(); + $this->expectFeed('getItems', [$this->item]); $result = $this->fetcher->fetch($this->url, false); $this->assertEquals([$feed, [$item]], $result); diff --git a/tests/unit/utility/ProxyConfigParserTest.php b/tests/unit/utility/ProxyConfigParserTest.php new file mode 100644 index 000000000..39f6e61a3 --- /dev/null +++ b/tests/unit/utility/ProxyConfigParserTest.php @@ -0,0 +1,93 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Utility; + + +class ProxyConfigParserTest extends \PHPUnit_Framework_TestCase { + + private $config; + private $feedService; + private $itemService; + private $parser; + + protected function setUp() { + $this->config = $this->getMockBuilder( + '\OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->parser = new ProxyConfigParser($this->config); + } + + private function setExpectedProxy($proxy=null, $userpasswd=null) { + $this->config->expects($this->at(0)) + ->method('getSystemValue') + ->with($this->equalTo('proxy')) + ->will($this->returnValue($proxy)); + $this->config->expects($this->at(1)) + ->method('getSystemValue') + ->with($this->equalTo('proxyuserpwd')) + ->will($this->returnValue($userpasswd)); + } + + public function testParsesNoProxy() { + $expected = [ + 'host' => null, + 'port' => null, + 'user' => null, + 'password' => null + ]; + $this->setExpectedProxy(); + $result = $this->parser->parse(); + $this->assertEquals($expected, $result); + } + + + public function testParsesHost() { + $expected = [ + 'host' => 'http://google.com/mytest', + 'port' => null, + 'user' => null, + 'password' => null + ]; + $this->setExpectedProxy('http://google.com/mytest'); + $result = $this->parser->parse(); + $this->assertEquals($expected, $result); + } + + + public function testParsesHostAndPort() { + $expected = [ + 'host' => 'http://google.com/mytest', + 'port' => 89, + 'user' => null, + 'password' => null + ]; + $this->setExpectedProxy('http://google.com:89/mytest'); + $result = $this->parser->parse(); + $this->assertEquals($expected, $result); + } + + + public function testParsesUser() { + $expected = [ + 'host' => null, + 'port' => null, + 'user' => 'john', + 'password' => 'doe:hey' + ]; + $this->setExpectedProxy(null, 'john:doe:hey'); + $result = $this->parser->parse(); + $this->assertEquals($expected, $result); + } +} \ No newline at end of file -- cgit v1.2.3