Пример #1
0
 /**
  * @expectedException \Trivago\Tas\Exception\UnexpectedResponseException
  * @expectedExceptionMessage The response is not a valid JSON response.
  */
 public function test_invalid_json()
 {
     $request = new RawRequest('http://foo/bar/?request=1');
     $httpHandler = $this->prophesize(HttpHandler::class);
     $httpHandler->sendRequest(Argument::type(HttpRequest::class))->willReturn(new Response('no valid json', 200, ['content-type' => 'text/html']));
     $config = new Config([Config::API_KEY => '1234', Config::HTTP_HANDLER => $httpHandler->reveal(), Config::GET_TRACKING_ID_CALLBACK => function () {
     }, Config::STORE_TRACKING_ID_CALLBACK => function ($trackingId) {
     }]);
     $client = new Client($config);
     $client->sendRequest($request);
 }
Пример #2
0
 public function __call($name, $arguments)
 {
     if (!isset(static::$responseMap[$name])) {
         throw new \BadMethodCallException("Method {$name} does not exist.");
     }
     $request = $arguments[0];
     $requestClass = static::$responseMap[$name]['request'];
     if (!$request instanceof static::$responseMap[$name]['request']) {
         throw new \InvalidArgumentException("Invalid Request: Expected {$requestClass} but got " . get_class($request));
     }
     $response = $this->client->sendRequest($request);
     $httpStatus = $response->getHttpCode();
     if ($httpStatus >= 200 && $httpStatus <= 299) {
         return call_user_func([static::$responseMap[$name]['response'], 'fromResponse'], $response);
     }
     $data = $response->getContentAsArray();
     throw new ProblemException(isset($data['type']) ? $data['type'] : '', isset($data['title']) ? $data['title'] : '', isset($data['detail']) ? $data['detail'] : '', isset($data['code']) ? $data['code'] : '', $response);
 }