From a2391fcbb3e027c162324f15d18874f10d62f657 Mon Sep 17 00:00:00 2001 From: polidog Date: Sun, 28 Jan 2018 11:59:26 +0900 Subject: [PATCH 1/7] Add New Client Class And Api Class --- src/Api.php | 461 +++++++++++++++++++++++++++++++ src/ApiMethods.php | 446 ------------------------------ src/Client.php | 84 ------ src/Client/Client.php | 85 ++++++ src/Client/ClientInterface.php | 16 ++ tests/ApiMethodsTest.php | 477 --------------------------------- tests/ClientTest.php | 31 --- tests/bootstrap.php | 4 - 8 files changed, 562 insertions(+), 1042 deletions(-) create mode 100644 src/Api.php delete mode 100644 src/ApiMethods.php delete mode 100644 src/Client.php create mode 100644 src/Client/Client.php create mode 100644 src/Client/ClientInterface.php delete mode 100644 tests/ApiMethodsTest.php delete mode 100644 tests/ClientTest.php delete mode 100644 tests/bootstrap.php diff --git a/src/Api.php b/src/Api.php new file mode 100644 index 0000000..697ea27 --- /dev/null +++ b/src/Api.php @@ -0,0 +1,461 @@ +client = $client; + $this->currentTeam = $currentTeam; + } + + /** + * @param array $params + * @param null $headers + */ + 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 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', "teams/{$this->currentTeam}/stats"); + } + + /** + * @return array + */ + public function members() + { + return $this->client->request('GET', "teams/{$this->currentTeam}/members"); + } + + /** + * @param array $params + * + * @return array + */ + public function posts(array $params = []) + { + return $this->client->request('GET', "teams/{$this->currentTeam}/posts", [ + 'query' => $params, + ]); + } + + /** + * @param $number + * + * @return array + */ + public function post($number) + { + return $this->client->request('GET', "teams/{$this->currentTeam}/posts/{$number}"); + } + + /** + * @param $data + * + * @return array + */ + public function createPost($data) + { + return $this->client->request('POST', "teams/{$this->currentTeam}/posts", [ + 'json' => [ + 'post' => $data, + ], + ]); + } + + /** + * @param $number + * @param $data + * + * @return array + */ + public function updatePost($number, $data) + { + return $this->client->request('PATCH', "teams/{$this->currentTeam}/posts/{$number}", [ + 'json' => [ + 'post' => $data, + ], + ]); + } + + /** + * @param $number + * + * @return array + */ + public function deletePost($number) + { + return $this->client->request('DELETE', "teams/{$this->currentTeam}/posts/{$number}"); + } + + /** + * @param int $number + * @param array $params + * + * @return array + */ + public function comments($number = null, $params = []) + { + if (empty($number)) { + return $this->client->request('GET', "teams/{$this->currentTeam}/comments", [ + 'query' => $params, + ]); + } + + return $this->client->request('GET', "teams/{$this->currentTeam}/posts/{$number}/comments", [ + 'query' => $params, + ]); + } + + /** + * @param $commentId + * @param array $params + * + * @return array + */ + public function comment($commentId, $params = []) + { + return $this->client->request('GET', "teams/{$this->currentTeam}/comments/{$commentId}", [ + 'query' => $params, + ]); + } + + /** + * @param $postNumber + * @param $data + * + * @return array + */ + public function createComment($postNumber, $data) + { + return $this->client->request('POST', "teams/{$this->currentTeam}/posts/{$postNumber}/comments", [ + 'json' => [ + 'comment' => $data, + ], + ]); + } + + /** + * @param $commentId + * @param $data + * + * @return array + */ + public function updateComment($commentId, $data) + { + return $this->client->request('PATCH', "teams/{$this->currentTeam}/comments/{$commentId}", [ + 'json' => [ + 'comment' => $data, + ], + ]); + } + + /** + * @param $commentId + * + * @return array + */ + public function deleteComment($commentId) + { + return $this->client->request('DELETE', "teams/{$this->currentTeam}/comments/{$commentId}"); + } + + /** + * @param $postNumber + * @return array + */ + public function createSharing($postNumber) + { + return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); + } + + /** + * @param $postNumber + * @return array + */ + public function deleteSharing($postNumber) + { + return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); + } + + /** + * @param $postNumber + * @param array $params + * + * @return array + */ + public function postStargazers($postNumber, $params = []) + { + return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/stargazers"), [ + 'query' => $params, + ]); + } + + /** + * @param $postNumber + * @param array $params + * + * @return array + */ + public function addPostStar($postNumber, $params = []) + { + return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/star"), [ + 'json' => $params, + ]); + } + + /** + * @param $postNumber + * + * @return array + */ + public function deletePostStar($postNumber) + { + return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/star")); + } + + /** + * @param $commentId + * @param array $params + * + * @return array + */ + public function commentStargazers($commentId, $params = []) + { + return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [ + 'query' => $params, + ]); + } + + /** + * @param $commentId + * @param array $params + * + * @return array + */ + public function addCommentStar($commentId, $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 $postNumber + * @param array $params + * + * @return array + */ + public function watchers($postNumber, $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('GET', $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($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 $data + * + * @return array + */ + public function sendInvitation(array $data) + { + return $this->client->request('POST', $this->getCurrentTeamUrl('invitations'), [ + 'json' => [ + 'members' => $data, + ], + ]); + } + + /** + * @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', "teams/{$this->currentTeam}/emojis", [ + 'query' => $params, + ]); + } + + /** + * @param $data + * + * @return array + */ + public function createEmoji($data) + { + return $this->client->request('POST', "teams/{$this->currentTeam}/emojis", [ + 'json' => [ + 'emoji' => $data, + ], + ]); + } + + /** + * @param $code + * + * @return array + */ + public function deleteEmoji($code) + { + return $this->client->request('DELETE', "teams/{$this->currentTeam}/emojis/{$code}"); + } + + public static function factory($accessToken, $currentTeam) + { + $client = Client::factory($accessToken, $currentTeam); + 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..773a619 --- /dev/null +++ b/src/Client/Client.php @@ -0,0 +1,85 @@ + '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..9176148 --- /dev/null +++ b/src/Client/ClientInterface.php @@ -0,0 +1,16 @@ +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/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__); From 909f9e49101a97a8b247dcf0f5b28c86b758b296 Mon Sep 17 00:00:00 2001 From: polidog Date: Sun, 28 Jan 2018 12:45:59 +0900 Subject: [PATCH 2/7] php cs fixer --- src/Api.php | 12 ++++++++++-- src/Client/Client.php | 9 ++++----- src/Client/ClientInterface.php | 5 ++--- src/Exception/ApiErrorException.php | 8 ++++---- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Api.php b/src/Api.php index 697ea27..9d2e255 100644 --- a/src/Api.php +++ b/src/Api.php @@ -5,7 +5,6 @@ use Polidog\Esa\Client\Client; use Polidog\Esa\Client\ClientInterface; - /** * Class ApiMethods. */ @@ -23,7 +22,7 @@ class Api /** * @param ClientInterface $client - * @param string $currentTeam + * @param string $currentTeam */ public function __construct(ClientInterface $client, $currentTeam) { @@ -211,6 +210,7 @@ public function deleteComment($commentId) /** * @param $postNumber + * * @return array */ public function createSharing($postNumber) @@ -220,6 +220,7 @@ public function createSharing($postNumber) /** * @param $postNumber + * * @return array */ public function deleteSharing($postNumber) @@ -448,9 +449,16 @@ 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, $currentTeam); + return new self($client, $currentTeam); } diff --git a/src/Client/Client.php b/src/Client/Client.php index 773a619..a6bb9b6 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -1,6 +1,5 @@ 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); + $response = $this->httpClient->request($method, $path, $data); + return json_decode($response->getBody()->getContents(), true); } catch (GuzzleException $e) { throw ApiErrorException::newException($e, $path, $data); @@ -69,6 +68,7 @@ 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)); } @@ -81,5 +81,4 @@ private static function createAuthStack($accessToken) return $stack; } - } diff --git a/src/Client/ClientInterface.php b/src/Client/ClientInterface.php index 9176148..971f401 100644 --- a/src/Client/ClientInterface.php +++ b/src/Client/ClientInterface.php @@ -1,15 +1,14 @@ getCode(), $e); + $self = new self(sprintf('Api method error: %s', $name), $e->getCode(), $e); $self->apiMethodName = $name; $self->args = $args; + return $self; } - } From 05974b6adf84c9bfced838bce8039cd3bcfe1341 Mon Sep 17 00:00:00 2001 From: polidog Date: Mon, 29 Jan 2018 01:00:04 +0900 Subject: [PATCH 3/7] Add test --- tests/ApiTest.php | 353 ++++++++++++++++++++++++++++++++++++ tests/Client/ClientTest.php | 53 ++++++ 2 files changed, 406 insertions(+) create mode 100644 tests/ApiTest.php create mode 100644 tests/Client/ClientTest.php diff --git a/tests/ApiTest.php b/tests/ApiTest.php new file mode 100644 index 0000000..5cca9ba --- /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 deleteEmoji() + { + $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']]); + } +} From 5a8488545893e8e6130c26cee0e76fe9cd6a06f9 Mon Sep 17 00:00:00 2001 From: polidog Date: Mon, 29 Jan 2018 01:00:39 +0900 Subject: [PATCH 4/7] update composer --- composer.json | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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" } From 382f8ed14de6ed1e56ccfd28dc56ad43dcd860a9 Mon Sep 17 00:00:00 2001 From: polidog Date: Mon, 29 Jan 2018 01:01:30 +0900 Subject: [PATCH 5/7] php cs fixer --- .gitignore | 1 + phpunit.xml.dist | 5 +-- src/Api.php | 109 ++++++++++++++++++++++++----------------------- 3 files changed, 58 insertions(+), 57 deletions(-) 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/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 index 9d2e255..74e8999 100644 --- a/src/Api.php +++ b/src/Api.php @@ -32,7 +32,8 @@ public function __construct(ClientInterface $client, $currentTeam) /** * @param array $params - * @param null $headers + * + * @return array */ public function user(array $params = []) { @@ -50,7 +51,7 @@ public function teams() } /** - * @param null $name + * @param string|null $name * * @return array */ @@ -64,7 +65,7 @@ public function team($name = null) */ public function stats() { - return $this->client->request('GET', "teams/{$this->currentTeam}/stats"); + return $this->client->request('GET', $this->getCurrentTeamUrl('stats')); } /** @@ -72,7 +73,7 @@ public function stats() */ public function members() { - return $this->client->request('GET', "teams/{$this->currentTeam}/members"); + return $this->client->request('GET', $this->getCurrentTeamUrl('members')); } /** @@ -82,29 +83,29 @@ public function members() */ public function posts(array $params = []) { - return $this->client->request('GET', "teams/{$this->currentTeam}/posts", [ + return $this->client->request('GET', $this->getCurrentTeamUrl('posts'), [ 'query' => $params, ]); } /** - * @param $number + * @param int $number * * @return array */ public function post($number) { - return $this->client->request('GET', "teams/{$this->currentTeam}/posts/{$number}"); + return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$number}")); } /** - * @param $data + * @param array $data * * @return array */ public function createPost($data) { - return $this->client->request('POST', "teams/{$this->currentTeam}/posts", [ + return $this->client->request('POST', $this->getCurrentTeamUrl('posts'), [ 'json' => [ 'post' => $data, ], @@ -112,14 +113,14 @@ public function createPost($data) } /** - * @param $number - * @param $data + * @param int $number + * @param array $data * * @return array */ - public function updatePost($number, $data) + public function updatePost($number, array $data) { - return $this->client->request('PATCH', "teams/{$this->currentTeam}/posts/{$number}", [ + return $this->client->request('PATCH', $this->getCurrentTeamUrl("posts/{$number}"), [ 'json' => [ 'post' => $data, ], @@ -127,13 +128,13 @@ public function updatePost($number, $data) } /** - * @param $number + * @param int $number * * @return array */ public function deletePost($number) { - return $this->client->request('DELETE', "teams/{$this->currentTeam}/posts/{$number}"); + return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$number}")); } /** @@ -142,41 +143,41 @@ public function deletePost($number) * * @return array */ - public function comments($number = null, $params = []) + public function comments($number = null, array $params = []) { if (empty($number)) { - return $this->client->request('GET', "teams/{$this->currentTeam}/comments", [ + return $this->client->request('GET', $this->getCurrentTeamUrl('comments'), [ 'query' => $params, ]); } - return $this->client->request('GET', "teams/{$this->currentTeam}/posts/{$number}/comments", [ + return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$number}/comments"), [ 'query' => $params, ]); } /** - * @param $commentId + * @param int $commentId * @param array $params * * @return array */ - public function comment($commentId, $params = []) + public function comment($commentId, array $params = []) { - return $this->client->request('GET', "teams/{$this->currentTeam}/comments/{$commentId}", [ + return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}"), [ 'query' => $params, ]); } /** - * @param $postNumber - * @param $data + * @param int $postNumber + * @param array $data * * @return array */ - public function createComment($postNumber, $data) + public function createComment($postNumber, array $data) { - return $this->client->request('POST', "teams/{$this->currentTeam}/posts/{$postNumber}/comments", [ + return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/comments"), [ 'json' => [ 'comment' => $data, ], @@ -184,14 +185,14 @@ public function createComment($postNumber, $data) } /** - * @param $commentId - * @param $data + * @param int $commentId + * @param array $data * * @return array */ - public function updateComment($commentId, $data) + public function updateComment($commentId, array $data) { - return $this->client->request('PATCH', "teams/{$this->currentTeam}/comments/{$commentId}", [ + return $this->client->request('PATCH', $this->getCurrentTeamUrl("comments/{$commentId}"), [ 'json' => [ 'comment' => $data, ], @@ -199,17 +200,17 @@ public function updateComment($commentId, $data) } /** - * @param $commentId + * @param int $commentId * * @return array */ public function deleteComment($commentId) { - return $this->client->request('DELETE', "teams/{$this->currentTeam}/comments/{$commentId}"); + return $this->client->request('DELETE', $this->getCurrentTeamUrl("comments/{$commentId}")); } /** - * @param $postNumber + * @param int $postNumber * * @return array */ @@ -219,7 +220,7 @@ public function createSharing($postNumber) } /** - * @param $postNumber + * @param int $postNumber * * @return array */ @@ -229,12 +230,12 @@ public function deleteSharing($postNumber) } /** - * @param $postNumber + * @param int $postNumber * @param array $params * * @return array */ - public function postStargazers($postNumber, $params = []) + public function postStargazers($postNumber, array $params = []) { return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/stargazers"), [ 'query' => $params, @@ -242,12 +243,12 @@ public function postStargazers($postNumber, $params = []) } /** - * @param $postNumber + * @param int $postNumber * @param array $params * * @return array */ - public function addPostStar($postNumber, $params = []) + public function addPostStar($postNumber, array $params = []) { return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/star"), [ 'json' => $params, @@ -255,7 +256,7 @@ public function addPostStar($postNumber, $params = []) } /** - * @param $postNumber + * @param int $postNumber * * @return array */ @@ -265,14 +266,14 @@ public function deletePostStar($postNumber) } /** - * @param $commentId + * @param int $commentId * @param array $params * * @return array */ - public function commentStargazers($commentId, $params = []) + public function commentStargazers($commentId, array $params = []) { - return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [ + return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}/stargazers"), [ 'query' => $params, ]); } @@ -283,7 +284,7 @@ public function commentStargazers($commentId, $params = []) * * @return array */ - public function addCommentStar($commentId, $params = []) + public function addCommentStar($commentId, array $params = []) { return $this->client->request('POST', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [ 'json' => $params, @@ -301,12 +302,12 @@ public function deleteCommentStar($commentId) } /** - * @param $postNumber + * @param integer$postNumber * @param array $params * * @return array */ - public function watchers($postNumber, $params = []) + public function watchers($postNumber, array $params = []) { return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/watchers"), [ 'query' => $params, @@ -320,7 +321,7 @@ public function watchers($postNumber, $params = []) */ public function addWatch($postNumber) { - return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); + return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); } /** @@ -346,7 +347,7 @@ public function categories() * * @return array */ - public function batchMoveCategory($params = []) + public function batchMoveCategory(array $params = []) { return $this->client->request('POST', $this->getCurrentTeamUrl('categories/batch_move'), [ 'json' => $params, @@ -382,7 +383,7 @@ public function regenerateInvitation() * * @return array */ - public function pendingInvitations(array $params) + public function pendingInvitations(array $params = []) { return $this->client->request('GET', $this->getCurrentTeamUrl('invitations'), [ 'query' => $params, @@ -390,15 +391,15 @@ public function pendingInvitations(array $params) } /** - * @param array $data + * @param array $emails * * @return array */ - public function sendInvitation(array $data) + public function sendInvitation(array $emails) { return $this->client->request('POST', $this->getCurrentTeamUrl('invitations'), [ 'json' => [ - 'members' => $data, + 'members' => ['emails' => $emails], ], ]); } @@ -420,7 +421,7 @@ public function cancelInvitation($code) */ public function emojis(array $params = []) { - return $this->client->request('GET', "teams/{$this->currentTeam}/emojis", [ + return $this->client->request('GET', $this->getCurrentTeamUrl('emojis'), [ 'query' => $params, ]); } @@ -430,9 +431,9 @@ public function emojis(array $params = []) * * @return array */ - public function createEmoji($data) + public function createEmoji(array $data) { - return $this->client->request('POST', "teams/{$this->currentTeam}/emojis", [ + return $this->client->request('POST', $this->getCurrentTeamUrl('emojis'), [ 'json' => [ 'emoji' => $data, ], @@ -457,7 +458,7 @@ public function deleteEmoji($code) */ public static function factory($accessToken, $currentTeam) { - $client = Client::factory($accessToken, $currentTeam); + $client = Client::factory($accessToken); return new self($client, $currentTeam); } From 1e7f858aa4408c348963f738ae7cc69e7e59ddff Mon Sep 17 00:00:00 2001 From: polidog Date: Mon, 29 Jan 2018 01:08:43 +0900 Subject: [PATCH 6/7] add deleteEmoji test. --- tests/ApiTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 5cca9ba..c3bc8f9 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -339,7 +339,7 @@ public function testCreateEmoji() ])->shouldHaveBeenCalled(); } - public function deleteEmoji() + public function testDeleteEmoji() { $api = $this->getApiObject(); $api->deleteEmoji('code'); From 0e653efff791a657354aaead4650c86887abe8df Mon Sep 17 00:00:00 2001 From: polidog Date: Mon, 29 Jan 2018 01:09:17 +0900 Subject: [PATCH 7/7] Update README.md --- README.md | 80 +++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) 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 ```