diff --git a/.gitignore b/.gitignore index ee98a84..eea2c19 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ demo.php vendor .idea .php_cs.cache +composer.lock diff --git a/README.md b/README.md index d2b5cff..a52f0e9 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![Total Downloads](https://poser.pugx.org/polidog/esa-php/downloads)](https://packagist.org/packages/polidog/esa-php) [![License](https://poser.pugx.org/polidog/esa-php/license)](https://packagist.org/packages/polidog/esa-php) -esa API v1 client library, written in PHP +esa API v2 client library, written in PHP ## Installation @@ -30,129 +30,129 @@ composer.phar require polidog/esa-php require 'vendor/autoload.php'; -$client = new Polidog\Esa\Client("", "foobar"); +$api = \Polidog\Esa\Api::factory("", "foobar"); -$client->user(); +$api->user(); // GET /v1/user -$client->teams(); +$api->teams(); // GET /v1/teams -$client->team('bar'); +$api->team('bar'); // GET /v1/teams/bar -$client->stats() +$api->stats() // GET /v1/teams/foobar/stats -$client->members(); +$api->members(); // GET /v1/teams/foobar/members -$client->posts(); +$api->posts(); // GET /v1/teams/foobar/posts -$client->posts(["q" => "in:help"]); +$api->posts(["q" => "in:help"]); // GET /v1/teams/foobar/posts?q=in%3Ahelp -$client->createPost(["name" => "foo"]); +$api->createPost(["name" => "foo"]); // POST /v1/teams/foobar/posts -$client->updatePost(1, ["name" => "bar"]); +$api->updatePost(1, ["name" => "bar"]); // PATCH /v1/teams/foobar/posts/1 -$client->deletePost(1); +$api->deletePost(1); // DELETE /v1/teams/foobar/posts/1 // Comment API -$client->comments(1); /* post number */ +$api->comments(1); /* post number */ // GET /v1/teams/foobar/posts/1/comments -$client->createComment(1, ['body_md' => 'baz']); +$api->createComment(1, ['body_md' => 'baz']); // POST /v1/teams/foobar/posts/1/comments -$client->comment(123); /* comment id */ +$api->comment(123); /* comment id */ // GET /v1/teams/foobar/comments/123 -$client->updateComment(123, ['body_md' => 'bazbaz']); +$api->updateComment(123, ['body_md' => 'bazbaz']); // PATCH /v1/teams/foobar/comments/123 -$client->deleteComment(123); +$api->deleteComment(123); // DELETE /v1/teams/foobar/comments/123 -$client->comments(); +$api->comments(); // GET /v1/teams/foobar/comments -$client->createSharing(1); +$api->createSharing(1); // POST /v1/teams/foobar/posts/1/sharing -$client->deleteSharing(1); +$api->deleteSharing(1); // DELETE /v1/teams/foobar/posts/1/sharing # Star API -$client->postStargazers(1); +$api->postStargazers(1); // GET /v1/teams/foobar/posts/1/stargazers -$client->addPostStar(1); +$api->addPostStar(1); // POST /v1/teams/foobar/posts/1/star -$client->deletePostStar(1); +$api->deletePostStar(1); // DELETE /v1/teams/foobar/posts/1/star -$client->commentStargazers(123); +$api->commentStargazers(123); // GET /v1/teams/foobar/comments/123/stargazers -$client->addCommentStar(123); +$api->addCommentStar(123); // POST /v1/teams/foobar/comments/123/star -$client->deleteCommentStar(123); +$api->deleteCommentStar(123); // DELETE /v1/teams/foobar/comments/123/star # Watch API -$client->watchers(1); +$api->watchers(1); // GET /v1/teams/foobar/posts/1/watchers -$client->addWatch(1); +$api->addWatch(1); // POST /v1/teams/foobar/posts/1/watch -$client->deleteWtach(1); +$api->deleteWtach(1); // DELETE /v1/teams/foobar/posts/1/watch # Categories API -$client->categories(); +$api->categories(); // GET /v1/teams/foobar/categories # Tags API -$client->tags(); +$api->tags(); // GET /v1/teams/foobar/tags # Invitation API -$client->invitation(); +$api->invitation(); // GET /v1/teams/foobar/invitation -$client->regenerateInvitation(); +$api->regenerateInvitation(); // POST /v1/teams/foobar/invitation_regenerator -$client->pendingInvitations(); +$api->pendingInvitations(); // GET /v1/teams/foobar/invitations -$client->sendInvitation(['test@test.com','test2@test.com']); +$api->sendInvitation(['test@test.com','test2@test.com']); // POST /v1/teams/foobar/invitations -$client->cancelInvitation($code); +$api->cancelInvitation($code); // DELETE /v1/teams/foobar/invitations/baz # Emoji API -$client->emojis(); +$api->emojis(); // GET /v1/teams/foobar/emojis -$client->createEmoji(['code' => 'team_emoji', image: '/path/to/image'); +$api->createEmoji(['code' => 'team_emoji', image: '/path/to/image'); // POST /v1/teams/foobar/emojis -$client->createEmoji(['code' => 'alias_code', origin_code: 'team_emoji'); +$api->createEmoji(['code' => 'alias_code', origin_code: 'team_emoji'); // POST /v1/teams/foobar/emojis -$client->deleteEmoji('team_emoji'); +$api->deleteEmoji('team_emoji'); // DELETE /v1/teams/foobar/emojis/team_emoji ``` diff --git a/composer.json b/composer.json index 6302a8d..a41c5e3 100644 --- a/composer.json +++ b/composer.json @@ -13,16 +13,20 @@ "guzzlehttp/guzzle": "~6.0" }, "require-dev": { - "phpunit/phpunit": "4.6.7", - "phpunit/phpcov": "*", - "phake/phake": "2.*", - "satooshi/php-coveralls": "dev-master" + "satooshi/php-coveralls": "^2.0", + "phpunit/phpunit": "^5.7", + "fabpot/php-cs-fixer": "^2.10" }, "autoload": { "psr-4": { "Polidog\\Esa\\": "src" } }, + "autoload-dev": { + "psr-4": { + "Polidog\\Esa\\Test\\": "tests" + } + }, "scripts": { "test": "./vendor/bin/phpunit" } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9aeb870..b9a8f0d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + ./tests/ @@ -17,6 +17,5 @@ - - \ No newline at end of file + diff --git a/src/Api.php b/src/Api.php new file mode 100644 index 0000000..74e8999 --- /dev/null +++ b/src/Api.php @@ -0,0 +1,470 @@ +client = $client; + $this->currentTeam = $currentTeam; + } + + /** + * @param array $params + * + * @return array + */ + public function user(array $params = []) + { + return $this->client->request('GET', 'user', [ + 'query' => $params, + ]); + } + + /** + * @return array + */ + public function teams() + { + return $this->client->request('GET', 'teams'); + } + + /** + * @param string|null $name + * + * @return array + */ + public function team($name = null) + { + return $this->client->request('GET', "teams/{$name}"); + } + + /** + * @return array + */ + public function stats() + { + return $this->client->request('GET', $this->getCurrentTeamUrl('stats')); + } + + /** + * @return array + */ + public function members() + { + return $this->client->request('GET', $this->getCurrentTeamUrl('members')); + } + + /** + * @param array $params + * + * @return array + */ + public function posts(array $params = []) + { + return $this->client->request('GET', $this->getCurrentTeamUrl('posts'), [ + 'query' => $params, + ]); + } + + /** + * @param int $number + * + * @return array + */ + public function post($number) + { + return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$number}")); + } + + /** + * @param array $data + * + * @return array + */ + public function createPost($data) + { + return $this->client->request('POST', $this->getCurrentTeamUrl('posts'), [ + 'json' => [ + 'post' => $data, + ], + ]); + } + + /** + * @param int $number + * @param array $data + * + * @return array + */ + public function updatePost($number, array $data) + { + return $this->client->request('PATCH', $this->getCurrentTeamUrl("posts/{$number}"), [ + 'json' => [ + 'post' => $data, + ], + ]); + } + + /** + * @param int $number + * + * @return array + */ + public function deletePost($number) + { + return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$number}")); + } + + /** + * @param int $number + * @param array $params + * + * @return array + */ + public function comments($number = null, array $params = []) + { + if (empty($number)) { + return $this->client->request('GET', $this->getCurrentTeamUrl('comments'), [ + 'query' => $params, + ]); + } + + return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$number}/comments"), [ + 'query' => $params, + ]); + } + + /** + * @param int $commentId + * @param array $params + * + * @return array + */ + public function comment($commentId, array $params = []) + { + return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}"), [ + 'query' => $params, + ]); + } + + /** + * @param int $postNumber + * @param array $data + * + * @return array + */ + public function createComment($postNumber, array $data) + { + return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/comments"), [ + 'json' => [ + 'comment' => $data, + ], + ]); + } + + /** + * @param int $commentId + * @param array $data + * + * @return array + */ + public function updateComment($commentId, array $data) + { + return $this->client->request('PATCH', $this->getCurrentTeamUrl("comments/{$commentId}"), [ + 'json' => [ + 'comment' => $data, + ], + ]); + } + + /** + * @param int $commentId + * + * @return array + */ + public function deleteComment($commentId) + { + return $this->client->request('DELETE', $this->getCurrentTeamUrl("comments/{$commentId}")); + } + + /** + * @param int $postNumber + * + * @return array + */ + public function createSharing($postNumber) + { + return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); + } + + /** + * @param int $postNumber + * + * @return array + */ + public function deleteSharing($postNumber) + { + return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); + } + + /** + * @param int $postNumber + * @param array $params + * + * @return array + */ + public function postStargazers($postNumber, array $params = []) + { + return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/stargazers"), [ + 'query' => $params, + ]); + } + + /** + * @param int $postNumber + * @param array $params + * + * @return array + */ + public function addPostStar($postNumber, array $params = []) + { + return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/star"), [ + 'json' => $params, + ]); + } + + /** + * @param int $postNumber + * + * @return array + */ + public function deletePostStar($postNumber) + { + return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/star")); + } + + /** + * @param int $commentId + * @param array $params + * + * @return array + */ + public function commentStargazers($commentId, array $params = []) + { + return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}/stargazers"), [ + 'query' => $params, + ]); + } + + /** + * @param $commentId + * @param array $params + * + * @return array + */ + public function addCommentStar($commentId, array $params = []) + { + return $this->client->request('POST', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [ + 'json' => $params, + ]); + } + + /** + * @param $commentId + * + * @return array + */ + public function deleteCommentStar($commentId) + { + return $this->client->request('DELETE', $this->getCurrentTeamUrl("comments/{$commentId}/star")); + } + + /** + * @param integer$postNumber + * @param array $params + * + * @return array + */ + public function watchers($postNumber, array $params = []) + { + return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/watchers"), [ + 'query' => $params, + ]); + } + + /** + * @param $postNumber + * + * @return array + */ + public function addWatch($postNumber) + { + return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); + } + + /** + * @param $postNumber + * + * @return array + */ + public function deleteWatch($postNumber) + { + return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); + } + + /** + * @return array + */ + public function categories() + { + return $this->client->request('GET', $this->getCurrentTeamUrl('categories')); + } + + /** + * @param array $params + * + * @return array + */ + public function batchMoveCategory(array $params = []) + { + return $this->client->request('POST', $this->getCurrentTeamUrl('categories/batch_move'), [ + 'json' => $params, + ]); + } + + /** + * @return array + */ + public function tags() + { + return $this->client->request('GET', $this->getCurrentTeamUrl('tags')); + } + + /** + * @return array + */ + public function invitation() + { + return $this->client->request('GET', $this->getCurrentTeamUrl('invitation')); + } + + /** + * @return array + */ + public function regenerateInvitation() + { + return $this->client->request('POST', $this->getCurrentTeamUrl('invitation_regenerator')); + } + + /** + * @param $params + * + * @return array + */ + public function pendingInvitations(array $params = []) + { + return $this->client->request('GET', $this->getCurrentTeamUrl('invitations'), [ + 'query' => $params, + ]); + } + + /** + * @param array $emails + * + * @return array + */ + public function sendInvitation(array $emails) + { + return $this->client->request('POST', $this->getCurrentTeamUrl('invitations'), [ + 'json' => [ + 'members' => ['emails' => $emails], + ], + ]); + } + + /** + * @param string $code + * + * @return array + */ + public function cancelInvitation($code) + { + return $this->client->request('DELETE', $this->getCurrentTeamUrl("invitations/{$code}")); + } + + /** + * @param array $params + * + * @return array + */ + public function emojis(array $params = []) + { + return $this->client->request('GET', $this->getCurrentTeamUrl('emojis'), [ + 'query' => $params, + ]); + } + + /** + * @param $data + * + * @return array + */ + public function createEmoji(array $data) + { + return $this->client->request('POST', $this->getCurrentTeamUrl('emojis'), [ + 'json' => [ + 'emoji' => $data, + ], + ]); + } + + /** + * @param $code + * + * @return array + */ + public function deleteEmoji($code) + { + return $this->client->request('DELETE', "teams/{$this->currentTeam}/emojis/{$code}"); + } + + /** + * @param $accessToken + * @param $currentTeam + * + * @return Api + */ + public static function factory($accessToken, $currentTeam) + { + $client = Client::factory($accessToken); + + return new self($client, $currentTeam); + } + + private function getCurrentTeamUrl($path = '') + { + return "teams/{$this->currentTeam}/$path"; + } +} diff --git a/src/ApiMethods.php b/src/ApiMethods.php deleted file mode 100644 index 71c4dc0..0000000 --- a/src/ApiMethods.php +++ /dev/null @@ -1,446 +0,0 @@ -httpClient = $client; - $this->currentTeam = $currentTeam; - } - - /** - * @param array $params - * @param null $headers - */ - public function user(array $params = []) - { - return $this->httpClient->request('GET', 'user', [ - 'query' => $params, - ]); - } - - /** - * @return \Psr\Http\Message\ResponseInterface - */ - public function teams() - { - return $this->httpClient->request('GET', 'teams'); - } - - /** - * @param null $name - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function team($name = null) - { - return $this->httpClient->request('GET', "teams/{$name}"); - } - - /** - * @return \Psr\Http\Message\ResponseInterface - */ - public function stats() - { - return $this->httpClient->request('GET', "teams/{$this->currentTeam}/stats"); - } - - /** - * @return \Psr\Http\Message\ResponseInterface - */ - public function members() - { - return $this->httpClient->request('GET', "teams/{$this->currentTeam}/members"); - } - - /** - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function posts(array $params = []) - { - return $this->httpClient->request('GET', "teams/{$this->currentTeam}/posts", [ - 'query' => $params, - ]); - } - - /** - * @param $number - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function post($number) - { - return $this->httpClient->request('GET', "teams/{$this->currentTeam}/posts/{$number}"); - } - - /** - * @param $data - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function createPost($data) - { - return $this->httpClient->request('POST', "teams/{$this->currentTeam}/posts", [ - 'json' => [ - 'post' => $data, - ], - ]); - } - - /** - * @param $number - * @param $data - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function updatePost($number, $data) - { - return $this->httpClient->request('PATCH', "teams/{$this->currentTeam}/posts/{$number}", [ - 'json' => [ - 'post' => $data, - ], - ]); - } - - /** - * @param $number - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function deletePost($number) - { - return $this->httpClient->request('DELETE', "teams/{$this->currentTeam}/posts/{$number}"); - } - - /** - * @param int $number - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function comments($number = null, $params = []) - { - if (empty($number)) { - return $this->httpClient->request('GET', "teams/{$this->currentTeam}/comments", [ - 'query' => $params, - ]); - } - - return $this->httpClient->request('GET', "teams/{$this->currentTeam}/posts/{$number}/comments", [ - 'query' => $params, - ]); - } - - /** - * @param $commentId - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function comment($commentId, $params = []) - { - return $this->httpClient->request('GET', "teams/{$this->currentTeam}/comments/{$commentId}", [ - 'query' => $params, - ]); - } - - /** - * @param $postNumber - * @param $data - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function createComment($postNumber, $data) - { - return $this->httpClient->request('POST', "teams/{$this->currentTeam}/posts/{$postNumber}/comments", [ - 'json' => [ - 'comment' => $data, - ], - ]); - } - - /** - * @param $commentId - * @param $data - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function updateComment($commentId, $data) - { - return $this->httpClient->request('PATCH', "teams/{$this->currentTeam}/comments/{$commentId}", [ - 'json' => [ - 'comment' => $data, - ], - ]); - } - - /** - * @param $commentId - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function deleteComment($commentId) - { - return $this->httpClient->request('DELETE', "teams/{$this->currentTeam}/comments/{$commentId}"); - } - - /** - * @param $postNumber - * @return \Psr\Http\Message\ResponseInterface - */ - public function createSharing($postNumber) - { - return $this->httpClient->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); - } - - /** - * @param $postNumber - * @return \Psr\Http\Message\ResponseInterface - */ - public function deleteSharing($postNumber) - { - return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); - } - - /** - * @param $postNumber - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function postStargazers($postNumber, $params = []) - { - return $this->httpClient->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/stargazers"), [ - 'query' => $params, - ]); - } - - /** - * @param $postNumber - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function addPostStar($postNumber, $params = []) - { - return $this->httpClient->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/star"), [ - 'json' => $params, - ]); - } - - /** - * @param $postNumber - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function deletePostStar($postNumber) - { - return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/star")); - } - - /** - * @param $commentId - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function commentStargazers($commentId, $params = []) - { - return $this->httpClient->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [ - 'query' => $params, - ]); - } - - /** - * @param $commentId - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function addCommentStar($commentId, $params = []) - { - return $this->httpClient->request('POST', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [ - 'json' => $params, - ]); - } - - /** - * @param $commentId - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function deleteCommentStar($commentId) - { - return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("comments/{$commentId}/star")); - } - - /** - * @param $postNumber - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function watchers($postNumber, $params = []) - { - return $this->httpClient->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/watchers"), [ - 'query' => $params, - ]); - } - - /** - * @param $postNumber - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function addWatch($postNumber) - { - return $this->httpClient->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); - } - - /** - * @param $postNumber - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function deleteWatch($postNumber) - { - return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); - } - - /** - * @return \Psr\Http\Message\ResponseInterface - */ - public function categories() - { - return $this->httpClient->request('GET', $this->getCurrentTeamUrl('categories')); - } - - /** - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function batchMoveCategory($params = []) - { - return $this->httpClient->request('POST', $this->getCurrentTeamUrl('categories/batch_move'), [ - 'json' => $params, - ]); - } - - /** - * @return \Psr\Http\Message\ResponseInterface - */ - public function tags() - { - return $this->httpClient->request('GET', $this->getCurrentTeamUrl('tags')); - } - - /** - * @return \Psr\Http\Message\ResponseInterface - */ - public function invitation() - { - return $this->httpClient->request('GET', $this->getCurrentTeamUrl('invitation')); - } - - /** - * @return \Psr\Http\Message\ResponseInterface - */ - public function regenerateInvitation() - { - return $this->httpClient->request('POST', $this->getCurrentTeamUrl('invitation_regenerator')); - } - - /** - * @param $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function pendingInvitations(array $params) - { - return $this->httpClient->request('GET', $this->getCurrentTeamUrl('invitations'), [ - 'query' => $params, - ]); - } - - /** - * @param array $data - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function sendInvitation(array $data) - { - return $this->httpClient->request('POST', $this->getCurrentTeamUrl('invitations'), [ - 'json' => [ - 'members' => $data, - ], - ]); - } - - /** - * @param string $code - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function cancelInvitation($code) - { - return $this->httpClient->request('DELETE', $this->getCurrentTeamUrl("invitations/{$code}")); - } - - /** - * @param array $params - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function emojis(array $params = []) - { - return $this->httpClient->request('GET', "teams/{$this->currentTeam}/emojis", [ - 'query' => $params, - ]); - } - - /** - * @param $data - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function createEmoji($data) - { - return $this->httpClient->request('POST', "teams/{$this->currentTeam}/emojis", [ - 'json' => [ - 'emoji' => $data, - ], - ]); - } - - /** - * @param $code - * - * @return \Psr\Http\Message\ResponseInterface - */ - public function deleteEmoji($code) - { - return $this->httpClient->request('DELETE', "teams/{$this->currentTeam}/emojis/{$code}"); - } - - private function getCurrentTeamUrl($path = '') - { - return "teams/{$this->currentTeam}/$path"; - } -} diff --git a/src/Client.php b/src/Client.php deleted file mode 100644 index 2575a8f..0000000 --- a/src/Client.php +++ /dev/null @@ -1,84 +0,0 @@ - 'https://api.esa.io/v1/', - 'timeout' => 60, - 'allow_redirect' => false, - 'headers' => [ - 'User-Agent' => 'esa-php-api v1', - 'Accept' => 'application/json', - ], - ]; - - /** - * @param $accessToken - * @param $currentTeam - * @param HttpClientInterface $httpClient - * @param array $httpOptions - */ - public function __construct($accessToken, $currentTeam, HttpClientInterface $httpClient = null, $httpOptions = []) - { - $this->accessToken = $accessToken; - $this->currentTeam = $currentTeam; - $this->httpOptions = array_merge($this->httpOptions, $httpOptions); - $this->httpOptions['handler'] = $this->createAuthStack(); - - if (empty($httpClient)) { - $httpClient = new \GuzzleHttp\Client($this->httpOptions); - } - $this->httpClient = $httpClient; - $this->apiMethods = new ApiMethods($httpClient, $currentTeam); - } - - /** - * @param $name - * @param $args - * @return mixed - * - * @throws ApiErrorException - */ - public function __call($name, $args) - { - try { - /** @var Response $response */ - $response = call_user_func_array([$this->apiMethods, $name], $args); - $data = json_decode($response->getBody()->getContents(), true); - return $data; - } catch (\Exception $exception) { - throw ApiErrorException::newException($exception, $name, $args); - } - } - - private function createAuthStack() - { - $stack = HandlerStack::create(); - $stack->push(Middleware::mapRequest(function (RequestInterface $request) { - return $request->withHeader('Authorization', 'Bearer '.$this->accessToken); - })); - - return $stack; - } -} diff --git a/src/Client/Client.php b/src/Client/Client.php new file mode 100644 index 0000000..a6bb9b6 --- /dev/null +++ b/src/Client/Client.php @@ -0,0 +1,84 @@ + 'https://api.esa.io/v1/', + 'timeout' => 60, + 'allow_redirect' => false, + 'headers' => [ + 'User-Agent' => 'esa-php-api v2', + 'Accept' => 'application/json', + ], + ]; + + /** + * @param string $accessToken + * @param HttpClientInterface $httpClient + */ + public function __construct($accessToken, HttpClientInterface $httpClient) + { + $this->accessToken = $accessToken; + $this->httpClient = $httpClient; + } + + /** + * @param string $method + * @param string $path + * @param array $data + * + * @return array + * + * @throws ApiErrorException + */ + public function request($method, $path, array $data = []) + { + try { + $response = $this->httpClient->request($method, $path, $data); + + return json_decode($response->getBody()->getContents(), true); + } catch (GuzzleException $e) { + throw ApiErrorException::newException($e, $path, $data); + } + } + + public static function factory($accessToken, $httpOptions = []) + { + $httpOptions = array_merge(static::$httpOptions, $httpOptions); + $httpOptions['handler'] = static::createAuthStack($accessToken); + + return new self($accessToken, new \GuzzleHttp\Client($httpOptions)); + } + + private static function createAuthStack($accessToken) + { + $stack = HandlerStack::create(); + $stack->push(Middleware::mapRequest(function (RequestInterface $request) use ($accessToken) { + return $request->withHeader('Authorization', 'Bearer '.$accessToken); + })); + + return $stack; + } +} diff --git a/src/Client/ClientInterface.php b/src/Client/ClientInterface.php new file mode 100644 index 0000000..971f401 --- /dev/null +++ b/src/Client/ClientInterface.php @@ -0,0 +1,15 @@ +getCode(), $e); + $self = new self(sprintf('Api method error: %s', $name), $e->getCode(), $e); $self->apiMethodName = $name; $self->args = $args; + return $self; } - } diff --git a/tests/ApiMethodsTest.php b/tests/ApiMethodsTest.php deleted file mode 100644 index 9f71d83..0000000 --- a/tests/ApiMethodsTest.php +++ /dev/null @@ -1,477 +0,0 @@ -teams(); - - Phake::verify($client)->request('GET', 'teams'); - } - - /** - * @test - */ - public function callApiTeam() - { - $client = Phake::mock('GuzzleHttp\\Client'); - - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->team('foo'); - - Phake::verify($client)->request('GET', 'teams/foo'); - } - - /** - * @test - */ - public function callApiPosts() - { - $client = Phake::mock('GuzzleHttp\\Client'); - - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->posts(['q' => 'in:help']); - - Phake::verify($client)->request('GET', 'teams/bar/posts', [ - 'query' => [ - 'q' => 'in:help', - ], - ]); - } - - /** - * @test - */ - public function callApiPost() - { - $client = Phake::mock('GuzzleHttp\\Client'); - - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->post(1); - - Phake::verify($client)->request('GET', 'teams/bar/posts/1'); - } - - /** - * @test - */ - public function callApiCreatePost() - { - $client = Phake::mock('GuzzleHttp\\Client'); - - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->createPost([ - 'name' => 'hogehoge', - ]); - - Phake::verify($client)->request('POST', 'teams/bar/posts', [ - 'json' => [ - 'post' => [ - 'name' => 'hogehoge', - ], - ], - ]); - } - - /** - * @test - */ - public function callApiUpdatePost() - { - $client = Phake::mock('GuzzleHttp\\Client'); - - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->updatePost(1, [ - 'name' => 'fuga', - ]); - - Phake::verify($client)->request('PATCH', 'teams/bar/posts/1', [ - 'json' => [ - 'post' => [ - 'name' => 'fuga', - ], - ], - ]); - } - - /** - * @test - */ - public function callApiDeletePost() - { - $client = Phake::mock('GuzzleHttp\\Client'); - - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->deletePost(1); - - Phake::verify($client)->request('DELETE', 'teams/bar/posts/1'); - } - - /** - * @test - */ - public function callApiEmoji() - { - $client = Phake::mock('GuzzleHttp\\Client'); - - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->emojis(['include' => 'all']); - - Phake::verify($client)->request('GET', 'teams/bar/emojis', [ - 'query' => [ - 'include' => 'all', - ], - ]); - } - - /** - * @test - */ - public function callApiCreateEmoji() - { - $client = Phake::mock('GuzzleHttp\\Client'); - - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->createEmoji([ - 'code' => 'team_emoji', - 'image' => 'base64_string', - ]); - - Phake::verify($client)->request('POST', 'teams/bar/emojis', [ - 'json' => [ - 'emoji' => [ - 'code' => 'team_emoji', - 'image' => 'base64_string', - ], - ], - ]); - } - - /** - * @test - */ - public function callApiDeleteEmoji() - { - $client = Phake::mock('GuzzleHttp\\Client'); - - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->deleteEmoji('team_emoji'); - - Phake::verify($client)->request('DELETE', 'teams/bar/emojis/team_emoji'); - } - - public function testCallApiUser() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->user([ - 'include' => 'teams', - ]); - - Phake::verify($client)->request('GET', 'user', [ - 'query' => [ - 'include' => 'teams', - ], - ]); - } - - public function testCallCommentList() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->comments(null, [ - 'page' => 3, - ]); - - Phake::verify($client)->request('GET', 'teams/bar/comments', [ - 'query' => [ - 'page' => 3, - ], - ]); - } - - public function testCallComment() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - - $apiMethods->comment(1, [ - 'include' => 'stargazers', - ]); - - Phake::verify($client)->request('GET', 'teams/bar/comments/1', [ - 'query' => [ - 'include' => 'stargazers', - ], - ]); - } - - public function testCreateComment() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $bodyMd = 'LGTM!!'; - - $apiMethods->createComment(2, [ - 'body_md' => $bodyMd, - ]); - - Phake::verify($client)->request('POST', 'teams/bar/posts/2/comments', [ - 'json' => [ - 'comment' => [ - 'body_md' => $bodyMd, - ], - ], - ]); - } - - public function testUpdateComment() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $bodyMd = 'LGTM!!'; - - $apiMethods->updateComment(22767, [ - 'body_md' => $bodyMd, - ]); - - Phake::verify($client)->request('PATCH', 'teams/bar/comments/22767', [ - 'json' => [ - 'comment' => [ - 'body_md' => $bodyMd, - ], - ], - ]); - } - - public function testDeleteComment() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->deleteComment(22767); - - Phake::verify($client)->request('DELETE', 'teams/bar/comments/22767'); - } - - public function testMembers() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - - $apiMethods->members(); - - Phake::verify($client)->request('GET', 'teams/bar/members'); - } - - public function testPostStargazers() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->postStargazers(2312, [ - 'page' => 2, - 'per_page' => 30, - ]); - - Phake::verify($client)->request('GET', 'teams/bar/posts/2312/stargazers', [ - 'query' => [ - 'page' => 2, - 'per_page' => 30, - ], - ]); - } - - public function testAddPostStar() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - - $apiMethods->addPostStar(123, [ - 'body' => 'foo bar', - ]); - - Phake::verify($client)->request('POST', 'teams/bar/posts/123/star', [ - 'json' => [ - 'body' => 'foo bar', - ], - ]); - } - - public function testDeletePostStar() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->deletePostStar(123); - - Phake::verify($client)->request('DELETE', 'teams/bar/posts/123/star'); - } - - public function testCommentStargazers() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - - $apiMethods->commentStargazers(123, [ - 'page' => 2, - 'par_page' => 20, - ]); - - Phake::verify($client)->request('GET', 'teams/bar/comments/123/star', [ - 'query' => [ - 'page' => 2, - 'par_page' => 20, - ], - ]); - } - - public function testAddCommentStar() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - - $apiMethods->addCommentStar(123, [ - 'body' => 'foo bar', - ]); - - Phake::verify($client)->request('POST', 'teams/bar/comments/123/star', [ - 'json' => [ - 'body' => 'foo bar', - ], - ]); - } - - public function testDeleteCommentStar() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - - $apiMethods->deleteCommentStar(123); - - Phake::verify($client)->request('DELETE', 'teams/bar/comments/123/star'); - } - - public function testWatchers() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - - $apiMethods->watchers(2312, [ - 'page' => 2, - 'par_page' => 30, - ]); - - Phake::verify($client)->request('GET', 'teams/bar/posts/2312/watchers', [ - 'query' => [ - 'page' => 2, - 'par_page' => 30, - ], - ]); - } - - public function testAddWatch() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->addWatch(2312); - - Phake::verify($client)->request('GET', 'teams/bar/posts/2312/watch'); - } - - public function testDeleteWatch() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->deleteWatch(2312); - - Phake::verify($client)->request('DELETE', 'teams/bar/posts/2312/watch'); - } - - public function testCategories() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->categories(['page' => 1]); - Phake::verify($client)->request('GET', 'teams/bar/categories'); - } - - public function testBatchMoveCategory() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->batchMoveCategory([ - 'from' => '/foo/bar/', - 'to' => '/baz/', - ]); - - Phake::verify($client)->request('POST', 'teams/bar/categories/batch_move', [ - 'json' => [ - 'from' => '/foo/bar/', - 'to' => '/baz/', - ], - ]); - } - - public function testTags() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->tags(); - - Phake::verify($client)->request('GET', 'teams/bar/tags'); - } - - public function testPendingInvitations() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - $apiMethods->pendingInvitations([ - 'page' => 2, - 'par_page' => 35, - ]); - - Phake::verify($client)->request('GET', 'teams/bar/invitations', [ - 'query' => [ - 'page' => 2, - 'par_page' => 35, - ], - ]); - } - - public function testSendInvitation() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - - $apiMethods->sendInvitation([ - 'test@test.com', - 'test2@test.com', - ]); - Phake::verify($client)->request('POST', 'teams/bar/invitations', [ - 'json' => [ - 'members' => [ - 'test@test.com', - 'test2@test.com', - ], - ], - ]); - } - - public function testCancelInvitation() - { - $client = Phake::mock('GuzzleHttp\\Client'); - $apiMethods = new ApiMethods($client, 'bar'); - - $apiMethods->cancelInvitation('mee93383edf699b525e01842d34078e28'); - - Phake::verify($client)->request('DELETE', 'teams/bar/invitations/mee93383edf699b525e01842d34078e28'); - } -} diff --git a/tests/ApiTest.php b/tests/ApiTest.php new file mode 100644 index 0000000..c3bc8f9 --- /dev/null +++ b/tests/ApiTest.php @@ -0,0 +1,353 @@ +client = $this->prophesize(ClientInterface::class); + } + + public function testFactory() + { + $api = Api::factory('token', 'team'); + $this->assertInstanceOf(Api::class, $api); + } + + public function testUser() + { + $api = $this->getApiObject(); + $api->user(); + $this->client->request('GET', 'user', [ + 'query' => [], + ])->shouldHaveBeenCalled(); + } + + public function testTeams() + { + $api = $this->getApiObject(); + $api->teams(); + $this->client->request('GET', 'teams')->shouldHaveBeenCalled(); + } + + public function testTeam() + { + $api = $this->getApiObject(); + $api->team('team_name'); + $this->client->request('GET', 'teams/team_name')->shouldHaveBeenCalled(); + } + + public function testStats() + { + $api = $this->getApiObject(); + $api->stats(); + $this->client->request('GET', 'teams/test/stats')->shouldHaveBeenCalled(); + } + + public function testMembers() + { + $api = $this->getApiObject(); + $api->members(); + $this->client->request('GET', 'teams/test/members')->shouldHaveBeenCalled(); + } + + public function testPosts() + { + $api = $this->getApiObject(); + $api->posts(); + $this->client->request('GET', 'teams/test/posts', [ + 'query' => [], + ])->shouldHaveBeenCalled(); + } + + public function testPost() + { + $api = $this->getApiObject(); + $api->post(1); + $this->client->request('GET', 'teams/test/posts/1')->shouldHaveBeenCalled(); + } + + public function testCreatePost() + { + $api = $this->getApiObject(); + $api->createPost(['name' => 'foo']); + $this->client->request('POST', 'teams/test/posts', [ + 'json' => [ + 'post' => ['name' => 'foo'], + ], + ])->shouldHaveBeenCalled(); + } + + public function testUpdatePost() + { + $api = $this->getApiObject(); + $api->updatePost(12, ['name' => 'bar']); + $this->client->request('PATCH', 'teams/test/posts/12', [ + 'json' => [ + 'post' => ['name' => 'bar'], + ], + ])->shouldHaveBeenCalled(); + } + + public function testDeletePost() + { + $api = $this->getApiObject(); + $api->deletePost(13); + $this->client->request('DELETE', 'teams/test/posts/13')->shouldHaveBeenCalled(); + } + + public function testComments() + { + $api = $this->getApiObject(); + $api->comments(); + $this->client->request('GET', 'teams/test/comments', [ + 'query' => [], + ])->shouldHaveBeenCalled(); + } + + public function testCommentsById() + { + $api = $this->getApiObject(); + $api->comments(1); + $this->client->request('GET', 'teams/test/posts/1/comments', [ + 'query' => [], + ])->shouldHaveBeenCalled(); + } + + public function testComment() + { + $api = $this->getApiObject(); + $api->comment(1); + $this->client->request('GET', 'teams/test/comments/1', ['query' => []])->shouldHaveBeenCalled(); + } + + public function testCreateComment() + { + $api = $this->getApiObject(); + $api->createComment(1, ['body_md' => 'baz']); + $this->client->request('POST', 'teams/test/posts/1/comments', ['json' => [ + 'comment' => [ + 'body_md' => 'baz', + ], + ]])->shouldHaveBeenCalled(); + } + + public function testUpdateComment() + { + $api = $this->getApiObject(); + $api->updateComment(1, ['body_md' => 'foo']); + $this->client->request('PATCH', 'teams/test/comments/1', ['json' => [ + 'comment' => [ + 'body_md' => 'foo', + ], + ]])->shouldHaveBeenCalled(); + } + + public function testDeleteCommand() + { + $api = $this->getApiObject(); + $api->deleteComment(1); + $this->client->request('DELETE', 'teams/test/comments/1')->shouldHaveBeenCalled(); + } + + public function testCreateSharing() + { + $api = $this->getApiObject(); + $api->createSharing(1); + $this->client->request('POST', 'teams/test/posts/1/sharing')->shouldHaveBeenCalled(); + } + + public function testDeleteSharing() + { + $api = $this->getApiObject(); + $api->deleteSharing(1); + $this->client->request('DELETE', 'teams/test/posts/1/sharing')->shouldHaveBeenCalled(); + } + + public function testPostStargazers() + { + $api = $this->getApiObject(); + $api->postStargazers(1); + $this->client->request('GET', 'teams/test/posts/1/stargazers', [ + 'query' => [], + ])->shouldHaveBeenCalled(); + } + + public function testAddPostStar() + { + $api = $this->getApiObject(); + $api->addPostStar(1, ['body' => 'foo bar']); + $this->client->request('POST', 'teams/test/posts/1/star', [ + 'json' => [ + 'body' => 'foo bar', + ], + ])->shouldHaveBeenCalled(); + } + + public function testDeletePostStar() + { + $api = $this->getApiObject(); + $api->deletePostStar(1); + $this->client->request('DELETE', 'teams/test/posts/1/star')->shouldHaveBeenCalled(); + } + + public function testCommentStargazers() + { + $api = $this->getApiObject(); + $api->commentStargazers(1); + $this->client->request('GET', 'teams/test/comments/1/stargazers', ['query' => []])->shouldHaveBeenCalled(); + } + + public function testAddCommentStar() + { + $api = $this->getApiObject(); + $api->addCommentStar(1, ['body' => 'foo bar']); + $this->client->request('POST', 'teams/test/comments/1/star', [ + 'json' => [ + 'body' => 'foo bar', + ], + ])->shouldHaveBeenCalled(); + } + + public function testDeleteCommentStar() + { + $api = $this->getApiObject(); + $api->deleteCommentStar(1); + $this->client->request('DELETE', 'teams/test/comments/1/star')->shouldHaveBeenCalled(); + } + + public function testWatchers() + { + $api = $this->getApiObject(); + $api->watchers(1); + $this->client->request('GET', 'teams/test/posts/1/watchers', ['query' => []])->shouldHaveBeenCalled(); + } + + public function testAddWatch() + { + $api = $this->getApiObject(); + $api->addWatch(1); + $this->client->request('POST', 'teams/test/posts/1/watch')->shouldHaveBeenCalled(); + } + + public function testDeleteWatch() + { + $api = $this->getApiObject(); + $api->deleteWatch(1); + $this->client->request('DELETE', 'teams/test/posts/1/watch')->shouldHaveBeenCalled(); + } + + public function testCategories() + { + $api = $this->getApiObject(); + $api->categories(); + $this->client->request('GET', 'teams/test/categories')->shouldHaveBeenCalled(); + } + + public function testBatchMoveCategory() + { + $api = $this->getApiObject(); + $api->batchMoveCategory([ + 'from' => '/foo/bar', + 'to' => '/biz', + ]); + + $this->client->request('POST', 'teams/test/categories/batch_move', [ + 'json' => [ + 'from' => '/foo/bar', + 'to' => '/biz', + ], + ])->shouldHaveBeenCalled(); + } + + public function testTags() + { + $api = $this->getApiObject(); + $api->tags(); + + $this->client->request('GET', 'teams/test/tags')->shouldHaveBeenCalled(); + } + + public function testInvitation() + { + $api = $this->getApiObject(); + $api->invitation(); + $this->client->request('GET', 'teams/test/invitation')->shouldHaveBeenCalled(); + } + + public function testRegenerateInvitation() + { + $api = $this->getApiObject(); + $api->regenerateInvitation(); + $this->client->request('POST', 'teams/test/invitation_regenerator')->shouldHaveBeenCalled(); + } + + public function testPendingInvitations() + { + $api = $this->getApiObject(); + $api->pendingInvitations(); + $this->client->request('GET', 'teams/test/invitations', ['query' => []])->shouldHaveBeenCalled(); + } + + public function testSendInvitation() + { + $api = $this->getApiObject(); + $api->sendInvitation(['polidogs@gmail.com']); + $this->client->request('POST', 'teams/test/invitations', ['json' => [ + 'members' => [ + 'emails' => ['polidogs@gmail.com'], + ], + ]])->shouldHaveBeenCalled(); + } + + public function testCancelInvitation() + { + $api = $this->getApiObject(); + $api->cancelInvitation('code'); + $this->client->request('DELETE', 'teams/test/invitations/code')->shouldHaveBeenCalled(); + } + + public function testEmojis() + { + $api = $this->getApiObject(); + $api->emojis(); + $this->client->request('GET', 'teams/test/emojis', ['query' => []])->shouldHaveBeenCalled(); + } + + public function testCreateEmoji() + { + $api = $this->getApiObject(); + $api->createEmoji([ + 'code' => 'team_emoji', + 'image' => 'base64...', + ]); + + $this->client->request('POST', 'teams/test/emojis', [ + 'json' => [ + 'emoji' => [ + 'code' => 'team_emoji', + 'image' => 'base64...', + ], + ], + ])->shouldHaveBeenCalled(); + } + + public function testDeleteEmoji() + { + $api = $this->getApiObject(); + $api->deleteEmoji('code'); + $this->client->request('DELETE', 'teams/test/emojis/code')->shouldHaveBeenCalled(); + } + + private function getApiObject() + { + return new Api($this->client->reveal(), 'test'); + } +} diff --git a/tests/Client/ClientTest.php b/tests/Client/ClientTest.php new file mode 100644 index 0000000..7c41fdb --- /dev/null +++ b/tests/Client/ClientTest.php @@ -0,0 +1,53 @@ +assertInstanceOf(Client::class, $client); + } + + public function testRequest() + { + $httpClient = $this->prophesize(HttpClientInterface::class); + $response = $this->prophesize(ResponseInterface::class); + $stream = $this->prophesize(StreamInterface::class); + + $stream->getContents() + ->willReturn(json_encode(['a' => 'b'])); + + $response->getBody() + ->willReturn($stream->reveal()); + + $httpClient->request(Argument::any(), Argument::any(), Argument::any()) + ->willReturn($response->reveal()); + + $client = new Client('token', $httpClient->reveal()); + $client->request('GET', '/test', ['query' => ['a' => 'b']]); + $httpClient->request('GET', '/test', ['query' => ['a' => 'b']]); + } + + /** + * @expectedException \Polidog\Esa\Exception\ApiErrorException + */ + public function testRequestException() + { + $httpClient = $this->prophesize(HttpClientInterface::class); + $httpClient->request('GET', '/test', ['query' => ['a' => 'b']])->willThrow( + new TransferException() + ); + $client = new Client('token', $httpClient->reveal()); + $client->request('GET', '/test', ['query' => ['a' => 'b']]); + } +} diff --git a/tests/ClientTest.php b/tests/ClientTest.php deleted file mode 100644 index 25edb1b..0000000 --- a/tests/ClientTest.php +++ /dev/null @@ -1,31 +0,0 @@ -request('GET', 'teams') - ->thenReturn($response); - - Phake::when($response)->getBody() - ->thenReturn($stream); - - $client = new Client('token', 'polidog', $httpClient); - $client->teams(); - - Phake::verify($response)->getBody(); - Phake::verify($stream)->getContents(); - } -} diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index 6282f95..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,4 +0,0 @@ -addPsr4('Polidog\\Esa\\', __DIR__);