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); }
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); }
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); }
/** * @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; }
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']); }
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); }
/** * @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; }
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); }
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); }
/** * @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; }
/** * 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'); }
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(); }
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); }
protected function guzzleMock() { $this->guzzle(); if (!$this->mock) { $this->mock = new Guzzle\Subscriber\Mock(); $this->guzzle->getEmitter()->attach($this->mock); } return $this->mock; }
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); }
/** * @param Client $client * @param string $logPath */ public function __construct(Client $client, $logPath) { $this->client = $client; $loggerPath = sprintf('%s/%s.log', $logPath, 'erp_client_logger'); $log = new Logger('erp_client_logger'); $log->pushHandler(new StreamHandler($loggerPath, Logger::DEBUG)); $subscriber = new LogSubscriber($log); $this->client->getEmitter()->attach($subscriber); }
public function setUp() { $logger = new Logger('guzzle.curl'); $this->loggerHandler = new TestHandler(); $this->client = new Client(); $curlFormatterSubscriber = new CurlFormatterSubscriber($logger); $logger->pushHandler($this->loggerHandler); $this->client->getEmitter()->attach($curlFormatterSubscriber); }
/** * Create a client obj * @param string $baseUrl * @return Client */ private function createClient($baseUrl = self::TWITTER_API_URL) { $client = new Client(['base_url' => $baseUrl, 'defaults' => ['auth' => 'oauth', 'stream' => true]]); $oauth = new Oauth1(['consumer_key' => $this->consumerKey, 'consumer_secret' => $this->consumerSecret, 'token' => $this->accessKey, 'token_secret' => $this->accessSecret]); $retry = new RetrySubscriber(['filter' => RetrySubscriber::createStatusFilter([503]), 'max' => self::MAX_RETRIES]); $client->getEmitter()->attach($retry); $client->getEmitter()->attach($oauth); return $client; }
/** * Sets up the test fixtures. */ protected function setUp() { $this->client = new Client(); $this->mock = new Mock(); $this->history = new History(); // Add the mock subscriber to the client. $this->client->getEmitter()->attach($this->mock); $this->client->getEmitter()->attach($this->history); $this->connector = new Connector($this->client, self::MERCHANT_ID, self::SHARED_SECRET); }
protected function getGuzzle() { $this->history = new History(); $this->mock = new Mock(); $this->mock->addResponse(new GuzzleResponse(200, array(), Stream::factory('<html><body><p>Hi</p></body></html>'))); $guzzle = new GuzzleClient(array('redirect.disable' => true, 'base_url' => '')); $guzzle->getEmitter()->attach($this->mock); $guzzle->getEmitter()->attach($this->history); return $guzzle; }
public function setUp() { $this->command = new HarvestCommand(); $client = new Client(); $this->history = new History(); $client->getEmitter()->attach($this->history); $mock = new Mock([new Response(200, array('Content-Length' => '10'), null), new Response(200, array('Content-Type' => 'application/zip'), null)]); $client->getEmitter()->attach($mock); $this->command->setClient($client); parent::setUp(); }
public function setUp() { $this->command = new HarvestCommand(); $client = new Client(); $this->history = new History(); $client->getEmitter()->attach($this->history); $mock = new Mock([new Response(200, array('Content-Length' => ''), $this->getResponseBody())]); $client->getEmitter()->attach($mock); $this->command->setClient($client); parent::setUp(); }
public static function setUpBeforeClass() { self::$client = new Client(); $getFileStream = Stream::factory(JsonLoader::getLibraryFileJson()); $getFilesStream = Stream::factory(JsonLoader::getLibraryFilesJson()); $getFolderStream = Stream::factory(JsonLoader::getLibraryFolderJson()); $getFoldersStream = Stream::factory(JsonLoader::getLibraryFoldersJson()); $getFileUploadStream = Stream::factory(JsonLoader::getFileUploadStatusJson()); $mock = new Mock([new Response(200, array(), $getFileStream), new Response(200, array(), $getFilesStream), new Response(200, array(), $getFolderStream), new Response(200, array(), $getFoldersStream), new Response(201, array("Id" => 1)), new Response(200, array(), $getFileUploadStream)]); self::$client->getEmitter()->attach($mock); }
public function setUp() { parent::setUp(); $this->sc = $this->getContainer()->get('swordclient'); $client = new Client(); $this->history = new History(); $client->getEmitter()->attach($this->history); $mock = new Mock([new Response(400, array(), $this->getResponseBody())]); $client->getEmitter()->attach($mock); $this->sc->setClient($client); // do not call serviceDocument() here - it's going throw an exception. }
public function setUp() { parent::setUp(); $this->sc = $this->getContainer()->get('swordclient'); $client = new Client(); $this->history = new History(); $client->getEmitter()->attach($this->history); $mock = new Mock([new Response(200, array('X-Mock' => 'abcpdq'), $this->getResponseBody())]); $client->getEmitter()->attach($mock); $this->sc->setClient($client); $this->sc->serviceDocument($this->references->getReference('journal')); }
public function testRegisterPlugin() { $client = new Client(['base_url' => 'http://example.com']); $client->getEmitter()->attach($this->getPlugin()); $mock = new Mock(); $mock->addResponse(new Response(200)); $client->getEmitter()->attach($mock); $request = $client->createRequest('GET', '/resource/1'); $client->send($request); $authorization = (string) $request->getHeader('Authorization'); $this->assertRegExp('@Acquia 1:([a-zA-Z0-9+/]+={0,2})$@', $authorization); }
public function testSignsRequests() { $client = new Client(); $client->getEmitter()->attach(new Mock([new Response(200)])); $credentials = new Credentials('*****@*****.**', 'abc16cfd7e7fa8263b8b83eb3b4467e60ca0638b'); $signer = $this->getMockBuilder('SellerCenter\\SDK\\Common\\Signature\\SignatureInterface')->setMethods(['signRequest'])->getMockForAbstractClass(); $subscriber = new Signature($credentials, $signer); $client->getEmitter()->attach($subscriber); $request = $client->createRequest('GET', 'http://foo.com'); $signer->expects($this->once())->method('signRequest')->with($request, $credentials); $client->send($request); }
public static function setUpBeforeClass() { self::$client = new Client(); $activityStream = Stream::factory(JsonLoader::getActivity()); $activitiesStream = Stream::factory(JsonLoader::getActivities()); $clearListActivityStream = Stream::factory(JsonLoader::getClearListsActivity()); $exportActivityStream = Stream::factory(JsonLoader::getExportContactsActivity()); $removeContactsFromListStream = Stream::factory(JsonLoader::getRemoveContactsFromListsActivity()); $addContactsStream = Stream::factory(JsonLoader::getAddContactsActivity()); $mock = new Mock([new Response(200, array(), $activityStream), new Response(200, array(), $activitiesStream), new Response(201, array(), $clearListActivityStream), new Response(201, array(), $exportActivityStream), new Response(201, array(), $removeContactsFromListStream), new Response(201, array(), $addContactsStream)]); self::$client->getEmitter()->attach($mock); }
public static function setUpBeforeClass() { self::$client = new Client(); $getBouncesStream = Stream::factory(JsonLoader::getBounces()); $getClicksStream = Stream::factory(JsonLoader::getClicks()); $getForwardsStream = Stream::factory(JsonLoader::getForwards()); $getUnsubscribesStream = Stream::factory(JsonLoader::getOptOuts()); $getSendsStream = Stream::factory(JsonLoader::getSends()); $getOpensStream = Stream::factory(JsonLoader::getOpens()); $getSummaryStream = Stream::factory(JsonLoader::getSummary()); $mock = new Mock([new Response(200, array(), $getBouncesStream), new Response(200, array(), $getClicksStream), new Response(200, array(), $getForwardsStream), new Response(200, array(), $getUnsubscribesStream), new Response(200, array(), $getSendsStream), new Response(200, array(), $getOpensStream), new Response(200, array(), $getSummaryStream)]); self::$client->getEmitter()->attach($mock); }
/** * @param array $guzzleConfig GuzzleHttp\Client defaults * @param array $retryConfig @see RestClient::createBackoff() * * retryConfig options * - maxRetries: (integer) max retries count * - http * - retryHeader (string) header containing retry time header * - codes (array) list of status codes to retry on * - curl * - codes (array) list of error codes to retry on * * @return self */ public static function create($guzzleConfig = [], $retryConfig = []) { $guzzle = new Client($guzzleConfig); $guzzle->getEmitter()->attach(self::createBackoff($retryConfig)); $guzzle->getEmitter()->on('error', function (ErrorEvent $errorEvent) { $errno = $errorEvent->getTransferInfo('errno'); $error = $errorEvent->getTransferInfo('error'); if ($errno > 0) { throw new UserException(sprintf("CURL error %d: %s", $errno, $error)); } }, "last"); return new self($guzzle); }