本文整理汇总了PHP中GuzzleHttp\Client::getEmitter方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::getEmitter方法的具体用法?PHP Client::getEmitter怎么用?PHP Client::getEmitter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Client
的用法示例。
在下文中一共展示了Client::getEmitter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: mockGuzzle
private function mockGuzzle($response)
{
$this->client = new Client();
$mock = new Mock(array(file_get_contents(__DIR__ . '/fixtures/' . $response)));
$this->client->getEmitter()->attach($mock);
$this->service = new LaravelDolarBlue($this->client);
}
示例2: testAuthenticateClient
public function testAuthenticateClient()
{
$guzzle = new Client(['base_url' => 'http://example.com/api']);
$guzzle->setDefaultOption('headers', ['X-Test' => '1']);
$expiresIn = 5000;
$expires = time() + $expiresIn;
$mock = new Mock([new Response(200, [], Stream::factory(json_encode((object) ['headerToken' => 1234, 'queryToken' => 4321, 'expiresIn' => $expiresIn]))), new Response(200, [], Stream::factory(json_encode((object) ['data' => [1, 2, 3]]))), new Response(200, [], Stream::factory(json_encode((object) ['data' => [1, 2, 3]])))]);
$guzzle->getEmitter()->attach($mock);
$history = new History();
$guzzle->getEmitter()->attach($history);
$restClient = new RestClient($guzzle);
$attrs = ['first' => 1, 'second' => 'two'];
$api = ['authentication' => ['loginRequest' => ['endpoint' => 'login', 'params' => ['par' => ['attr' => 'first']], 'headers' => ['X-Header' => ['attr' => 'second']], 'method' => 'POST'], 'apiRequest' => ['headers' => ['X-Test-Auth' => 'headerToken'], 'query' => ['qToken' => 'queryToken']], 'expires' => ['response' => 'expiresIn', 'relative' => true]]];
$auth = new Login($attrs, $api);
$auth->authenticateClient($restClient);
$request = $restClient->createRequest(['endpoint' => '/']);
$restClient->download($request);
$restClient->download($request);
// test creation of the login request
self::assertEquals($attrs['second'], $history->getIterator()[0]['request']->getHeader('X-Header'));
self::assertEquals(json_encode(['par' => $attrs['first']]), (string) $history->getIterator()[0]['request']->getBody());
// test signature of the api request
self::assertEquals(1234, $history->getIterator()[1]['request']->getHeader('X-Test-Auth'));
self::assertEquals('qToken=4321', (string) $history->getIterator()[1]['request']->getQuery());
self::assertEquals(1234, $history->getIterator()[2]['request']->getHeader('X-Test-Auth'));
self::assertEquals('qToken=4321', (string) $history->getIterator()[2]['request']->getQuery());
$expiry = self::getProperty($restClient->getClient()->getEmitter()->listeners('before')[0][0], 'expires');
self::assertGreaterThan($expires - 2, $expiry);
self::assertLessThan($expires + 2, $expiry);
}
示例3: testSend
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);
}
示例4: getHttpClient
/**
* @return HttpClient
*/
public function getHttpClient()
{
if (!$this->httpClient) {
$this->httpClient = new HttpClient(['base_url' => $this->host]);
$this->httpClient->getEmitter()->attach(new AuthSubscriber($this->publicKey, $this->secretKey));
}
return $this->httpClient;
}
示例5: setUpBeforeClass
public static function setUpBeforeClass()
{
self::$client = new Client();
$getWareHouses = Stream::factory(JsonLoader::getWareHouse());
$mock = new Mock([new Response(200, array(), $getWareHouses)]);
self::$client->getEmitter()->attach($mock);
self::$transRush = new \TransRush\TransRush(['Token' => '3E3CB737-FD2E-4699-9E25-66ADA387C37A', 'Env' => 'DEV', 'Key' => 'CD88153912AG']);
}
示例6: setUpBeforeClass
public static function setUpBeforeClass()
{
self::$client = new Client();
$tokenInfoStream = Stream::factory(JsonLoader::getTokenInfoJson());
$accessTokenStream = Stream::factory(JsonLoader::getAccessTokenJson());
$mock = new Mock([new Response(200, array(), $tokenInfoStream), new Response(200, array(), $accessTokenStream)]);
self::$client->getEmitter()->attach($mock);
}
示例7: getHttpClient
/**
* @param array $options
* @return HttpClient
*/
protected function getHttpClient($options = [])
{
if (is_null($this->httpClient)) {
$this->httpClient = new HttpClient($options);
$this->httpClient->getEmitter()->attach(new HttpHistorySubscriber());
}
return $this->httpClient;
}
示例8: setUpBeforeClass
public static function setUpBeforeClass()
{
self::$client = new Client();
$verifiedAddressStream = Stream::factory(JsonLoader::getVerifiedAddressesJson());
$accountInfoStream = Stream::factory(JsonLoader::getAccountInfoJson());
$mock = new Mock([new Response(200, array(), $verifiedAddressStream), new Response(200, array(), $accountInfoStream)]);
self::$client->getEmitter()->attach($mock);
}
示例9: setUpBeforeClass
public static function setUpBeforeClass()
{
self::$client = new Client();
$schedulesStream = Stream::factory(JsonLoader::getCampaignSchedulesJson());
$scheduleStream = Stream::factory(JsonLoader::getCampaignScheduleJson());
$testSendStream = Stream::factory(JsonLoader::getTestSendJson());
$mock = new Mock([new Response(200, array(), $schedulesStream), new Response(200, array(), $scheduleStream), new Response(201, array(), $scheduleStream), new Response(200, array(), $scheduleStream), new Response(204, array()), new Response(400, array()), new Response(200, array(), $testSendStream)]);
self::$client->getEmitter()->attach($mock);
}
示例10: getClient
/**
* @return Client
*/
private function getClient()
{
if (null === $this->client) {
$this->client = new Client(['base_url' => static::API_BASE_URL, 'defaults' => ['headers' => ['user-id' => 'vicb', 'api-name' => $this->apiName, 'api-key' => $this->apiKey, 'Accept' => 'application/json']]]);
$retry = new RetrySubscriber(['filter' => RetrySubscriber::createStatusFilter(), 'max' => 3]);
$this->client->getEmitter()->attach($retry);
}
return $this->client;
}
示例11: testFindByUserUuid
/**
* testFindByUserUuid
*
* @access public
* @return void
*/
public function testFindByUserUuid()
{
$http = new HttpClient();
$history = new History();
$mock = new Mock([new Response(200, [], Stream::factory('{"data":[]}'))]);
$http->getEmitter()->attach($mock);
$http->getEmitter()->attach($history);
$this->given($instance = $this->newTestedInstance($http, $this->token, $this->transformer))->then->given($list = $instance->findByUserUuid('user-id', ['image_sizes' => ['320x160', '640x320']]))->object($list)->isInstanceOf('Mapado\\Sdk\\Model\\MapadoList')->given($lastRequest = $history->getLastRequest())->then->string($lastRequest->getUrl())->contains('/users/user-id/user-lists?image_sizes%5B0%5D=320x160&image_sizes%5B1%5D=640x320');
}
示例12: __construct
public function __construct($options)
{
$this->options = array_merge($this->options, $options);
$logger = new Logger($this->logger, [new StreamHandler($this->options['log.file.path'], Logger::INFO)]);
$subscriber = new LogSubscriber($logger);
$this->httpClient = new HttpClient();
$this->httpClient->getEmitter()->attach($subscriber);
$this->init();
}
示例13: setUpBeforeClass
public static function setUpBeforeClass()
{
self::$client = new Client();
$getCampaignsStream = Stream::factory(JsonLoader::getCampaignsJson());
$getCampaignStream = Stream::factory(JsonLoader::getCampaignJson());
$getPreviewStream = Stream::factory(JsonLoader::getPreviewJson());
$mock = new Mock([new Response(200, array(), $getCampaignsStream), new Response(204, array()), new Response(400, array()), new Response(200, array(), $getCampaignStream), new Response(201, array(), $getCampaignStream), new Response(200, array(), $getCampaignStream), new Response(200, array(), $getPreviewStream)]);
self::$client->getEmitter()->attach($mock);
}
示例14: guzzleMock
protected function guzzleMock()
{
$this->guzzle();
if (!$this->mock) {
$this->mock = new Guzzle\Subscriber\Mock();
$this->guzzle->getEmitter()->attach($this->mock);
}
return $this->mock;
}
示例15: setUpBeforeClass
public static function setUpBeforeClass()
{
self::$client = new Client();
$getListsStream = Stream::factory(JsonLoader::getListsJson());
$getListStream = Stream::factory(JsonLoader::getListJson());
$getContactsStream = Stream::factory(JsonLoader::getContactsJson());
$mock = new Mock([new Response(200, array(), $getListsStream), new Response(200, array(), $getListStream), new Response(201, array(), $getListStream), new Response(200, array(), $getListStream), new Response(200, array(), $getContactsStream)]);
self::$client->getEmitter()->attach($mock);
}