diff --git a/README.md b/README.md index 376ef03..14c620e 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,6 @@ other configuration settings. Make sure to remove `.dist` from your file.Your `n [ - 'service_url' => 'https://api.nytimes.com', - 'version' => 'v3', - 'format' => 'json', 'api_key' => '', ] ]; @@ -57,8 +54,12 @@ Calling from your code: $request->setList('hardcover-fiction') ->setSortOrder(AbstractRequest::SORT_ORDER_ASC); - $records = $this->getService()->bestSellerList($request); + $result = $this->getService()->bestSellerList($request); + $numberOfRecords = $result->toArray()['num_results]; + var_dump($numberOfRecords); + + $records= $result->toArray()['results]; var_dump($records); ``` diff --git a/config/module.config.php b/config/module.config.php index 382c835..e21ecaf 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -72,7 +72,259 @@ ) ) ) - ) + ), + 'nytimes_service' => [ + 'description' => [ + 'baseUri' => 'https://api.nytimes.com', + 'apiVersion' => 'v3', + 'operations' => [ + 'lists_command' => [ + 'httpMethod' => 'GET', + 'uri' => '/svc/books/v3/lists.json', + 'responseModel' => 'getResponse', + 'parameters' => [ + 'api-key' => [ + 'type' => 'string', + 'required' => true, + 'location' => 'query' + ], + 'list' => [ + 'type' => 'string', + 'location' => 'query' + ], + 'weeks-on-list' => [ + 'type' => 'integer', + 'location' => 'query' + ], + 'bestsellers-date' => [ + 'type' => 'date-time', + 'required' => false, + 'location' => 'query', + ], + 'date' => [ + 'type' => 'string', + 'required' => false, + 'location' => 'query' + ], + 'isbn' => [ + 'type' => 'string', + 'location' => 'query' + ], + 'published-date' => [ + 'type' => 'string', + 'required' => false, + 'location' => 'query' + ], + 'rank' => [ + 'type' => 'integer', + 'location' => 'query' + ], + 'rank-last-week' => [ + 'type' => 'integer', + 'location' => 'query' + ], + 'offset' => [ + 'type' => 'integer', + 'location' => 'query' + ], + 'sort-order' => [ + 'type' => 'string', + 'location' => 'query' + ] + ] + ], + 'history_command' => [ + 'httpMethod' => 'GET', + 'uri' => '/svc/books/v3/lists/best-sellers/history.json', + 'responseModel' => 'getResponse', + 'parameters' => [ + 'age-group' => [ + "location" => "query", + "description" => "The target age group for the best seller.", + "type" => "string" + ], + 'author' => [ + "location" => "query", + "description" => "The author of the best seller. The author field does not include additional contributors (see Data Structure for more details about the author and contributor fields).\n\nWhen searching the author field, you can specify any combination of first, middle and last names.\n\nWhen sort-by is set to author, the results will be sorted by author's first name. ", + "type" => "string" + ], + 'contributor' => [ + "name" => "contributor", + "location" => "query", + "description" => "The author of the best seller, as well as other contributors such as the illustrator (to search or sort by author name only, use author instead).\n\nWhen searching, you can specify any combination of first, middle and last names of any of the contributors.\n\nWhen sort-by is set to contributor, the results will be sorted by the first name of the first contributor listed. ", + "type" => "string" + ], + 'isbn' => [ + "location" => "query", + "description" => "International Standard Book Number, 10 or 13 digits\n\nA best seller may have both 10-digit and 13-digit ISBNs, and may have multiple ISBNs of each type. To search on multiple ISBNs, separate the ISBNs with semicolons (example: 9780446579933;0061374229).", + "type" => "string" + ], + 'price' => [ + "location" => "query", + "description" => "The publisher's list price of the best seller, including decimal point", + "type" => "string" + ], + 'publisher' => [ + "location" => "query", + "description" => "The standardized name of the publisher", + "type" => "string" + ], + 'title' => [ + "location" => "query", + "description" => "The title of the best seller\n\nWhen searching, you can specify a portion of a title or a full title.", + "type" => "string" + ], + 'api-key' => [ + "location" => "query", + 'required' => true, + "description" => "api key", + "type" => "string" + ] + ] + ], + 'list_names_command' => [ + 'httpMethod' => 'GET', + 'uri' => '/svc/books/v3/lists/names.json', + 'responseModel' => 'getResponse', + 'parameters' => [ + 'api-key' => [ + 'type' => 'string', + 'required' => true, + 'location' => 'query' + ] + ] + ], + 'overview_command' => [ + 'httpMethod' => 'GET', + 'uri' => '/svc/books/v3/lists/overview.json', + 'responseModel' => 'getResponse', + 'parameters' => [ + 'api-key' => [ + 'type' => 'string', + 'required' => true, + 'location' => 'query' + ], + 'published_date' => [ + 'type' => 'string', + 'location' => 'query' + ], + ] + ], + 'lists_by_date_command' => [ + 'httpMethod' => 'GET', + 'uri' => '/svc/books/v3/lists/{date}/{list}.json', + 'responseModel' => 'getResponse', + 'parameters' => [ + 'api-key' => [ + 'type' => 'string', + 'required' => true, + 'location' => 'query' + ], + 'date' => [ + 'type' => 'string', + 'required' => true, + 'location' => 'uri' + ], + 'list' => [ + 'type' => 'string', + 'required' => true, + 'location' => 'uri' + ], + 'isbn' => [ + 'type' => 'integer', + 'location' => 'query' + ], + 'list-name' => [ + 'type' => 'string', + 'location' => 'query' + ], + 'published-date' => [ + 'type' => 'date-time', + 'location' => 'query' + ], + 'bestsellers-date' => [ + 'type' => 'string', + 'location' => 'query' + ], + 'weeks-on-list' => [ + 'type' => 'integer', + 'location' => 'query' + ], + 'rank' => [ + 'type' => 'string', + 'location' => 'query' + ], + 'rank-last-week' => [ + 'type' => 'integer', + 'location' => 'query' + ], + 'offset' => [ + 'type' => 'integer', + 'location' => 'query' + ], + 'sort-order' => [ + 'type' => 'string', + 'location' => 'query' + ], + ], + ], + 'reviews_command' => [ + 'httpMethod' => 'GET', + 'uri' => '/svc/books/v3/reviews.json', + 'responseModel' => 'getResponse', + 'parameters' => [ + 'api-key' => [ + 'type' => 'string', + 'required' => true, + 'location' => 'query' + ], + 'isbn' => [ + 'type' => 'integer', + 'location' => 'query' + ], + 'title' => [ + 'type' => 'string', + 'location' => 'query' + ], + 'author' => [ + 'type' => 'string', + 'location' => 'query' + ] + ] + ] + ], + 'models' => [ + 'getResponse' => [ + 'type' => 'object', + "properties" => [ + "success" => [ + "type" => "string", + "required" => true + ], + "errors" => [ + "type" => "array", + "items" => [ + "type" => "object", + "properties" => [ + "code" => [ + "type" => "string", + "description" => "The error code." + ], + "message" => [ + "type" => "string", + "description" => "The detailed message from the server." + ] + ] + ] + ] + ], + 'additionalProperties' => [ + 'location' => 'json' + ] + ] + ] + ] + ] ); diff --git a/config/nytimes.local.php.dist b/config/nytimes.local.php.dist index bad5593..2a31b50 100644 --- a/config/nytimes.local.php.dist +++ b/config/nytimes.local.php.dist @@ -1,9 +1,6 @@ [ - 'service_url' => 'https://api.nytimes.com', - 'version' => 'v3', - 'format' => 'json', 'api_key' => '', ] ]; diff --git a/src/Controller/ConsoleController.php b/src/Controller/ConsoleController.php index 8bdbf79..3362593 100644 --- a/src/Controller/ConsoleController.php +++ b/src/Controller/ConsoleController.php @@ -32,7 +32,9 @@ public function listsAction() $request->setSortOrder(AbstractRequest::SORT_ORDER_ASC); - $records = $this->getService()->bestSellerList($request); + $result = $this->getService()->bestSellerList($request); + + $records = $result->toArray()['results']; var_dump($records); @@ -52,7 +54,9 @@ public function historyAction() $request->setAuthor('Michael Connelly'); } - $records = $this->getService()->bestSellerHistoryList($request); + $result = $this->getService()->bestSellerHistoryList($request); + + $records = $result->toArray()['results']; var_dump($records); @@ -65,7 +69,9 @@ public function listNamesAction() $request = new ListNames(); - $records = $this->getService()->bestSellerListNames($request); + $result = $this->getService()->bestSellerListNames($request); + + $records = $result->toArray()['results']; var_dump($records); @@ -79,7 +85,9 @@ public function overviewAction() $request = new Overview(); $request->setPublishedDate('2016-01-10'); - $records = $this->getService()->bestSellerListOverview($request); + $result = $this->getService()->bestSellerListOverview($request); + + $records = $result->toArray()['results']; var_dump($records); @@ -106,7 +114,9 @@ public function listByDateAction() $request->setDate('2016-10-21'); } - $records = $this->getService()->bestSellerListByDate($request); + $result = $this->getService()->bestSellerListByDate($request); + + $records = $result->toArray()['results']; var_dump($records); @@ -126,7 +136,10 @@ public function reviewsAction() $request->setAuthor('John Grisham'); } - $records = $this->getService()->reviews($request); + $result = $this->getService()->reviews($request); + + $records = $result->toArray()['results']; + var_dump($records); diff --git a/src/Request/Books/Search.php b/src/Request/Books/Search.php deleted file mode 100644 index 62cc76e..0000000 --- a/src/Request/Books/Search.php +++ /dev/null @@ -1,265 +0,0 @@ -setListName($args['list-name']); - - if (isset($args['date'])) { - $this->setDate($args['date']); - } - if (isset($args['isbn'])) { - $this->setIsbn($args['isbn']); - } - - if (isset($args['list'])) { - $this->setList($args['list']); - } - if (isset($args['published-date'])) { - $this->setPublishedDate($args['published-date']); - } - if (isset($args['rank'])) { - $this->setRank($args['rank']); - } - if (isset($args['rank-last-week'])) { - $this->setRankLastWeek($args['rank-last-week']); - } - if (isset($args['weeks-on-list'])) { - $this->setWeeksOnList($args['weeks-on-list']); - } - if (isset($args['sort-by'])) { - $this->setSortBy($args['sort-by']); - } - } - - - public function toArray() - { - return array_merge(parent::toArray(), array( - 'list-name' => $this->getListName(), - 'date' => $this->getDate(), - 'isbn' => $this->getIsbn(), - 'list' => $this->getList(), - 'published-date' => $this->getPublishedDate(), - 'rank' => $this->getRank(), - 'rank-last-week' => $this->getRankLastWeek(), - 'weeks-on-list' => $this->getWeeksOnList(), - 'sort-by' => $this->getSortBy() - )); - } - - - /** - * - * @return $listName - */ - public function getListName() - { - return $this->listName; - } - - - /** - * - * @return $date - */ - public function getDate() - { - return $this->date; - } - - - /** - * - * @return $isbn - */ - public function getIsbn() - { - return $this->isbn; - } - - - /** - * - * @return $list - */ - public function getList() - { - return $this->list; - } - - - /** - * - * @return $publishedDate - */ - public function getPublishedDate() - { - return $this->publishedDate; - } - - - /** - * - * @return $rank - */ - public function getRank() - { - return $this->rank; - } - - - /** - * - * @return $rankLastWeek - */ - public function getRankLastWeek() - { - return $this->rankLastWeek; - } - - - /** - * - * @return $weeksOnList - */ - public function getWeeksOnList() - { - return $this->weeksOnList; - } - - - /** - * - * @return $sortBy - */ - public function getSortBy() - { - return $this->sortBy; - } - - - /** - * - * @param $listName - */ - public function setListName($listName) - { - $this->listName = $listName; - } - - - /** - * - * @param $date - */ - public function setDate($date) - { - $this->date = $date; - } - - - /** - * - * @param $isbn - */ - public function setIsbn($isbn) - { - $this->isbn = $isbn; - } - - - /** - * - * @param $list - */ - public function setList($list) - { - $this->list = $list; - } - - - /** - * - * @param $publishedDate - */ - public function setPublishedDate($publishedDate) - { - $this->publishedDate = $publishedDate; - } - - - /** - * - * @param $rank - */ - public function setRank($rank) - { - $this->rank = $rank; - } - - - /** - * - * @param $rankLastWeek - */ - public function setRankLastWeek($rankLastWeek) - { - $this->rankLastWeek = $rankLastWeek; - } - - - /** - * - * @param $weeksOnList - */ - public function setWeeksOnList($weeksOnList) - { - $this->weeksOnList = $weeksOnList; - } - - - /** - * - * @param $sortBy - */ - public function setSortBy($sortBy) - { - $this->sortBy = $sortBy; - } -} \ No newline at end of file diff --git a/src/Service/BooksService.php b/src/Service/BooksService.php index afce8e7..efd788f 100644 --- a/src/Service/BooksService.php +++ b/src/Service/BooksService.php @@ -1,35 +1,29 @@ isValid($request)) { return $validator->getMessages(); } - return $this->getClient()->lists(array_filter($request->toArray())); + + return $this->lists_command(array_filter($request->toArray())); } /** @@ -68,7 +63,7 @@ public function bestSellerHistoryList(History $request) return $validator->getMessages(); } - return $this->getClient()->history(array_filter($request->toArray())); + return $this->history_command(array_filter($request->toArray())); } /** @@ -84,7 +79,7 @@ public function bestSellerListNames(ListNames $request) return $validator->getMessages(); } - return $this->getClient()->listNames(array_filter($request->toArray())); + return $this->list_names_command(array_filter($request->toArray())); } /** @@ -100,7 +95,7 @@ public function bestSellerListOverview(Overview $request) return $validator->getMessages(); } - return $this->getClient()->overview(array_filter($request->toArray())); + return $this->overview_command(array_filter($request->toArray())); } /** @@ -116,7 +111,7 @@ public function bestSellerListByDate(Lists $request) return $validator->getMessages(); } - return $this->getClient()->listsByDate(array_filter($request->toArray())); + return $this->lists_by_date_command(array_filter($request->toArray())); } /** @@ -132,304 +127,7 @@ public function reviews(Reviews $request) return $validator->getMessages(); } - return $this->getClient()->reviews(array_filter($request->toArray())); - } - - protected function pathHelper($path) - { - if (!$this->getServiceUrl() || !$this->getVersion() || !$this->getFormat()) { - throw new \Exception('Required Parameter is not set'); - } - - // prepend service path and append response format - return sprintf('/svc/books/%s/%s.%s', $this->getVersion(), $path, $this->getFormat()); - - } - - protected function getClient() - { - - $client = new Client(); - $description = new Description([ - 'apiVersion' => $this->getVersion(), - 'baseUri' => $this->getServiceUrl(), - 'operations' => [ - 'lists' => [ - 'httpMethod' => 'GET', - 'uri' => $this->pathHelper('lists'), - 'responseModel' => 'getResponse', - 'parameters' => [ - 'api-key' => [ - 'type' => 'string', - 'required' => true, - 'location' => 'query' - ], - 'list' => [ - 'type' => 'string', - 'location' => 'query' - ], - 'weeks-on-list' => [ - 'type' => 'integer', - 'location' => 'query' - ], - 'bestsellers-date' => [ - 'type' => 'date-time', - 'required' => false, - 'location' => 'query', - ], - 'date' => [ - 'type' => 'string', - 'required' => false, - 'location' => 'query' - ], - 'isbn' => [ - 'type' => 'string', - 'location' => 'query' - ], - 'published-date' => [ - 'type' => 'string', - 'required' => false, - 'location' => 'query' - ], - 'rank' => [ - 'type' => 'integer', - 'location' => 'query' - ], - 'rank-last-week' => [ - 'type' => 'integer', - 'location' => 'query' - ], - 'offset' => [ - 'type' => 'integer', - 'location' => 'query' - ], - 'sort-order' => [ - 'type' => 'string', - 'location' => 'query' - ] - ] - ], - 'history' => [ - 'httpMethod' => 'GET', - 'uri' => $this->pathHelper('lists/best-sellers/history'), - 'responseModel' => 'getResponse', - 'parameters' => [ - 'age-group' => [ - "location" => "query", - "description" => "The target age group for the best seller.", - "type" => "string" - ], - 'author' => [ - "location" => "query", - "description" => "The author of the best seller. The author field does not include additional contributors (see Data Structure for more details about the author and contributor fields).\n\nWhen searching the author field, you can specify any combination of first, middle and last names.\n\nWhen sort-by is set to author, the results will be sorted by author's first name. ", - "type" => "string" - ], - 'contributor' => [ - "name" => "contributor", - "location" => "query", - "description" => "The author of the best seller, as well as other contributors such as the illustrator (to search or sort by author name only, use author instead).\n\nWhen searching, you can specify any combination of first, middle and last names of any of the contributors.\n\nWhen sort-by is set to contributor, the results will be sorted by the first name of the first contributor listed. ", - "type" => "string" - ], - 'isbn' => [ - "location" => "query", - "description" => "International Standard Book Number, 10 or 13 digits\n\nA best seller may have both 10-digit and 13-digit ISBNs, and may have multiple ISBNs of each type. To search on multiple ISBNs, separate the ISBNs with semicolons (example: 9780446579933;0061374229).", - "type" => "string" - ], - 'price' => [ - "location" => "query", - "description" => "The publisher's list price of the best seller, including decimal point", - "type" => "string" - ], - 'publisher' => [ - "location" => "query", - "description" => "The standardized name of the publisher", - "type" => "string" - ], - 'title' => [ - "location" => "query", - "description" => "The title of the best seller\n\nWhen searching, you can specify a portion of a title or a full title.", - "type" => "string" - ], - 'api-key' => [ - "location" => "query", - 'required' => true, - "description" => "api key", - "type" => "string" - ] - ] - ], - 'listNames' => [ - 'httpMethod' => 'GET', - 'uri' => $this->pathHelper('lists/names'), - 'responseModel' => 'getResponse', - 'parameters' => [ - 'api-key' => [ - 'type' => 'string', - 'required' => true, - 'location' => 'query' - ] - ] - ], - 'overview' => [ - 'httpMethod' => 'GET', - 'uri' => $this->pathHelper('lists/overview'), - 'responseModel' => 'getResponse', - 'parameters' => [ - 'api-key' => [ - 'type' => 'string', - 'required' => true, - 'location' => 'query' - ], - 'published_date' => [ - 'type' => 'string', - 'location' => 'query' - ], - ] - ], - 'listsByDate' => [ - 'httpMethod' => 'GET', - 'uri' => $this->pathHelper('lists/{date}/{list}'), - 'responseModel' => 'getResponse', - 'parameters' => [ - 'api-key' => [ - 'type' => 'string', - 'required' => true, - 'location' => 'query' - ], - 'date' => [ - 'type' => 'string', - 'required' => true, - 'location' => 'uri' - ], - 'list' => [ - 'type' => 'string', - 'required' => true, - 'location' => 'uri' - ], - 'isbn' => [ - 'type' => 'integer', - 'location' => 'query' - ], - 'list-name' => [ - 'type' => 'string', - 'location' => 'query' - ], - 'published-date' => [ - 'type' => 'date-time', - 'location' => 'query' - ], - 'bestsellers-date' => [ - 'type' => 'string', - 'location' => 'query' - ], - 'weeks-on-list' => [ - 'type' => 'integer', - 'location' => 'query' - ], - 'rank' => [ - 'type' => 'string', - 'location' => 'query' - ], - 'rank-last-week' => [ - 'type' => 'integer', - 'location' => 'query' - ], - 'offset' => [ - 'type' => 'integer', - 'location' => 'query' - ], - 'sort-order' => [ - 'type' => 'string', - 'location' => 'query' - ], - ], - ], - 'reviews' => [ - 'httpMethod' => 'GET', - 'uri' => $this->pathHelper('reviews'), - 'responseModel' => 'getResponse', - 'parameters' => [ - 'api-key' => [ - 'type' => 'string', - 'required' => true, - 'location' => 'query' - ], - 'isbn' => [ - 'type' => 'integer', - 'location' => 'query' - ], - 'title' => [ - 'type' => 'string', - 'location' => 'query' - ], - 'author' => [ - 'type' => 'string', - 'location' => 'query' - ], - ] - ], - ], - 'models' => [ - 'getResponse' => [ - 'type' => 'object', - 'additionalProperties' => [ - 'location' => 'json' - ] - ] - ] - ]); - - $guzzleClient = new GuzzleClient($client, $description); - return $guzzleClient; - } - - - /** - * @return string - */ - public function getServiceUrl() - { - return $this->serviceUrl; - } - - /** - * @param string $serviceUrl - */ - public function setServiceUrl($serviceUrl) - { - $this->serviceUrl = $serviceUrl; - } - - /** - * @return string - */ - public function getVersion() - { - return $this->version; - } - - /** - * @param string $version - */ - public function setVersion($version) - { - $this->version = $version; - } - - /** - * @return string - */ - public function getFormat() - { - return $this->format; - } - - /** - * @param string $format - */ - public function setFormat($format) - { - $this->format = $format; + return $this->reviews_command(array_filter($request->toArray())); } @@ -465,5 +163,4 @@ public function setRequestValidator($requestValidator) $this->requestValidator = $requestValidator; } - } \ No newline at end of file diff --git a/src/Service/Factory/BooksServiceFactory.php b/src/Service/Factory/BooksServiceFactory.php index 2a26003..c88938a 100644 --- a/src/Service/Factory/BooksServiceFactory.php +++ b/src/Service/Factory/BooksServiceFactory.php @@ -3,8 +3,10 @@ use Firelike\NYTimes\Service\BooksService; +use GuzzleHttp\Command\Guzzle\Description; use Zend\ServiceManager\Factory\FactoryInterface; use Interop\Container\ContainerInterface; +use GuzzleHttp\Client; class BooksServiceFactory implements FactoryInterface @@ -13,29 +15,27 @@ class BooksServiceFactory implements FactoryInterface public function __invoke(ContainerInterface $sm, $requestedName, array $options = null) { - $service = new BooksService(); - $config = $sm->get('Config'); - if (isset($config['nytimes_service'])) { + if (!isset($config['nytimes_service'])) { + throw new \Exception('Required configuration node - nytimes_service is missing'); + } + + if (!isset($config['nytimes_service']['api_key'])) { + throw new \Exception('Required nytimes_service configuration parameters are missing is missing'); + } - if (isset($config['nytimes_service']['service_url'])) { - $service->setServiceUrl($config['nytimes_service']['service_url']); - } + if (!isset($config['nytimes_service']['description'])) { + throw new \Exception('Required nytimes_service configuration parameters are missing is missing'); + } - if (isset($config['nytimes_service']['version'])) { - $service->setVersion($config['nytimes_service']['version']); - } + $client = new Client(); - if (isset($config['nytimes_service']['format'])) { - $service->setFormat($config['nytimes_service']['format']); - } + $description = new Description($config['nytimes_service']['description']); - if (isset($config['nytimes_service']['api_key'])) { - $service->setApiKey($config['nytimes_service']['api_key']); - } + $service = new BooksService($client, $description); - } + $service->setApiKey($config['nytimes_service']['api_key']); $service->setRequestValidator($sm->get('Firelike\NYTimes\Validator\BooksServiceRequestValidator')); diff --git a/src/Validator/BooksServiceRequestValidator.php b/src/Validator/BooksServiceRequestValidator.php index 2920cd3..2437d35 100644 --- a/src/Validator/BooksServiceRequestValidator.php +++ b/src/Validator/BooksServiceRequestValidator.php @@ -2,7 +2,9 @@ namespace Firelike\NYTimes\Validator; +use Firelike\NYTimes\Request\AbstractRequest; use Zend\Validator\AbstractValidator; +use Zend\Validator\Isbn\Isbn13; class BooksServiceRequestValidator extends AbstractValidator { @@ -22,7 +24,7 @@ class BooksServiceRequestValidator extends AbstractValidator */ public function isValid($request) { - if (!$request instanceof \Firelike\NYTimes\Request\AbstractRequest) { + if (!$request instanceof AbstractRequest) { return false; } @@ -49,7 +51,7 @@ public function isValid($request) if (method_exists($request, 'getIsbn')) { if ($request->getIsbn()) { - $validator = new \Zend\Validator\Isbn\Isbn13(); + $validator = new Isbn13(); if (!$validator->isValid($request->getIsbn())) { $this->setMessage('Invalid ISBN-13'); return false; diff --git a/tests/Service/BooksServiceTest.php b/tests/Service/BooksServiceTest.php index 5d4a2f2..8e7239b 100644 --- a/tests/Service/BooksServiceTest.php +++ b/tests/Service/BooksServiceTest.php @@ -25,6 +25,9 @@ use Firelike\NYTimes\Validator\BooksServiceRequestValidator; use Firelike\NYTimes\Validator\OffsetValidator; use Firelike\NYTimes\Validator\SortOrderValidator; +use GuzzleHttp\Client; +use GuzzleHttp\Command\Guzzle\Description; +use GuzzleHttp\Command\ResultInterface; class BooksServiceTest extends \PHPUnit_Framework_TestCase @@ -39,18 +42,17 @@ public function setUp() { parent::setUp(); - $config = [ - 'service_url' => 'https://api.nytimes.com', - 'version' => 'v3', - 'format' => 'json', - 'api_key' => '', - ]; + $client = new Client(); - $this->service = new BooksService(); - $this->service->setServiceUrl($config['service_url']); - $this->service->setVersion($config['version']); - $this->service->setFormat($config['format']); - $this->service->setApiKey($config['api_key']); + $config = include __DIR__ . '/../../config/module.config.php'; + $description = new Description($config['nytimes_service']['description']); + + $this->service = new BooksService($client, $description); + + // to run the test set an environment variable + // with name 'nytimes_api_key' and value your NYTimes API key + $apiKey = getenv('nytimes_api_key'); + $this->service->setApiKey($apiKey); $validator = new BooksServiceRequestValidator(); $validator->setOffsetValidator(new OffsetValidator()); @@ -66,32 +68,37 @@ public function testLists() $request->setList('fiction'); $result = $this->service->bestSellerList($request); - $this->assertArrayHasKey('results', $result); + $this->assertInstanceOf(ResultInterface::class, $result); + $this->assertArrayHasKey('num_results', $result->toArray()); + $this->assertArrayHasKey('results', $result->toArray()); } public function testHistory() { $request = new History(); - $result = $this->service->bestSellerHistoryList($request); - $this->assertArrayHasKey('results', $result); + $this->assertInstanceOf(ResultInterface::class, $result); + $this->assertArrayHasKey('num_results', $result->toArray()); + $this->assertArrayHasKey('results', $result->toArray()); } public function testListNames() { $request = new ListNames(); - $result = $this->service->bestSellerListNames($request); - $this->assertArrayHasKey('results', $result); + $this->assertInstanceOf(ResultInterface::class, $result); + $this->assertArrayHasKey('num_results', $result->toArray()); + $this->assertArrayHasKey('results', $result->toArray()); } public function testOverview() { $request = new Overview(); - $result = $this->service->bestSellerListOverview($request); - $this->assertArrayHasKey('results', $result); + $this->assertInstanceOf(ResultInterface::class, $result); + $this->assertArrayHasKey('num_results', $result->toArray()); + $this->assertArrayHasKey('results', $result->toArray()); } public function testListByDate() @@ -100,7 +107,9 @@ public function testListByDate() $request->setDate('2016-10-20')->setList('hardcover-fiction'); $result = $this->service->bestSellerListByDate($request); - $this->assertArrayHasKey('results', $result); + $this->assertInstanceOf(ResultInterface::class, $result); + $this->assertArrayHasKey('num_results', $result->toArray()); + $this->assertArrayHasKey('results', $result->toArray()); } public function testReviews() @@ -109,7 +118,9 @@ public function testReviews() $request->setAuthor('Michael Connelly'); $result = $this->service->reviews($request); - $this->assertArrayHasKey('results', $result); + $this->assertInstanceOf(ResultInterface::class, $result); + $this->assertArrayHasKey('num_results', $result->toArray()); + $this->assertArrayHasKey('results', $result->toArray()); } }