Exemplo n.º 1
0
 public function testSend()
 {
     $self = $this;
     $eventsDispatched = [];
     $history = new History();
     $mock = new Mock();
     $mockRequestData = ['foo' => 'bar', 'token' => self::TOKEN];
     $mockResponseData = ['ok' => true, 'foo' => 'bar'];
     $mockPayload = new MockPayload();
     $mockPayload->setFoo('bar');
     $mockResponseBody = json_encode($mockResponseData);
     $mock->addResponse(sprintf("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", strlen($mockResponseBody), $mockResponseBody));
     $client = new Client();
     $client->getEmitter()->attach($history);
     $client->getEmitter()->attach($mock);
     $apiClient = new ApiClient(self::TOKEN, $client);
     $apiClient->addRequestListener(function (RequestEvent $event) use(&$eventsDispatched, $mockRequestData, $self) {
         $eventsDispatched[ApiClient::EVENT_REQUEST] = true;
         $self->assertEquals($mockRequestData, $event->getRawPayload());
     });
     $apiClient->addResponseListener(function (ResponseEvent $event) use(&$eventsDispatched, $mockResponseData, $self) {
         $eventsDispatched[ApiClient::EVENT_RESPONSE] = true;
         $self->assertEquals($mockResponseData, $event->getRawPayloadResponse());
     });
     $apiClient->send($mockPayload);
     $lastRequest = $history->getLastRequest();
     $expectedUrl = ApiClient::API_BASE_URL . 'mock';
     parse_str($lastRequest->getBody()->__toString(), $lastRequestContent);
     $lastResponseContent = json_decode($history->getLastResponse()->getBody(), true);
     $this->assertEquals($mockRequestData, $lastRequestContent);
     $this->assertEquals($mockResponseData, $lastResponseContent);
     $this->assertEquals($expectedUrl, $history->getLastRequest()->getUrl());
     $this->assertArrayHasKey(ApiClient::EVENT_REQUEST, $eventsDispatched);
     $this->assertArrayHasKey(ApiClient::EVENT_RESPONSE, $eventsDispatched);
 }
Exemplo n.º 2
0
 public function testSend()
 {
     $self = $this;
     $mockRequestData = ['foo' => 'bar', 'token' => self::TOKEN];
     $mockResponseData = ['ok' => true, 'foo' => 'bar'];
     $handler = HandlerStack::create(new MockHandler([new Response(200, [], json_encode($mockResponseData))]));
     $historyContainer = [];
     $history = Middleware::history($historyContainer);
     $handler->push($history);
     $apiClient = new ApiClient(self::TOKEN, new Client(['handler' => $handler]));
     $apiClient->addRequestListener(function (RequestEvent $event) use(&$eventsDispatched, $mockRequestData, $self) {
         $eventsDispatched[ApiClient::EVENT_REQUEST] = true;
         $self->assertEquals($mockRequestData, $event->getRawPayload());
     });
     $apiClient->addResponseListener(function (ResponseEvent $event) use(&$eventsDispatched, $mockResponseData, $self) {
         $eventsDispatched[ApiClient::EVENT_RESPONSE] = true;
         $self->assertEquals($mockResponseData, $event->getRawPayloadResponse());
     });
     $mockPayload = new MockPayload();
     $mockPayload->setFoo('bar');
     $apiClient->send($mockPayload);
     $transaction = $historyContainer[0];
     $requestUrl = (string) $transaction['request']->getUri();
     $requestContentType = $transaction['request']->getHeader('content-type')[0];
     parse_str($transaction['request']->getBody(), $requestBody);
     $responseBody = json_decode($transaction['response']->getBody(), true);
     $this->assertEquals(ApiClient::API_BASE_URL . 'mock', $requestUrl);
     $this->assertEquals('application/x-www-form-urlencoded', $requestContentType);
     $this->assertEquals($mockRequestData, $requestBody);
     $this->assertEquals($mockResponseData, $responseBody);
     $this->assertArrayHasKey(ApiClient::EVENT_REQUEST, $eventsDispatched);
     $this->assertArrayHasKey(ApiClient::EVENT_RESPONSE, $eventsDispatched);
 }
 public function testSerialize()
 {
     $payload = new MockPayload();
     $payload->setFoo('bar');
     $serializedPayload = $this->payloadSerializer->serialize($payload);
     $this->assertInternalType('array', $serializedPayload);
     $this->assertArrayHasKey('foo', $serializedPayload);
     $this->assertEquals('bar', $serializedPayload['foo']);
 }