/** * Create service with name * * @param ServiceLocatorInterface $serviceLocator * @param $name * @param $requestedName * @return mixed */ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { $allConfigs = $this->getConfig($serviceLocator); $config = $allConfigs[$requestedName]; $httpClient = new HttpClient(); if (isset($config['http_client_options'])) { $httpClient->setOptions($config['http_client_options']); } $client = new Client($httpClient, $config['base_uri']); if (isset($config['default_headers'])) { $client->setDefaultHeaders($config['default_headers']); } if (isset($config['throw_exceptions'])) { $client->setThrowExceptions($config['throw_exceptions']); } return $client; }
public function testFetchInvalidStatusCodeThrowsException() { $request = new Request(); $request->setUri('http://test.dev/testapi/17'); $request->setMethod(Request::METHOD_GET); $request->getHeaders()->addHeaders(array('Accept' => 'application/json')); $response = new Response(); $response->setContent(json_encode(['type' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html', 'title' => 'Forbidden', 'status' => 403, 'detail' => 'Forbidden'])); $httpClient = $this->getMock('Zend\\Http\\Client', array('send')); $httpClient->expects($this->once())->method('send')->with($this->equalTo($request))->will($this->returnValue($response)); $client = new Client($httpClient, 'http://test.dev/', ['Accept' => 'application/json']); $client->setThrowExceptions(true); $this->expectException('Jolicht\\ApigilityClient\\ClientException'); $this->expectExceptionCode(403); $this->expectExceptionMessage('Forbidden'); $client->fetch('/testapi', 17); }