Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit ac3d0af84ce0011c56158ade4c80134e5f3ccc4b 1 parent 41694f1
Till! till authored
7 src/PEAR2/Services/NYTimes/Articlesearch.php
@@ -42,9 +42,11 @@ class Articlesearch extends Base implements NYTimesInterface
42 42 /**
43 43 * Search by URL.
44 44 *
  45 + * Returns a \stdClass if successful, or false when not (no result).
  46 + *
45 47 * @param string $url A URL to an article.
46 48 *
47   - * @return stdClass
  49 + * @return stdClass|false
48 50 */
49 51 public function byUrl($url)
50 52 {
@@ -55,6 +57,9 @@ public function byUrl($url)
55 57 $response = $this->makeRequest($uri);
56 58
57 59 $data = $this->parseResponse($response);
  60 + if (empty($data->results)) {
  61 + return false;
  62 + }
58 63
59 64 return $data->results[0];
60 65 }
14 tests/PEAR2/Services/NYTimes/Test/ArticlesearchTest.php
@@ -43,4 +43,18 @@ public function testUrlSearch()
43 43
44 44 $this->assertInstanceOf('stdClass', $data);
45 45 }
  46 +
  47 + public function testUrlSearchNoResult()
  48 + {
  49 + $responseObject = $this->setUpResponseObject(
  50 + 'articlesearch',
  51 + 'v1',
  52 + 'by-url-no-result.php'
  53 + );
  54 +
  55 + $asMock = $this->getApiMocked('articlesearch', $responseObject);
  56 + $data = $asMock->byUrl('http://www.nytimes.com/2011/07/09/business/economy/job-growth-falters-badly-clouding-hope-for-recovery.html?hp');
  57 +
  58 + $this->assertFalse($data);
  59 + }
46 60 }
6 tests/fixtures/articlesearch/v1/by-url-no-result.php
... ... @@ -0,0 +1,6 @@
  1 +<?php
  2 +return array(
  3 + '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',
  4 + 'body' => '{"offset" : "0" , "results" : [] , "tokens" : ["url:http" , "www" , "nytimes" , "com" , "2012" , "09" , "05" , "opinion" , "why" , "mali" , "matters" , "html"] , "total" : 0}',
  5 + 'statusLine' => 'HTTP/1.1 200 OK',
  6 +);

0 comments on commit ac3d0af

Please sign in to comment.
Something went wrong with that request. Please try again.