/** * Create service with name * * @param ServiceLocatorInterface $serviceLocator * @param $name * @param $requestedName * @return mixed */ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { $config = $this->getConfig($serviceLocator)[$requestedName]; $resourceName = $config['resource_name']; /** @var $httpClient Client */ $httpClient = isset($config['http_client']) && $serviceLocator->has($config['http_client']) ? $serviceLocator->get($config['http_client']) : null; /** @var $baseRequest Request */ $baseRequest = isset($config['base_request']) && $serviceLocator->has($config['base_request']) ? $serviceLocator->get($config['base_request']) : null; $restClient = new RestClient($resourceName, $httpClient, $baseRequest); if (isset($config['uri_resource_strategy']) && $serviceLocator->has($config['uri_resource_strategy'])) { $restClient->setUriResourceStrategy($serviceLocator->get($config['uri_resource_strategy'])); } // Array of int code valid if (isset($config['valid_status_code']) && is_array($config['valid_status_code'])) { $restClient->setValidStatusCodes($config['valid_status_code']); } // string json/xml if (isset($config['request_format'])) { $restClient->setRequestFormat($config['request_format']); } // Profiler if (isset($config['profiler']) && $serviceLocator->has($config['profiler'])) { $restClient->setProfiler($serviceLocator->get($config['profiler'])); } return $restClient; }
public function testGetItems() { $resultSet = new ArrayObjectResultSet(); $this->modelMock->expects($this->atLeastOnce())->method('getDataGateway')->willReturn($this->restClient); $count = 0; $this->modelMock->expects($this->atMost(1))->method('find')->with($this->callback(function ($c) use(&$count) { // Due to phpunit bug, this callback is called multiple time $count++; if ($count > 1) { return true; } $ok = $c instanceof FindAllCriteria && $c->getLimit() === 20 && $c->getOffset() === 30; return $ok; }))->willReturn($resultSet); $decoderMock = $this->getMockBuilder('\\Matryoshka\\Service\\Api\\Response\\Decoder\\DecoderInterface')->disableOriginalConstructor()->setMethods(['getLastResponseData'])->getMockForAbstractClass(); $decoderMock->expects($this->atMost(1))->method('getLastPayload')->willReturn(['total_items' => 1000]); /** @var $decoderMock DecoderInterface */ $this->restClient->setResponseDecoder($decoderMock); $this->assertSame($resultSet, $this->paginatorAdapter->getItems(30, 20)); $this->assertCount(1000, $this->paginatorAdapter); }
public function testPrepareRequestShouldThrowExceptionOnInvalidFormat() { $this->restClient->setRequestFormat('invalid format'); $this->setExpectedException('\\Matryoshka\\Service\\Api\\Exception\\InvalidFormatException'); $this->restClient->prepareRequest('post', null, ['foo' => 'baz']); }