diff --git a/.gitignore b/.gitignore index b5a5916..e5fecf4 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ composer.lock *.sublime-workspace *.phar *.log + +.env diff --git a/README.md b/README.md index e05b50d..7cbafdb 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ try { $query = $client->get('query', [ 'query' => 'Hello', + 'sessionId' => '13371337' ]); $response = json_decode((string) $query->getBody(), true); diff --git a/codeception.yml b/codeception.yml new file mode 100644 index 0000000..f0446cc --- /dev/null +++ b/codeception.yml @@ -0,0 +1,10 @@ +paths: + tests: tests + output: tests/_output + data: tests/_data + support: tests/_support + envs: tests/_envs +actor_suffix: Tester +extensions: + enabled: + - Codeception\Extension\RunFailed diff --git a/composer.json b/composer.json index 00b9000..1547c19 100644 --- a/composer.json +++ b/composer.json @@ -25,5 +25,9 @@ "require": { "php": ">=5.5.0", "guzzlehttp/guzzle": "^6.2" + }, + "require-dev": { + "codeception/codeception": "^2.4", + "vlucas/phpdotenv": "^2.5" } -} \ No newline at end of file +} diff --git a/src/Client.php b/src/Client.php index eaa3729..6f9a1d3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -3,7 +3,7 @@ namespace DialogFlow; use DialogFlow\HttpClient\HttpClient; -use DialogFlow\HttpClient\GuzzleHttpClient; +use GuzzleHttp\Client as GuzzleClient; use DialogFlow\Exception\BadResponseException; use GuzzleHttp\Promise\PromiseInterface; use function GuzzleHttp\Promise\rejection_for; @@ -101,7 +101,14 @@ public function __construct($accessToken, HttpClient $httpClient = null, $apiLan */ private function defaultHttpClient() { - return new GuzzleHttpClient(); + return new GuzzleClient([ + 'base_uri' => Client::API_BASE_URI . Client::DEFAULT_API_ENDPOINT, + 'timeout' => Client::DEFAULT_TIMEOUT, + 'connect_timeout' => Client::DEFAULT_TIMEOUT, + 'headers' => [ + 'Authorization' => sprintf('Bearer %s', $this->accessToken), + ], + ]); } /** @@ -144,7 +151,7 @@ public function getLastResponse() */ public function get($uri, array $params = []) { - return $this->send('GET', $uri, null, $params); + return $this->send('GET', $uri, $params); } /** @@ -190,14 +197,15 @@ public function postAsync($uri, array $params = []) * * @return ResponseInterface */ - public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = []) + public function send($method, $uri, array $query = []) { $this->validateMethod($method); $query = array_merge($this->getDefaultQuery(), $query); - $headers = array_merge($this->getDefaultHeaders(), $headers); - $this->lastResponse = $this->client->send($method, $uri, $body, $query, $headers, $options); + $this->lastResponse = $this->client->request($method, $uri, [ + 'query' => $query, + ]); $this->validateResponse($this->lastResponse); diff --git a/src/HttpClient/GuzzleHttpClient.php b/src/HttpClient/GuzzleHttpClient.php index 98b9b02..0bcaded 100644 --- a/src/HttpClient/GuzzleHttpClient.php +++ b/src/HttpClient/GuzzleHttpClient.php @@ -24,12 +24,13 @@ class GuzzleHttpClient implements HttpClient * * @param ClientInterface|null $guzzleClient */ - public function __construct(ClientInterface $guzzleClient = null) + public function __construct(ClientInterface $guzzleClient = null, $headers) { $this->guzzleClient = $guzzleClient ?: new GuzzleClient([ 'base_uri' => Client::API_BASE_URI . Client::DEFAULT_API_ENDPOINT, 'timeout' => Client::DEFAULT_TIMEOUT, 'connect_timeout' => Client::DEFAULT_TIMEOUT, + 'headers' => $headers, ]); } diff --git a/tests/_data/.gitkeep b/tests/_data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/_output/.gitignore b/tests/_output/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/tests/_output/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/_support/AcceptanceTester.php b/tests/_support/AcceptanceTester.php new file mode 100644 index 0000000..4c7dcbb --- /dev/null +++ b/tests/_support/AcceptanceTester.php @@ -0,0 +1,26 @@ +load(); + $this->client = new Client($_ENV['DIALOGFLOW_CLIENT_ACCESS_TOKEN']); + } + + protected function _after() + { + } + + // tests + public function testQueryDoesNotRaiseException() + { + $query = $this->client->get('query', [ + 'query' => 'Hello', + 'sessionId' => '1', + ]); + + $response = json_decode((string) $query->getBody(), true); + } +}