/** * @dataProvider getDataErrorResponses */ public function testError(array $response, $msg) { if ($msg) { $this->setExpectedException('RuntimeException', $msg); } $this->_sut->checkForApiResponseError($response); }
private function _findMaxResults($minimumPage, $maximumPage) { $currentPage = floor(($maximumPage + $minimumPage) / 2); if ($this->_shouldLog) { $this->_logDebug(sprintf('Starting new iteration. Minimum page is <code>%d</code>, maximum page is <code>%d</code>, current page is <code>%d</code>', $minimumPage, $maximumPage, $currentPage)); } $url = $this->_urlFetched->getClone(); $query = $url->getQuery(); $query->set('fields', 'id'); $query->set('limit', 100); $query->set('page', $currentPage); $result = $this->_apiUtility->getDecodedApiResponse($url); $currentPageCount = count($this->_arrayReader->getAsArray($result, 'list')); $hasMore = $this->_arrayReader->getAsBoolean($result, 'has_more'); unset($url); unset($query); unset($result); if ($this->_shouldLog) { $this->_logDebug(sprintf('Page <code>%d</code> contains <code>%d</code> videos and <code>has_more</code> is <code>%s</code>', $currentPage, $currentPageCount, $hasMore ? 'true' : 'false')); } /* * There are 4 possible cases here: * * 1. We have over 10,000 results. * 2. Middle page is the last page in the result set. * 3. Middle page overshoots the last page. * 4. Middle page undershoots the last page. */ if ($hasMore) { //scenario 1 if (intval($currentPage) === 100) { if ($this->_shouldLog) { $this->_logDebug('There are over 10K videos in this result set.'); } // we've hit the max return 10000; } if ($this->_shouldLog) { $this->_logDebug('We have undershot the last page in the result set.'); } //scenario 4 return $this->_findMaxResults($currentPage + 1, $maximumPage); } // scenario 2 if ($currentPageCount > 0) { if ($this->_shouldLog) { $this->_logDebug('Looks like this was the last page in the result set.'); } return ($currentPage - 1) * 100 + $currentPageCount; } if ($this->_shouldLog) { $this->_logDebug('We have overshot the last page in the result set.'); } // scenario 3 return $this->_findMaxResults($minimumPage, $currentPage - 1); }
public function getValueMap() { if (!isset($this->_cache)) { $url = $this->_urlFactory->fromString($this->_urlAsString); $response = $this->_apiUtility->getDecodedApiResponse($url); $list = $this->_arrayReader->getAsArray($response, 'list'); $this->_cache = array(); foreach ($list as $entry) { if (!isset($entry[$this->_codeKey])) { continue; } $code = $entry[$this->_codeKey]; $displayName = $this->getDisplayNameFromCode($code, $entry); if ($displayName) { $this->_cache[$code] = "{$code} - {$displayName}"; } } ksort($this->_cache); $this->_cache = array_merge(array('none' => 'select ...'), $this->_cache); } return $this->_cache; }