/** * @dataProvider caseProvider * * @param $about * @param Service $service * @param $name * @param array $args * @param $serialized */ public function testPassesComplianceTest($about, Service $service, $name, array $args, $serialized) { $endpoint = 'https://api-staging.sellercenter.net'; $client = new SdkClient(['api' => $service, 'credentials' => new NullCredentials(), 'client' => new Client(), 'signature' => $this->getMock('SellerCenter\\SDK\\Commom\\Signature\\SignatureInterface'), 'store' => 'mobly', 'environment' => 'staging', 'endpoint' => $endpoint, 'error_parser' => Service::createErrorParser($service->getProtocol()), 'serializer' => Service::createSerializer($service, $endpoint), 'version' => 'latest']); $factory = new ClientFactory(); /* @var \SellerCenter\SDK\Common\ClientFactory $reflectionClass */ /* @var \ReflectionClass $reflectionClass */ $reflectionClass = new \ReflectionClass($factory); $reflectionMethod = $reflectionClass->getMethod('applyParser'); $reflectionMethod->setAccessible(true); $reflectionMethod->invoke($factory, $client, 'http://foo.com'); $command = $client->getCommand($name, $args); $transaction = new CommandTransaction($client, $command); /** @var callable $serializer */ $serializer = $this->readAttribute($client, 'serializer'); $request = $serializer($transaction); $this->assertEquals($serialized['uri'], $request->getResource()); $body = (string) $request->getBody(); if (!empty($body) && $service->getMetadata('type') == 'rest-xml') { $this->assertXmlStringEqualsXmlString($serialized['body'], $body); } if (isset($serialized['headers'])) { foreach ($serialized['headers'] as $key => $value) { $this->assertSame($value, $request->getHeader($key)); } } }
public function __invoke(CommandTransaction $trans) { $command = $trans->command; $operation = $this->api->getOperation($command->getName()); $args = $command->toArray(); $request = $trans->client->createRequest($operation['http']['method'], $this->buildEndpoint($operation, $args), ['config' => ['command' => $command]]); return $this->serialize($request, $operation, $args); }
/** * @dataProvider caseProvider */ public function testPassesComplianceTest(Service $service, $name, $deserialize, array $expectedResult, $response) { $parser = Service::createParser($service); // Create a response based on the serialized property of the test. $response = new Response($response['status_code'], $response['headers'], Stream::factory($response['body'])); $parsedResult = $parser($response, $deserialize); $expectedHead = new Head(); $expectedHead->setRequestId($expectedResult['Head']['RequestId']); $expectedHead->setRequestAction($expectedResult['Head']['RequestAction']); $expectedHead->setResponseType($expectedResult['Head']['ResponseType']); $expectedHead->setTimestamp(\DateTime::createFromFormat(DATE_ISO8601, $expectedResult['Head']['Timestamp'])); if (isset($expectedResult['Head']['RequestParameters']) && count($expectedResult['Head']['RequestParameters'])) { $expectedHead->setRequestParameters(new RequestParameters()); if (!empty($expectedResult['Head']['RequestParameters'])) { $expectedHead->getRequestParameters()->setFeedId($expectedResult['Head']['RequestParameters']['FeedID']); } } $expectedResponse = new $deserialize(); $expectedResponse->setHead($expectedHead); $expectedResponse->setBody($expectedResult['Body']); $this->assertEquals($expectedResponse->getHead(), $parsedResult->getHead()); $this->assertEquals($expectedResponse->getBody(), $parsedResult->getBody()); }
/** * @dataProvider errorParserProvider */ public function testCreatesRelevantErrorParsers($p, $cl) { $this->assertInstanceOf($cl, Service::createErrorParser($p)); }
/** * Creates and attaches parsers given client based on the protocol of the * description. * * @param SdkClientInterface $client SDK client to update * * @throws \UnexpectedValueException if the protocol doesn't exist */ protected function applyParser(SdkClientInterface $client) { $api = $client->getApi(); $parser = Service::createParser($api); $errorParser = Service::createErrorParser($api->getProtocol()); $client->getEmitter()->on('process', function (ProcessEvent $e) use($errorParser) { // Ensure a response exists in order to parse. $response = $e->getResponse(); if (!$response) { throw new \RuntimeException('No response was received.'); } /* @var \SellerCenter\SDK\Common\Api\ErrorParser\XmlErrorParser $errorParser */ $result = $errorParser($response, $e); if ($result instanceof Api\Response\Error\ErrorResponse) { $e->setResult($result); $e->stopPropagation(); } }); $client->getEmitter()->on('process', function (ProcessEvent $e) use($parser, $api) { // Guard against exceptions and injected results. if ($e->getException() || $e->getResult()) { return; } // Ensure a response exists in order to parse. $response = $e->getResponse(); if (!$response) { throw new \RuntimeException('No response was received.'); } $operation = $api->getOperation($e->getCommand()->getName()); $result = $parser($response, $operation['deserialize']); $e->setResult($result); }); }
public function getWaiter($name, array $args = [], array $config = []) { $config += $this->api->getWaiterConfig($name); return new ResourceWaiter($this, $name, $args, $config); }
/** * Applies the listeners needed to parse client models. * * @param Service $api API to create a parser for * @return callable * @throws \UnexpectedValueException */ public static function createParser(Service $api) { static $mapping = ['rest-xml' => 'SellerCenter\\SDK\\Common\\Api\\Parser\\RestXmlParser']; $protocol = $api->getProtocol(); if (isset($mapping[$protocol])) { return new $mapping[$protocol]($api); } else { throw new \UnexpectedValueException('Unknown protocol: ' . $api->getProtocol()); } }