Permalink
Browse files

Handle 'no result' more gracefully then a NOTICE/WARNING! #1

  • Loading branch information...
1 parent 41694f1 commit ac3d0af84ce0011c56158ade4c80134e5f3ccc4b @till till committed Sep 27, 2012
View
7 src/PEAR2/Services/NYTimes/Articlesearch.php
@@ -42,9 +42,11 @@ class Articlesearch extends Base implements NYTimesInterface
/**
* Search by URL.
*
+ * Returns a \stdClass if successful, or false when not (no result).
+ *
* @param string $url A URL to an article.
*
- * @return stdClass
+ * @return stdClass|false
*/
public function byUrl($url)
{
@@ -55,6 +57,9 @@ public function byUrl($url)
$response = $this->makeRequest($uri);
$data = $this->parseResponse($response);
+ if (empty($data->results)) {
+ return false;
+ }
return $data->results[0];
}
View
14 tests/PEAR2/Services/NYTimes/Test/ArticlesearchTest.php
@@ -43,4 +43,18 @@ public function testUrlSearch()
$this->assertInstanceOf('stdClass', $data);
}
+
+ public function testUrlSearchNoResult()
+ {
+ $responseObject = $this->setUpResponseObject(
+ 'articlesearch',
+ 'v1',
+ 'by-url-no-result.php'
+ );
+
+ $asMock = $this->getApiMocked('articlesearch', $responseObject);
+ $data = $asMock->byUrl('http://www.nytimes.com/2011/07/09/business/economy/job-growth-falters-badly-clouding-hope-for-recovery.html?hp');
+
+ $this->assertFalse($data);
+ }
}
View
6 tests/fixtures/articlesearch/v1/by-url-no-result.php
@@ -0,0 +1,6 @@
+<?php
+return array(
+ 'effectiveUrl' => 'http://api.nytimes.com/svc/search/v1/article?query=url%3Ahttp%3A%2F%2Fwww.nytimes.com%2F2011%2F07%2F09%2Fbusiness%2Feconomy%2Fjob-growth-falters-badly-clouding-hope-for-recovery.html&api-key=f8f203975851104767077c83bb51fd95%3A6%3A57657007',
+ 'body' => '{"offset" : "0" , "results" : [] , "tokens" : ["url:http" , "www" , "nytimes" , "com" , "2012" , "09" , "05" , "opinion" , "why" , "mali" , "matters" , "html"] , "total" : 0}',
+ 'statusLine' => 'HTTP/1.1 200 OK',
+);

0 comments on commit ac3d0af

Please sign in to comment.