/** * @test */ public function shouldReturnNullPaginationOnImproperResponse() { $mockResponse = array(); $listenerResponse = $this->getMock('Guzzle\\Http\\Message\\Response', array(), array(200)); $listenerResponse->expects($this->once())->method('getBody')->with(TRUE)->will($this->returnValue(json_encode($mockResponse))); $this->assertSame(NULL, ResponseMediator::getPagination($listenerResponse)); }
/** * {@inheritDoc} */ public function onRequestError(Event $event) { /** @var $request \Guzzle\Http\Message\Request */ $request = $event['request']; $response = $request->getResponse(); if ($response->isClientError() || $response->isServerError()) { $content = ResponseMediator::getContent($response); if (is_array($content)) { $error = $content[0]; if (400 == $response->getStatusCode()) { if (isset($error['property']) && isset($error['requirement'])) { throw new ErrorException(sprintf('%s - %s (%s)', $error['type'], $error['property'], $error['requirement']['type']), 400); } else { throw new ErrorException(sprintf('%s - %s', $error['type'], $error['message'] ? $error['message'] : json_encode($content)), 400); } } } else { throw new RuntimeException(json_encode($content), $response->getStatusCode()); } } }
/** * @test */ public function shouldHandlePagination() { $url = 'http://example.com'; $path = '/some/path'; $body = 'a = b'; $headers = array('c' => 'd'); $response = new Response(200, NULL, json_encode(array('page' => 2, 'pageSize' => 10, 'total' => 30))); $response->setInfo(array('url' => $url)); $expected_pagination = array('first' => array($url => array('page' => 1, 'count' => 10)), 'last' => array($url => array('page' => 3, 'count' => 10)), 'next' => array($url => array('page' => 3, 'count' => 10)), 'prev' => array($url => array('page' => 1, 'count' => 10))); $client = $this->getBrowserMock(); $httpClient = new HttpClient(array(), $client); $httpClient->request($path, $body, 'HEAD', $headers); $this->assertEquals($expected_pagination, ResponseMediator::getPagination($response)); }
/** * {@inheritdoc} */ protected function get($key) { if ($this->has($key)) { $url = key($this->pagination[$key]); $result = $this->client->getHttpClient()->get($url, $this->pagination[$key][$url]); $this->postFetch(); return ResponseMediator::getContent($result); } }
/** * Sends a DELETE request with JSON-encoded parameters. * * @param string $path * The request path. * * @param array $parameters * The POST parameters to be JSON encoded. * * @param array $requestHeaders * The request headers. */ protected function delete($path, array $parameters = array(), $requestHeaders = array()) { $response = $this->client->getHttpClient()->delete($path, $this->createJsonBody($parameters), $requestHeaders); return ResponseMediator::getContent($response); }