diff --git a/composer.json b/composer.json index 94605d9..fda651a 100644 --- a/composer.json +++ b/composer.json @@ -23,5 +23,10 @@ "require": { "php": ">=5.5.0", "guzzlehttp/guzzle": "^6.2" + }, + + "require-dev": { + "phpunit/phpunit": "~5.6" } + } \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..949f227 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,24 @@ + + + + + + ./tests/ + + + + + + ./src/ + + + \ No newline at end of file diff --git a/src/ActionMapping.php b/src/ActionMapping.php index e1a1b04..dbd0f39 100644 --- a/src/ActionMapping.php +++ b/src/ActionMapping.php @@ -5,32 +5,30 @@ use ApiAi\Model\Context; /** - * Class ActionMapping - * - * @package ApiAi + * Class ActionMapping. */ abstract class ActionMapping { /** - * @param string $sessionId - * @param string $action - * @param array $parameters + * @param string $sessionId + * @param string $action + * @param array $parameters * @param Context[] $contexts + * * @return */ abstract public function action($sessionId, $action, $parameters, $contexts); /** - * @param string $sessionId - * @param string $speech + * @param string $sessionId + * @param string $speech * @param Context[] $contexts */ abstract public function speech($sessionId, $speech, $contexts); /** - * @param string $sessionId + * @param string $sessionId * @param \Exception|string $error */ abstract public function error($sessionId, $error); - } diff --git a/src/Client.php b/src/Client.php index c8e84bc..5a3f79b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,45 +2,43 @@ namespace ApiAi; -use ApiAi\HttpClient\HttpClient; -use ApiAi\HttpClient\GuzzleHttpClient; use ApiAi\Exception\BadResponseException; +use ApiAi\HttpClient\GuzzleHttpClient; +use ApiAi\HttpClient\HttpClient; use Psr\Http\Message\ResponseInterface; /** - * Class Client - * - * @package ApiAi + * Class Client. */ class Client { /** - * API Base url + * API Base url. */ const API_BASE_URI = 'https://api.api.ai/'; /** - * API Version + * API Version. */ const DEFAULT_API_VERSION = '20150910'; /** - * API Endpoint + * API Endpoint. */ const DEFAULT_API_ENDPOINT = 'v1/'; /** - * API Default Language + * API Default Language. */ const DEFAULT_API_LANGUAGE = 'en'; /** - * API Default Source + * API Default Source. */ const DEFAULT_API_SOURCE = 'php'; /** - * Request default timeout + * Request default timeout. */ const DEFAULT_TIMEOUT = 5; @@ -77,10 +75,10 @@ class Client /** * Client constructor. * - * @param string $accessToken + * @param string $accessToken * @param HttpClient|null $httpClient - * @param string $apiLanguage - * @param string $apiVersion + * @param string $apiLanguage + * @param string $apiVersion */ public function __construct($accessToken, HttpClient $httpClient = null, $apiLanguage = self::DEFAULT_API_LANGUAGE, $apiVersion = self::DEFAULT_API_VERSION) { @@ -136,7 +134,7 @@ public function getLastResponse() /** * @param string $uri - * @param array $params + * @param array $params * * @return ResponseInterface */ @@ -147,7 +145,7 @@ public function get($uri, array $params = []) /** * @param string $uri - * @param array $params + * @param array $params * * @return ResponseInterface */ @@ -159,10 +157,10 @@ public function post($uri, array $params = []) /** * @param string $method * @param string $uri - * @param mixed $body - * @param array $query - * @param array $headers - * @param array $options + * @param mixed $body + * @param array $query + * @param array $headers + * @param array $options * * @return ResponseInterface */ @@ -193,6 +191,7 @@ private function validateMethod($method) /** * @param ResponseInterface $response + * * @throws BadResponseException */ private function validateResponse(ResponseInterface $response) @@ -204,30 +203,29 @@ private function validateResponse(ResponseInterface $response) } /** - * Get the defaults query + * Get the defaults query. * * @return array */ private function getDefaultQuery() { return [ - 'v' => $this->apiVersion, + 'v' => $this->apiVersion, 'lang' => $this->apiLanguage, ]; } /** - * Get the defaults headers + * Get the defaults headers. * * @return array */ private function getDefaultHeaders() { return [ - 'Content-Type' => 'application/json; charset=utf-8', - 'Authorization' => 'Bearer ' . $this->accessToken, + 'Content-Type' => 'application/json; charset=utf-8', + 'Authorization' => 'Bearer '.$this->accessToken, 'api-request-source' => self::DEFAULT_API_SOURCE, ]; } - } diff --git a/src/Dialog.php b/src/Dialog.php index 1242c41..5d71039 100644 --- a/src/Dialog.php +++ b/src/Dialog.php @@ -2,18 +2,16 @@ namespace ApiAi; +use ApiAi\Exception\DialogException; +use ApiAi\Exception\InvalidStepException; use ApiAi\Method\QueryApi; use ApiAi\Model\Query; use ApiAi\Model\Step; use ApiAi\Model\Step\Action; use ApiAi\Model\Step\Speech; -use ApiAi\Exception\DialogException; -use ApiAi\Exception\InvalidStepException; /** - * Class Dialog - * - * @package ApiAi + * Class Dialog. */ class Dialog { @@ -30,7 +28,7 @@ class Dialog /** * Dialog constructor. * - * @param QueryApi $queryApi + * @param QueryApi $queryApi * @param ActionMapping $actionMapping */ public function __construct(QueryApi $queryApi, ActionMapping $actionMapping) @@ -42,7 +40,7 @@ public function __construct(QueryApi $queryApi, ActionMapping $actionMapping) /** * @param string $sessionId * @param string $message - * @param array $contexts + * @param array $contexts * * @return bool|void */ @@ -60,7 +58,7 @@ public function create($sessionId, $message, $contexts = []) /** * @param string $sessionId * @param string $message - * @param array $contexts + * @param array $contexts * * @return Action|Speech */ @@ -68,7 +66,7 @@ private function getStep($sessionId, $message, $contexts = []) { $query = $this->queryApi->extractMeaning($message, [ 'sessionId' => $sessionId, - 'contexts' => $contexts, + 'contexts' => $contexts, ]); $query = new Query($query); @@ -92,7 +90,7 @@ private function getStep($sessionId, $message, $contexts = []) /** * @param string $sessionId - * @param Step $step + * @param Step $step * * @return bool */ @@ -109,5 +107,4 @@ private function performStep($sessionId, Step $step) return false; } - } diff --git a/src/Exception/BadResponseException.php b/src/Exception/BadResponseException.php index 2e70ad2..0f2296b 100644 --- a/src/Exception/BadResponseException.php +++ b/src/Exception/BadResponseException.php @@ -5,9 +5,7 @@ use Psr\Http\Message\ResponseInterface; /** - * Class BadResponseException - * - * @package ApiAi\Exception + * Class BadResponseException. */ class BadResponseException extends \RuntimeException { @@ -19,7 +17,7 @@ class BadResponseException extends \RuntimeException /** * BadResponseException constructor. * - * @param string $message + * @param string $message * @param ResponseInterface $response */ public function __construct($message, ResponseInterface $response) diff --git a/src/Exception/DialogException.php b/src/Exception/DialogException.php index bab8a85..4f25f39 100644 --- a/src/Exception/DialogException.php +++ b/src/Exception/DialogException.php @@ -5,9 +5,7 @@ use ApiAi\Model\Query; /** - * Class DialogException - * - * @package ApiAi\Exception + * Class DialogException. */ class DialogException extends \RuntimeException { @@ -44,5 +42,4 @@ public function getQuery() { return $this->query; } - } diff --git a/src/Exception/InvalidStepException.php b/src/Exception/InvalidStepException.php index efc1cfe..194ab55 100644 --- a/src/Exception/InvalidStepException.php +++ b/src/Exception/InvalidStepException.php @@ -5,9 +5,7 @@ use ApiAi\Model\Query; /** - * Class InvalidStepException - * - * @package ApiAi\Exception + * Class InvalidStepException. */ class InvalidStepException extends \RuntimeException { @@ -20,7 +18,7 @@ class InvalidStepException extends \RuntimeException * InvalidStepException constructor. * * @param string $message - * @param Query $query + * @param Query $query */ public function __construct($message, Query $query) { @@ -36,5 +34,4 @@ public function getQuery() { return $this->query; } - } diff --git a/src/HttpClient/GuzzleHttpClient.php b/src/HttpClient/GuzzleHttpClient.php index fad24aa..0077f1f 100644 --- a/src/HttpClient/GuzzleHttpClient.php +++ b/src/HttpClient/GuzzleHttpClient.php @@ -2,15 +2,13 @@ namespace ApiAi\HttpClient; +use ApiAi\Client; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\ClientInterface; use GuzzleHttp\RequestOptions; -use ApiAi\Client; /** - * Class GuzzleHttpClient - * - * @package ApiAi\HttpClient + * Class GuzzleHttpClient. */ class GuzzleHttpClient implements HttpClient { @@ -27,19 +25,19 @@ class GuzzleHttpClient implements HttpClient public function __construct(ClientInterface $guzzleClient = null) { $this->guzzleClient = $guzzleClient ?: new GuzzleClient([ - 'base_uri' => Client::API_BASE_URI . Client::DEFAULT_API_ENDPOINT, - 'timeout' => Client::DEFAULT_TIMEOUT, + 'base_uri' => Client::API_BASE_URI.Client::DEFAULT_API_ENDPOINT, + 'timeout' => Client::DEFAULT_TIMEOUT, 'connect_timeout' => Client::DEFAULT_TIMEOUT, ]); } /** - * @inheritdoc + * {@inheritdoc} */ public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = []) { $options = array_merge($options, [ - RequestOptions::QUERY => $query, + RequestOptions::QUERY => $query, RequestOptions::HEADERS => $headers, ]); @@ -51,5 +49,4 @@ public function send($method, $uri, $body = null, array $query = [], array $head return $this->guzzleClient->request($method, $uri, $options); } - } diff --git a/src/HttpClient/HttpClient.php b/src/HttpClient/HttpClient.php index f2c893a..7e045a0 100644 --- a/src/HttpClient/HttpClient.php +++ b/src/HttpClient/HttpClient.php @@ -5,19 +5,17 @@ use Psr\Http\Message\ResponseInterface; /** - * Interface HttpClient - * - * @package ApiAi\HttpClient + * Interface HttpClient. */ interface HttpClient { /** * @param string $method * @param string $uri - * @param mixed $body - * @param array $query - * @param array $headers - * @param array $options + * @param mixed $body + * @param array $query + * @param array $headers + * @param array $options * * @return ResponseInterface */ diff --git a/src/Method/QueryApi.php b/src/Method/QueryApi.php index 2514d84..6c9e2cf 100644 --- a/src/Method/QueryApi.php +++ b/src/Method/QueryApi.php @@ -6,9 +6,7 @@ use ApiAi\ResponseHandler; /** - * Class QueryApi - * - * @package ApiAi\Method + * Class QueryApi. */ class QueryApi { @@ -31,14 +29,14 @@ public function __construct(Client $client) /** * @param string $query - * @param array $extraParams + * @param array $extraParams * * @return mixed */ public function extractMeaning($query, $extraParams = []) { $query = array_merge($extraParams, [ - 'lang' => $this->client->getApiLanguage(), + 'lang' => $this->client->getApiLanguage(), 'query' => $query, ]); @@ -46,5 +44,4 @@ public function extractMeaning($query, $extraParams = []) return $this->decodeResponse($response); } - } diff --git a/src/Model/Base.php b/src/Model/Base.php index abcbc02..110c387 100644 --- a/src/Model/Base.php +++ b/src/Model/Base.php @@ -3,9 +3,7 @@ namespace ApiAi\Model; /** - * Class Base - * - * @package ApiAi\Model + * Class Base. */ class Base implements \JsonSerializable { @@ -44,7 +42,7 @@ public function has($name) /** * @param string $name - * @param mixed $default + * @param mixed $default * * @return mixed */ @@ -55,7 +53,7 @@ public function get($name, $default = null) /** * @param string $name - * @param mixed $value + * @param mixed $value */ public function add($name, $value) { @@ -72,7 +70,7 @@ public function remove($name) /** * @param string $name - * @param mixed $default + * @param mixed $default * * @return mixed */ @@ -80,5 +78,4 @@ private function getField($name, $default = null) { return $this->has($name) ? $this->data[$name] : $default; } - } diff --git a/src/Model/Context.php b/src/Model/Context.php index f842ed2..c3714d6 100644 --- a/src/Model/Context.php +++ b/src/Model/Context.php @@ -3,9 +3,7 @@ namespace ApiAi\Model; /** - * Class Context - * - * @package ApiAi\Model + * Class Context. */ class Context extends Base { @@ -26,11 +24,10 @@ public function getParameters() } /** - * @return integer + * @return int */ public function getLifespan() { return parent::get('lifespan'); } - } diff --git a/src/Model/Fulfillment.php b/src/Model/Fulfillment.php index b26ee26..3df6909 100644 --- a/src/Model/Fulfillment.php +++ b/src/Model/Fulfillment.php @@ -3,9 +3,7 @@ namespace ApiAi\Model; /** - * Class Fulfillment - * - * @package ApiAi\Model + * Class Fulfillment. */ class Fulfillment extends Base { @@ -48,5 +46,4 @@ public function getContextOut() { return parent::get('contextOut'); } - } diff --git a/src/Model/Metadata.php b/src/Model/Metadata.php index 82302b1..4395c88 100644 --- a/src/Model/Metadata.php +++ b/src/Model/Metadata.php @@ -3,9 +3,7 @@ namespace ApiAi\Model; /** - * Class Metadata - * - * @package ApiAi\Model + * Class Metadata. */ class Metadata extends Base { @@ -32,5 +30,4 @@ public function getWebhookUsed() { return parent::get('webhookUsed'); } - } diff --git a/src/Model/Query.php b/src/Model/Query.php index 2bd5915..554426b 100644 --- a/src/Model/Query.php +++ b/src/Model/Query.php @@ -3,9 +3,7 @@ namespace ApiAi\Model; /** - * Class Query - * - * @package ApiAi\Model + * Class Query. */ class Query extends Base { @@ -40,7 +38,7 @@ public function getId() } /** - * Return the timestamp in the ISO8601 format + * Return the timestamp in the ISO8601 format. * * @return string */ @@ -72,5 +70,4 @@ public function getSessionId() { return parent::get('sessionId'); } - } diff --git a/src/Model/QueryResult.php b/src/Model/QueryResult.php index a348f39..677a3d0 100644 --- a/src/Model/QueryResult.php +++ b/src/Model/QueryResult.php @@ -3,9 +3,7 @@ namespace ApiAi\Model; /** - * Class QueryResult - * - * @package ApiAi\Model + * Class QueryResult. */ class QueryResult extends Base { @@ -104,5 +102,4 @@ public function getScore() { return parent::get('score'); } - } diff --git a/src/Model/Status.php b/src/Model/Status.php index 1db9191..2ab9e43 100644 --- a/src/Model/Status.php +++ b/src/Model/Status.php @@ -3,14 +3,12 @@ namespace ApiAi\Model; /** - * Class Status - * - * @package ApiAi\Model + * Class Status. */ class Status extends Base { /** - * @return integer + * @return int */ public function getCode() { @@ -40,5 +38,4 @@ public function getErrorDetails() { return parent::get('errorDetails'); } - } diff --git a/src/Model/Step.php b/src/Model/Step.php index 7a2b0f6..4799b2a 100644 --- a/src/Model/Step.php +++ b/src/Model/Step.php @@ -3,9 +3,7 @@ namespace ApiAi\Model; /** - * Interface Step - * - * @package ApiAi\Model + * Interface Step. */ interface Step { @@ -16,5 +14,4 @@ interface Step * @return string */ public function getType(); - } diff --git a/src/Model/Step/Action.php b/src/Model/Step/Action.php index fce5860..6c0b924 100644 --- a/src/Model/Step/Action.php +++ b/src/Model/Step/Action.php @@ -6,9 +6,7 @@ use ApiAi\Model\Step; /** - * Class Action - * - * @package ApiAi\Model\Step + * Class Action. */ class Action implements Step { @@ -30,8 +28,8 @@ class Action implements Step /** * Action constructor. * - * @param string $action - * @param array $parameters + * @param string $action + * @param array $parameters * @param Context[] $contexts */ public function __construct($action, array $parameters, array $contexts) @@ -72,5 +70,4 @@ public function getType() { return Step::TYPE_ACTION; } - } diff --git a/src/Model/Step/Speech.php b/src/Model/Step/Speech.php index 4f86cec..9e3b750 100644 --- a/src/Model/Step/Speech.php +++ b/src/Model/Step/Speech.php @@ -6,9 +6,7 @@ use ApiAi\Model\Step; /** - * Class Speech - * - * @package ApiAi\Model\Step + * Class Speech. */ class Speech implements Step { @@ -25,7 +23,7 @@ class Speech implements Step /** * Speech constructor. * - * @param string $speech + * @param string $speech * @param Context[] $contexts */ public function __construct($speech, array $contexts) @@ -57,5 +55,4 @@ public function getType() { return Step::TYPE_SPEECH; } - } diff --git a/src/ResponseHandler.php b/src/ResponseHandler.php index b61fd27..a74711f 100644 --- a/src/ResponseHandler.php +++ b/src/ResponseHandler.php @@ -5,9 +5,7 @@ use Psr\Http\Message\ResponseInterface; /** - * Class ResponseHandler - * - * @package ApiAi + * Class ResponseHandler. */ trait ResponseHandler { @@ -20,5 +18,4 @@ public function decodeResponse(ResponseInterface $response) { return json_decode((string) $response->getBody(), true); } - } diff --git a/src/StepFactory.php b/src/StepFactory.php index fd2e9cf..3bfd990 100644 --- a/src/StepFactory.php +++ b/src/StepFactory.php @@ -2,16 +2,14 @@ namespace ApiAi; +use ApiAi\Exception\InvalidStepException; use ApiAi\Model\Query; use ApiAi\Model\Step; use ApiAi\Model\Step\Action; use ApiAi\Model\Step\Speech; -use ApiAi\Exception\InvalidStepException; /** - * Class StepFactory - * - * @package ApiAi + * Class StepFactory. */ class StepFactory { @@ -72,5 +70,4 @@ public static function createSpeechStep(Query $query) $query->getResult()->getContexts() ); } - } diff --git a/tests/ClientTest.php b/tests/ClientTest.php new file mode 100644 index 0000000..18efd8c --- /dev/null +++ b/tests/ClientTest.php @@ -0,0 +1,23 @@ +get('query', [ + 'query' => 'Hello', + ]); + + $this->assertTrue($query->getReasonPhrase() === 'OK'); + $this->assertTrue($query->getStatusCode() === 200); + + $response = json_decode((string) $query->getBody(), true); + + $this->assertEquals($response['result']['action'], 'greeting'); + $this->assertEquals($response['result']['fulfillment']['speech'], 'Hi! How are you?'); + } +} diff --git a/tests/QueryApiTest.php b/tests/QueryApiTest.php new file mode 100644 index 0000000..6db88f2 --- /dev/null +++ b/tests/QueryApiTest.php @@ -0,0 +1,27 @@ +extractMeaning('Hello', [ + 'sessionId' => '1234567890', + 'lang' => 'en', + ]); + + $response = new Query($meaning); + + $this->assertEquals($response->getStatus()->getCode(), 200); + $this->assertEquals($response->getStatus()->getErrorType(), 'success'); + $this->assertEquals($response->getSessionId(), '1234567890'); + $this->assertEquals($response->getResult()->getAction(), 'greeting'); + $this->assertEquals($response->getResult()->getFulfillment()->getSpeech(), 'Hi! How are you?'); + } +}