├── .gitignore ├── src ├── Client │ ├── ClientInterface.php │ ├── Authorization.php │ └── Client.php ├── Exception │ └── ClientException.php └── Api.php ├── .php-cs-fixer.dist.php ├── .github └── workflows │ └── test.yaml ├── phpunit.xml.dist ├── composer.json ├── .scrutinizer.yml ├── LICENSE ├── tests ├── Client │ ├── AuthorizationTest.php │ └── ClientTest.php └── ApiTest.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | demo.php 3 | vendor 4 | .idea 5 | .php_cs.cache 6 | composer.lock 7 | .php-cs-fixer.cache 8 | .phpunit.result.cache 9 | -------------------------------------------------------------------------------- /src/Client/ClientInterface.php: -------------------------------------------------------------------------------- 1 | in(__DIR__) 5 | ->exclude('vendor'); 6 | 7 | return (new PhpCsFixer\Config()) 8 | ->setRules([ 9 | '@PSR12' => true, 10 | 'strict_param' => true, 11 | 'array_syntax' => ['syntax' => 'short'], 12 | '@PHP80Migration:risky' => true, 13 | ]) 14 | ->setRiskyAllowed(true) 15 | ->setFinder($finder) 16 | ; 17 | -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: ['push'] 4 | 5 | jobs: 6 | test: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | max-parallel: 5 10 | matrix: 11 | php-versions: [ '8.0', '8.1', '8.2' ] 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - name: Run composer install 16 | run: | 17 | composer install 18 | 19 | - name: phpunit 20 | run: | 21 | composer test 22 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests/ 6 | 7 | 8 | 9 | 10 | 11 | ./ 12 | 13 | ./vendor/ 14 | ./tests/ 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Client/Authorization.php: -------------------------------------------------------------------------------- 1 | push(Middleware::mapRequest([$this, 'handle'])); 21 | } 22 | 23 | public function handle(RequestInterface $request): MessageInterface 24 | { 25 | return $request->withHeader('Authorization', 'Bearer '.$this->accessToken); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "polidog/esa-php", 3 | "description": "esa.io api library", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Ryota Mochizuki", 8 | "email": "polidogs@gmail.com" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=8.0", 13 | "ext-json": "*", 14 | "guzzlehttp/guzzle": "~7.0" 15 | }, 16 | "require-dev": { 17 | "phpunit/phpunit": "^9.6", 18 | "friendsofphp/php-cs-fixer": "^3.23", 19 | "phpspec/prophecy-phpunit": "^2.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Polidog\\Esa\\": "src" 24 | } 25 | }, 26 | "autoload-dev": { 27 | "psr-4": { 28 | "Polidog\\Esa\\Test\\": "tests" 29 | } 30 | }, 31 | "scripts": { 32 | "test": "./vendor/bin/phpunit", 33 | "cs-fix": "./vendor/bin/php-cs-fixer fix" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | filter: 2 | paths: 3 | - 'src/*' 4 | excluded_paths: 5 | - 'tests/*' 6 | checks: 7 | php: true 8 | 9 | tools: 10 | php_cpd: 11 | enabled: true 12 | excluded_dirs: { } 13 | names: 14 | - '*.php' 15 | min_lines: 5 16 | min_tokens: 70 17 | filter: 18 | paths: 19 | - 'src/*' 20 | php_pdepend: 21 | excluded_dirs: 22 | - vendor 23 | enabled: true 24 | configuration_file: null 25 | suffixes: 26 | - php 27 | filter: 28 | paths: 29 | - 'src/*' 30 | php_changetracking: 31 | enabled: true 32 | bug_patterns: 33 | - '\bfix(?:es|ed)?\b' 34 | feature_patterns: 35 | - '\badd(?:s|ed)?\b' 36 | - '\bimplement(?:s|ed)?\b' 37 | filter: 38 | paths: 39 | - 'src/*' 40 | external_code_coverage: 41 | timeout: 300 42 | runs: 1 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ryota Mochizuki 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Exception/ClientException.php: -------------------------------------------------------------------------------- 1 | method; 30 | } 31 | 32 | /** 33 | * @return string 34 | */ 35 | public function getPath() 36 | { 37 | return $this->path; 38 | } 39 | 40 | /** 41 | * @return array 42 | */ 43 | public function getParams() 44 | { 45 | return $this->params; 46 | } 47 | 48 | /** 49 | * @param $method 50 | * @param $path 51 | * 52 | * @return ClientException 53 | */ 54 | public static function newException(\Exception $e, $method, $path, array $params) 55 | { 56 | $self = new self(sprintf('Api method error: %s : %s', $method, $path), $e->getCode(), $e); 57 | $self->method = $method; 58 | $self->path = $path; 59 | $self->params = $params; 60 | 61 | return $self; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /tests/Client/AuthorizationTest.php: -------------------------------------------------------------------------------- 1 | prophesize(RequestInterface::class); 23 | $message = $this->prophesize(MessageInterface::class); 24 | 25 | $request->withHeader('Authorization', 'Bearer '.$token)->willReturn($message); 26 | 27 | $authorization = new Authorization($token); 28 | $authorization->handle($request->reveal()); 29 | 30 | $request->withHeader('Authorization', 'Bearer '.$token) 31 | ->shouldHaveBeenCalled(); 32 | } 33 | 34 | public function testPush(): void 35 | { 36 | $handlerStack = $this->prophesize(HandlerStack::class); 37 | 38 | $authorization = new Authorization('token'); 39 | $authorization->push($handlerStack->reveal()); 40 | 41 | $handlerStack->push(Argument::any()) 42 | ->shouldHaveBeenCalled(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Client/Client.php: -------------------------------------------------------------------------------- 1 | 'https://api.esa.io/v1/', 19 | 'timeout' => 60, 20 | 'allow_redirect' => false, 21 | 'headers' => [ 22 | 'User-Agent' => 'esa-php-api v2', 23 | 'Accept' => 'application/json', 24 | ], 25 | ]; 26 | 27 | public function __construct(private HttpClientInterface $httpClient) 28 | { 29 | } 30 | 31 | /** 32 | * @throws ClientException 33 | * @throws \RuntimeException 34 | * @throws \JsonException 35 | */ 36 | public function request(string $method, string $path, array $data = []): array 37 | { 38 | try { 39 | $response = $this->httpClient->request($method, $path, $data); 40 | 41 | return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); 42 | } catch (GuzzleException $e) { 43 | throw ClientException::newException($e, $method, $path, $data); 44 | } 45 | } 46 | 47 | public static function factory(string $accessToken, array $httpOptions = []): self 48 | { 49 | $httpOptions = array_merge(self::$httpOptions, $httpOptions); 50 | $authorization = new Authorization($accessToken); 51 | 52 | $httpOptions['handler'] = HandlerStack::create(); 53 | $authorization->push($httpOptions['handler']); 54 | 55 | return new self(new \GuzzleHttp\Client($httpOptions)); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /tests/Client/ClientTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf(Client::class, $client); 25 | } 26 | 27 | public function testRequest(): void 28 | { 29 | $httpClient = $this->prophesize(HttpClientInterface::class); 30 | $response = $this->prophesize(ResponseInterface::class); 31 | $stream = $this->prophesize(StreamInterface::class); 32 | 33 | $stream->getContents() 34 | ->willReturn(json_encode(['a' => 'b'])); 35 | 36 | $response->getBody() 37 | ->willReturn($stream->reveal()); 38 | 39 | $httpClient->request(Argument::any(), Argument::any(), Argument::any()) 40 | ->willReturn($response->reveal()); 41 | 42 | $client = new Client($httpClient->reveal()); 43 | $client->request('GET', '/test', ['query' => ['a' => 'b']]); 44 | $httpClient->request('GET', '/test', ['query' => ['a' => 'b']]); 45 | 46 | 47 | $stream->getContents() 48 | ->willReturn(json_encode(['a' => 'b'])) 49 | ->shouldHaveBeenCalledOnce(); 50 | 51 | $response->getBody() 52 | ->willReturn($stream->reveal()) 53 | ->shouldHaveBeenCalledOnce(); 54 | 55 | $httpClient->request(Argument::any(), Argument::any(), Argument::any()) 56 | ->willReturn($response->reveal()) 57 | ->shouldHaveBeenCalledOnce(); 58 | } 59 | 60 | public function testRequestException(): void 61 | { 62 | $this->expectException(ClientException::class); 63 | 64 | $httpClient = $this->prophesize(HttpClientInterface::class); 65 | $httpClient->request('GET', '/test', ['query' => ['a' => 'b']])->willThrow( 66 | new TransferException() 67 | ); 68 | $client = new Client($httpClient->reveal()); 69 | $client->request('GET', '/test', ['query' => ['a' => 'b']]); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esa-php 2 | 3 | ![test](https://github.com/polidog/esa-php/workflows/test/badge.svg) 4 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/polidog/esa-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/polidog/esa-php/?branch=master) 5 | [![Latest Stable Version](https://poser.pugx.org/polidog/esa-php/v/stable)](https://packagist.org/packages/polidog/esa-php) 6 | [![Total Downloads](https://poser.pugx.org/polidog/esa-php/downloads)](https://packagist.org/packages/polidog/esa-php) 7 | [![License](https://poser.pugx.org/polidog/esa-php/license)](https://packagist.org/packages/polidog/esa-php) 8 | 9 | esa API v1 client library, written in PHP 10 | 11 | ## Installation 12 | 13 | The recommended way to install esa-php is through Composer. 14 | 15 | ``` 16 | # Install Composer 17 | curl -sS https://getcomposer.org/installer | php 18 | ``` 19 | Next, run the Composer command to install the lasted stable version of esa-php. 20 | 21 | ``` 22 | composer.phar require polidog/esa-php 23 | ``` 24 | 25 | ## Usage 26 | 27 | ``` 28 | ", "foobar"); 33 | 34 | $api->user(); 35 | // GET /v1/user 36 | 37 | $api->teams(); 38 | // GET /v1/teams 39 | 40 | $api->team('bar'); 41 | // GET /v1/teams/bar 42 | 43 | $api->stats() 44 | // GET /v1/teams/foobar/stats 45 | 46 | $api->members(); 47 | // GET /v1/teams/foobar/members 48 | 49 | $api->posts(); 50 | // GET /v1/teams/foobar/posts 51 | 52 | $api->posts(["q" => "in:help"]); 53 | // GET /v1/teams/foobar/posts?q=in%3Ahelp 54 | 55 | $api->createPost(["name" => "foo"]); 56 | // POST /v1/teams/foobar/posts 57 | 58 | $api->updatePost(1, ["name" => "bar"]); 59 | // PATCH /v1/teams/foobar/posts/1 60 | 61 | $api->deletePost(1); 62 | // DELETE /v1/teams/foobar/posts/1 63 | 64 | // Comment API 65 | $api->comments(1); /* post number */ 66 | // GET /v1/teams/foobar/posts/1/comments 67 | 68 | $api->createComment(1, ['body_md' => 'baz']); 69 | // POST /v1/teams/foobar/posts/1/comments 70 | 71 | $api->comment(123); /* comment id */ 72 | // GET /v1/teams/foobar/comments/123 73 | 74 | $api->updateComment(123, ['body_md' => 'bazbaz']); 75 | // PATCH /v1/teams/foobar/comments/123 76 | 77 | $api->deleteComment(123); 78 | // DELETE /v1/teams/foobar/comments/123 79 | 80 | $api->comments(); 81 | // GET /v1/teams/foobar/comments 82 | 83 | $api->createSharing(1); 84 | // POST /v1/teams/foobar/posts/1/sharing 85 | 86 | $api->deleteSharing(1); 87 | // DELETE /v1/teams/foobar/posts/1/sharing 88 | 89 | 90 | # Star API 91 | $api->postStargazers(1); 92 | // GET /v1/teams/foobar/posts/1/stargazers 93 | 94 | $api->addPostStar(1); 95 | // POST /v1/teams/foobar/posts/1/star 96 | 97 | $api->deletePostStar(1); 98 | // DELETE /v1/teams/foobar/posts/1/star 99 | 100 | $api->commentStargazers(123); 101 | // GET /v1/teams/foobar/comments/123/stargazers 102 | 103 | $api->addCommentStar(123); 104 | // POST /v1/teams/foobar/comments/123/star 105 | 106 | $api->deleteCommentStar(123); 107 | // DELETE /v1/teams/foobar/comments/123/star 108 | 109 | 110 | # Watch API 111 | $api->watchers(1); 112 | // GET /v1/teams/foobar/posts/1/watchers 113 | 114 | $api->addWatch(1); 115 | // POST /v1/teams/foobar/posts/1/watch 116 | 117 | $api->deleteWtach(1); 118 | // DELETE /v1/teams/foobar/posts/1/watch 119 | 120 | # Categories API 121 | $api->categories(); 122 | // GET /v1/teams/foobar/categories 123 | 124 | # Tags API 125 | $api->tags(); 126 | // GET /v1/teams/foobar/tags 127 | 128 | # Invitation API 129 | $api->invitation(); 130 | // GET /v1/teams/foobar/invitation 131 | 132 | $api->regenerateInvitation(); 133 | // POST /v1/teams/foobar/invitation_regenerator 134 | 135 | $api->pendingInvitations(); 136 | // GET /v1/teams/foobar/invitations 137 | 138 | $api->sendInvitation(['test@test.com','test2@test.com']); 139 | // POST /v1/teams/foobar/invitations 140 | 141 | $api->cancelInvitation($code); 142 | // DELETE /v1/teams/foobar/invitations/baz 143 | 144 | # Emoji API 145 | $api->emojis(); 146 | // GET /v1/teams/foobar/emojis 147 | 148 | $api->createEmoji(['code' => 'team_emoji', image: '/path/to/image'); 149 | // POST /v1/teams/foobar/emojis 150 | 151 | $api->createEmoji(['code' => 'alias_code', origin_code: 'team_emoji'); 152 | // POST /v1/teams/foobar/emojis 153 | 154 | $api->deleteEmoji('team_emoji'); 155 | // DELETE /v1/teams/foobar/emojis/team_emoji 156 | ``` 157 | 158 | ## Contributing 159 | 160 | 1. Fork it ( https://github.com/polidog/esa-php/fork ) 161 | 2. Create your feature branch (`git checkout -b my-new-feature`) 162 | 3. Commit your changes (`git commit -am 'Add some feature'`) 163 | 4. Push to the branch (`git push origin my-new-feature`) 164 | 5. Create a new Pull Request 165 | 166 | -------------------------------------------------------------------------------- /src/Api.php: -------------------------------------------------------------------------------- 1 | client->request('GET', 'user', [ 22 | 'query' => $params, 23 | ]); 24 | } 25 | 26 | public function teams(): array 27 | { 28 | return $this->client->request('GET', 'teams'); 29 | } 30 | 31 | public function team(string $name = null): array 32 | { 33 | return $this->client->request('GET', "teams/{$name}"); 34 | } 35 | 36 | public function stats(): array 37 | { 38 | return $this->client->request('GET', $this->getCurrentTeamUrl('stats')); 39 | } 40 | 41 | public function members(): array 42 | { 43 | return $this->client->request('GET', $this->getCurrentTeamUrl('members')); 44 | } 45 | 46 | public function posts(array $params = []): array 47 | { 48 | return $this->client->request('GET', $this->getCurrentTeamUrl('posts'), [ 49 | 'query' => $params, 50 | ]); 51 | } 52 | 53 | public function post(int $number): array 54 | { 55 | return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$number}")); 56 | } 57 | 58 | public function createPost(array $data): array 59 | { 60 | return $this->client->request('POST', $this->getCurrentTeamUrl('posts'), [ 61 | 'json' => [ 62 | 'post' => $data, 63 | ], 64 | ]); 65 | } 66 | 67 | public function updatePost(int $number, array $data): array 68 | { 69 | return $this->client->request('PATCH', $this->getCurrentTeamUrl("posts/{$number}"), [ 70 | 'json' => [ 71 | 'post' => $data, 72 | ], 73 | ]); 74 | } 75 | 76 | public function deletePost(int $number): array 77 | { 78 | return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$number}")); 79 | } 80 | 81 | public function comments(int $number = null, array $params = []): array 82 | { 83 | if (null === $number) { 84 | return $this->client->request('GET', $this->getCurrentTeamUrl('comments'), [ 85 | 'query' => $params, 86 | ]); 87 | } 88 | 89 | return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$number}/comments"), [ 90 | 'query' => $params, 91 | ]); 92 | } 93 | 94 | public function comment(int $commentId, array $params = []): array 95 | { 96 | return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}"), [ 97 | 'query' => $params, 98 | ]); 99 | } 100 | 101 | public function createComment(int $postNumber, array $data): array 102 | { 103 | return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/comments"), [ 104 | 'json' => [ 105 | 'comment' => $data, 106 | ], 107 | ]); 108 | } 109 | 110 | public function updateComment(int $commentId, array $data): array 111 | { 112 | return $this->client->request('PATCH', $this->getCurrentTeamUrl("comments/{$commentId}"), [ 113 | 'json' => [ 114 | 'comment' => $data, 115 | ], 116 | ]); 117 | } 118 | 119 | public function deleteComment(int $commentId): array 120 | { 121 | return $this->client->request('DELETE', $this->getCurrentTeamUrl("comments/{$commentId}")); 122 | } 123 | 124 | public function createSharing(int $postNumber): array 125 | { 126 | return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); 127 | } 128 | 129 | public function deleteSharing(int $postNumber): array 130 | { 131 | return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/sharing")); 132 | } 133 | 134 | public function postStargazers(int $postNumber, array $params = []): array 135 | { 136 | return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/stargazers"), [ 137 | 'query' => $params, 138 | ]); 139 | } 140 | 141 | public function addPostStar(int $postNumber, array $params = []): array 142 | { 143 | return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/star"), [ 144 | 'json' => $params, 145 | ]); 146 | } 147 | 148 | public function deletePostStar(int $postNumber): array 149 | { 150 | return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/star")); 151 | } 152 | 153 | public function commentStargazers(int $commentId, array $params = []): array 154 | { 155 | return $this->client->request('GET', $this->getCurrentTeamUrl("comments/{$commentId}/stargazers"), [ 156 | 'query' => $params, 157 | ]); 158 | } 159 | 160 | public function addCommentStar(int $commentId, array $params = []): array 161 | { 162 | return $this->client->request('POST', $this->getCurrentTeamUrl("comments/{$commentId}/star"), [ 163 | 'json' => $params, 164 | ]); 165 | } 166 | 167 | public function deleteCommentStar(int $commentId): array 168 | { 169 | return $this->client->request('DELETE', $this->getCurrentTeamUrl("comments/{$commentId}/star")); 170 | } 171 | 172 | public function watchers(int $postNumber, array $params = []): array 173 | { 174 | return $this->client->request('GET', $this->getCurrentTeamUrl("posts/{$postNumber}/watchers"), [ 175 | 'query' => $params, 176 | ]); 177 | } 178 | 179 | public function addWatch(int $postNumber): array 180 | { 181 | return $this->client->request('POST', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); 182 | } 183 | 184 | public function deleteWatch(int $postNumber): array 185 | { 186 | return $this->client->request('DELETE', $this->getCurrentTeamUrl("posts/{$postNumber}/watch")); 187 | } 188 | 189 | public function categories(): array 190 | { 191 | return $this->client->request('GET', $this->getCurrentTeamUrl('categories')); 192 | } 193 | 194 | public function batchMoveCategory(array $params = []): array 195 | { 196 | return $this->client->request('POST', $this->getCurrentTeamUrl('categories/batch_move'), [ 197 | 'json' => $params, 198 | ]); 199 | } 200 | 201 | public function tags(): array 202 | { 203 | return $this->client->request('GET', $this->getCurrentTeamUrl('tags')); 204 | } 205 | 206 | public function invitation(): array 207 | { 208 | return $this->client->request('GET', $this->getCurrentTeamUrl('invitation')); 209 | } 210 | 211 | public function regenerateInvitation(): array 212 | { 213 | return $this->client->request('POST', $this->getCurrentTeamUrl('invitation_regenerator')); 214 | } 215 | 216 | public function pendingInvitations(array $params = []): array 217 | { 218 | return $this->client->request('GET', $this->getCurrentTeamUrl('invitations'), [ 219 | 'query' => $params, 220 | ]); 221 | } 222 | 223 | public function sendInvitation(array $emails): array 224 | { 225 | return $this->client->request('POST', $this->getCurrentTeamUrl('invitations'), [ 226 | 'json' => [ 227 | 'members' => ['emails' => $emails], 228 | ], 229 | ]); 230 | } 231 | 232 | public function cancelInvitation(string $code): array 233 | { 234 | return $this->client->request('DELETE', $this->getCurrentTeamUrl("invitations/{$code}")); 235 | } 236 | 237 | public function emojis(array $params = []): array 238 | { 239 | return $this->client->request('GET', $this->getCurrentTeamUrl('emojis'), [ 240 | 'query' => $params, 241 | ]); 242 | } 243 | 244 | public function createEmoji(array $data): array 245 | { 246 | return $this->client->request('POST', $this->getCurrentTeamUrl('emojis'), [ 247 | 'json' => [ 248 | 'emoji' => $data, 249 | ], 250 | ]); 251 | } 252 | 253 | public function deleteEmoji(string $code): array 254 | { 255 | return $this->client->request('DELETE', "teams/{$this->currentTeam}/emojis/{$code}"); 256 | } 257 | 258 | public static function factory(string $accessToken, string $currentTeam): self 259 | { 260 | $client = Client::factory($accessToken); 261 | 262 | return new self($client, $currentTeam); 263 | } 264 | 265 | private function getCurrentTeamUrl(string $path = ''): string 266 | { 267 | return "teams/{$this->currentTeam}/$path"; 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /tests/ApiTest.php: -------------------------------------------------------------------------------- 1 | client = $this->prophesize(ClientInterface::class); 24 | } 25 | 26 | public function testFactory(): void 27 | { 28 | $api = Api::factory('token', 'team'); 29 | $this->assertInstanceOf(Api::class, $api); 30 | } 31 | 32 | public function testUser(): void 33 | { 34 | $this->client->request('GET', 'user', [ 35 | 'query' => [], 36 | ])->willReturn([]); 37 | 38 | $api = $this->getApiObject(); 39 | $api->user(); 40 | $this->client->request('GET', 'user', [ 41 | 'query' => [], 42 | ])->shouldHaveBeenCalled(); 43 | } 44 | 45 | public function testTeams(): void 46 | { 47 | $this->client->request('GET', 'teams')->willReturn([]); 48 | $api = $this->getApiObject(); 49 | $api->teams(); 50 | $this->client->request('GET', 'teams')->shouldHaveBeenCalled(); 51 | } 52 | 53 | public function testTeam(): void 54 | { 55 | $this->client->request('GET', 'teams/team_name')->willReturn([]); 56 | $api = $this->getApiObject(); 57 | $api->team('team_name'); 58 | $this->client->request('GET', 'teams/team_name')->shouldHaveBeenCalled(); 59 | } 60 | 61 | public function testStats(): void 62 | { 63 | $this->client->request('GET', 'teams/test/stats')->willReturn([]); 64 | 65 | $api = $this->getApiObject(); 66 | $api->stats(); 67 | $this->client->request('GET', 'teams/test/stats')->shouldHaveBeenCalled(); 68 | } 69 | 70 | public function testMembers(): void 71 | { 72 | $this->client->request('GET', 'teams/test/members')->willReturn([]); 73 | $api = $this->getApiObject(); 74 | $api->members(); 75 | $this->client->request('GET', 'teams/test/members')->shouldHaveBeenCalled(); 76 | } 77 | 78 | public function testPosts(): void 79 | { 80 | $this->client->request('GET', 'teams/test/posts', [ 81 | 'query' => [], 82 | ])->willReturn([]); 83 | 84 | $api = $this->getApiObject(); 85 | $api->posts(); 86 | $this->client->request('GET', 'teams/test/posts', [ 87 | 'query' => [], 88 | ])->shouldHaveBeenCalled(); 89 | } 90 | 91 | public function testPost(): void 92 | { 93 | $this->client->request('GET', 'teams/test/posts/1')->willReturn([]); 94 | 95 | $api = $this->getApiObject(); 96 | $api->post(1); 97 | $this->client->request('GET', 'teams/test/posts/1')->shouldHaveBeenCalled(); 98 | } 99 | 100 | public function testCreatePost(): void 101 | { 102 | $this->client->request('POST', 'teams/test/posts', [ 103 | 'json' => [ 104 | 'post' => ['name' => 'foo'], 105 | ], 106 | ])->willReturn([]); 107 | 108 | $api = $this->getApiObject(); 109 | $api->createPost(['name' => 'foo']); 110 | $this->client->request('POST', 'teams/test/posts', [ 111 | 'json' => [ 112 | 'post' => ['name' => 'foo'], 113 | ], 114 | ])->shouldHaveBeenCalled(); 115 | } 116 | 117 | public function testUpdatePost(): void 118 | { 119 | $this->client->request('PATCH', 'teams/test/posts/12', [ 120 | 'json' => [ 121 | 'post' => ['name' => 'bar'], 122 | ], 123 | ])->willReturn([]); 124 | 125 | $api = $this->getApiObject(); 126 | $api->updatePost(12, ['name' => 'bar']); 127 | $this->client->request('PATCH', 'teams/test/posts/12', [ 128 | 'json' => [ 129 | 'post' => ['name' => 'bar'], 130 | ], 131 | ])->shouldHaveBeenCalled(); 132 | } 133 | 134 | public function testDeletePost(): void 135 | { 136 | $this->client->request('DELETE', 'teams/test/posts/13')->willReturn([]); 137 | 138 | $api = $this->getApiObject(); 139 | $api->deletePost(13); 140 | $this->client->request('DELETE', 'teams/test/posts/13')->shouldHaveBeenCalled(); 141 | } 142 | 143 | public function testComments(): void 144 | { 145 | $this->client->request('GET', 'teams/test/comments', [ 146 | 'query' => [], 147 | ])->willReturn([]); 148 | 149 | $api = $this->getApiObject(); 150 | $api->comments(); 151 | $this->client->request('GET', 'teams/test/comments', [ 152 | 'query' => [], 153 | ])->shouldHaveBeenCalled(); 154 | } 155 | 156 | public function testCommentsById(): void 157 | { 158 | $this->client->request('GET', 'teams/test/posts/1/comments', [ 159 | 'query' => [], 160 | ])->willReturn([]); 161 | 162 | $api = $this->getApiObject(); 163 | $api->comments(1); 164 | $this->client->request('GET', 'teams/test/posts/1/comments', [ 165 | 'query' => [], 166 | ])->shouldHaveBeenCalled(); 167 | } 168 | 169 | public function testComment(): void 170 | { 171 | $this->client->request('GET', 'teams/test/comments/1', ['query' => []])->willReturn([]); 172 | $api = $this->getApiObject(); 173 | $api->comment(1); 174 | $this->client->request('GET', 'teams/test/comments/1', ['query' => []])->shouldHaveBeenCalled(); 175 | } 176 | 177 | public function testCreateComment(): void 178 | { 179 | $this->client->request('POST', 'teams/test/posts/1/comments', ['json' => [ 180 | 'comment' => [ 181 | 'body_md' => 'baz', 182 | ], 183 | ]])->willReturn([]); 184 | 185 | $api = $this->getApiObject(); 186 | $api->createComment(1, ['body_md' => 'baz']); 187 | $this->client->request('POST', 'teams/test/posts/1/comments', ['json' => [ 188 | 'comment' => [ 189 | 'body_md' => 'baz', 190 | ], 191 | ]])->shouldHaveBeenCalled(); 192 | } 193 | 194 | public function testUpdateComment(): void 195 | { 196 | $this->client->request('PATCH', 'teams/test/comments/1', ['json' => [ 197 | 'comment' => [ 198 | 'body_md' => 'foo', 199 | ], 200 | ]])->willReturn([]); 201 | 202 | $api = $this->getApiObject(); 203 | $api->updateComment(1, ['body_md' => 'foo']); 204 | $this->client->request('PATCH', 'teams/test/comments/1', ['json' => [ 205 | 'comment' => [ 206 | 'body_md' => 'foo', 207 | ], 208 | ]])->shouldHaveBeenCalled(); 209 | } 210 | 211 | public function testDeleteCommand(): void 212 | { 213 | $this->client->request('DELETE', 'teams/test/comments/1')->willReturn([]); 214 | 215 | $api = $this->getApiObject(); 216 | $api->deleteComment(1); 217 | $this->client->request('DELETE', 'teams/test/comments/1')->shouldHaveBeenCalled(); 218 | } 219 | 220 | public function testCreateSharing(): void 221 | { 222 | $this->client->request('POST', 'teams/test/posts/1/sharing')->willReturn([]); 223 | 224 | $api = $this->getApiObject(); 225 | $api->createSharing(1); 226 | $this->client->request('POST', 'teams/test/posts/1/sharing')->shouldHaveBeenCalled(); 227 | } 228 | 229 | public function testDeleteSharing(): void 230 | { 231 | $this->client->request('DELETE', 'teams/test/posts/1/sharing')->willReturn([]); 232 | 233 | $api = $this->getApiObject(); 234 | $api->deleteSharing(1); 235 | $this->client->request('DELETE', 'teams/test/posts/1/sharing')->shouldHaveBeenCalled(); 236 | } 237 | 238 | public function testPostStargazers(): void 239 | { 240 | $this->client->request('GET', 'teams/test/posts/1/stargazers', [ 241 | 'query' => [], 242 | ])->willReturn([]); 243 | 244 | $api = $this->getApiObject(); 245 | $api->postStargazers(1); 246 | $this->client->request('GET', 'teams/test/posts/1/stargazers', [ 247 | 'query' => [], 248 | ])->shouldHaveBeenCalled(); 249 | } 250 | 251 | public function testAddPostStar(): void 252 | { 253 | $this->client->request('POST', 'teams/test/posts/1/star', [ 254 | 'json' => [ 255 | 'body' => 'foo bar', 256 | ], 257 | ])->willReturn([]); 258 | 259 | $api = $this->getApiObject(); 260 | $api->addPostStar(1, ['body' => 'foo bar']); 261 | $this->client->request('POST', 'teams/test/posts/1/star', [ 262 | 'json' => [ 263 | 'body' => 'foo bar', 264 | ], 265 | ])->shouldHaveBeenCalled(); 266 | } 267 | 268 | public function testDeletePostStar(): void 269 | { 270 | $this->client->request('DELETE', 'teams/test/posts/1/star')->willReturn([]); 271 | 272 | $api = $this->getApiObject(); 273 | $api->deletePostStar(1); 274 | $this->client->request('DELETE', 'teams/test/posts/1/star')->shouldHaveBeenCalled(); 275 | } 276 | 277 | public function testCommentStargazers(): void 278 | { 279 | $this->client->request('GET', 'teams/test/comments/1/stargazers', ['query' => []])->willReturn([]); 280 | 281 | $api = $this->getApiObject(); 282 | $api->commentStargazers(1); 283 | $this->client->request('GET', 'teams/test/comments/1/stargazers', ['query' => []])->shouldHaveBeenCalled(); 284 | } 285 | 286 | public function testAddCommentStar(): void 287 | { 288 | $this->client->request('POST', 'teams/test/comments/1/star', [ 289 | 'json' => [ 290 | 'body' => 'foo bar', 291 | ], 292 | ])->willReturn([]); 293 | 294 | $api = $this->getApiObject(); 295 | $api->addCommentStar(1, ['body' => 'foo bar']); 296 | $this->client->request('POST', 'teams/test/comments/1/star', [ 297 | 'json' => [ 298 | 'body' => 'foo bar', 299 | ], 300 | ])->shouldHaveBeenCalled(); 301 | } 302 | 303 | public function testDeleteCommentStar(): void 304 | { 305 | $this->client->request('DELETE', 'teams/test/comments/1/star')->willReturn([]); 306 | 307 | $api = $this->getApiObject(); 308 | $api->deleteCommentStar(1); 309 | $this->client->request('DELETE', 'teams/test/comments/1/star')->shouldHaveBeenCalled(); 310 | } 311 | 312 | public function testWatchers(): void 313 | { 314 | $this->client->request('GET', 'teams/test/posts/1/watchers', ['query' => []])->willReturn([]); 315 | 316 | $api = $this->getApiObject(); 317 | $api->watchers(1); 318 | $this->client->request('GET', 'teams/test/posts/1/watchers', ['query' => []])->shouldHaveBeenCalled(); 319 | } 320 | 321 | public function testAddWatch(): void 322 | { 323 | $this->client->request('POST', 'teams/test/posts/1/watch')->willReturn([]); 324 | 325 | $api = $this->getApiObject(); 326 | $api->addWatch(1); 327 | $this->client->request('POST', 'teams/test/posts/1/watch')->shouldHaveBeenCalled(); 328 | } 329 | 330 | public function testDeleteWatch(): void 331 | { 332 | $this->client->request('DELETE', 'teams/test/posts/1/watch')->willReturn([]); 333 | 334 | $api = $this->getApiObject(); 335 | $api->deleteWatch(1); 336 | $this->client->request('DELETE', 'teams/test/posts/1/watch')->shouldHaveBeenCalled(); 337 | } 338 | 339 | public function testCategories(): void 340 | { 341 | $this->client->request('GET', 'teams/test/categories')->willReturn([]); 342 | 343 | $api = $this->getApiObject(); 344 | $api->categories(); 345 | $this->client->request('GET', 'teams/test/categories')->shouldHaveBeenCalled(); 346 | } 347 | 348 | public function testBatchMoveCategory(): void 349 | { 350 | $this->client->request('POST', 'teams/test/categories/batch_move', [ 351 | 'json' => [ 352 | 'from' => '/foo/bar', 353 | 'to' => '/biz', 354 | ], 355 | ])->willReturn([]); 356 | 357 | $api = $this->getApiObject(); 358 | $api->batchMoveCategory([ 359 | 'from' => '/foo/bar', 360 | 'to' => '/biz', 361 | ]); 362 | 363 | $this->client->request('POST', 'teams/test/categories/batch_move', [ 364 | 'json' => [ 365 | 'from' => '/foo/bar', 366 | 'to' => '/biz', 367 | ], 368 | ])->shouldHaveBeenCalled(); 369 | } 370 | 371 | public function testTags(): void 372 | { 373 | $this->client->request('GET', 'teams/test/tags')->willReturn([]); 374 | 375 | $api = $this->getApiObject(); 376 | $api->tags(); 377 | 378 | $this->client->request('GET', 'teams/test/tags')->shouldHaveBeenCalled(); 379 | } 380 | 381 | public function testInvitation(): void 382 | { 383 | $this->client->request('GET', 'teams/test/invitation')->willReturn([]); 384 | 385 | $api = $this->getApiObject(); 386 | $api->invitation(); 387 | $this->client->request('GET', 'teams/test/invitation')->shouldHaveBeenCalled(); 388 | } 389 | 390 | public function testRegenerateInvitation(): void 391 | { 392 | $this->client->request('POST', 'teams/test/invitation_regenerator')->willReturn([]); 393 | 394 | $api = $this->getApiObject(); 395 | $api->regenerateInvitation(); 396 | $this->client->request('POST', 'teams/test/invitation_regenerator')->shouldHaveBeenCalled(); 397 | } 398 | 399 | public function testPendingInvitations(): void 400 | { 401 | $this->client->request('GET', 'teams/test/invitations', ['query' => []])->willReturn([]); 402 | 403 | $api = $this->getApiObject(); 404 | $api->pendingInvitations(); 405 | $this->client->request('GET', 'teams/test/invitations', ['query' => []])->shouldHaveBeenCalled(); 406 | } 407 | 408 | public function testSendInvitation(): void 409 | { 410 | $this->client->request('POST', 'teams/test/invitations', ['json' => [ 411 | 'members' => [ 412 | 'emails' => ['polidogs@gmail.com'], 413 | ], 414 | ]])->willReturn([]); 415 | 416 | $api = $this->getApiObject(); 417 | $api->sendInvitation(['polidogs@gmail.com']); 418 | $this->client->request('POST', 'teams/test/invitations', ['json' => [ 419 | 'members' => [ 420 | 'emails' => ['polidogs@gmail.com'], 421 | ], 422 | ]])->shouldHaveBeenCalled(); 423 | } 424 | 425 | public function testCancelInvitation(): void 426 | { 427 | $this->client->request('DELETE', 'teams/test/invitations/code')->willReturn([]); 428 | 429 | $api = $this->getApiObject(); 430 | $api->cancelInvitation('code'); 431 | $this->client->request('DELETE', 'teams/test/invitations/code')->shouldHaveBeenCalled(); 432 | } 433 | 434 | public function testEmojis(): void 435 | { 436 | $this->client->request('GET', 'teams/test/emojis', ['query' => []])->willReturn([]); 437 | 438 | $api = $this->getApiObject(); 439 | $api->emojis(); 440 | $this->client->request('GET', 'teams/test/emojis', ['query' => []])->shouldHaveBeenCalled(); 441 | } 442 | 443 | public function testCreateEmoji(): void 444 | { 445 | $this->client->request('POST', 'teams/test/emojis', [ 446 | 'json' => [ 447 | 'emoji' => [ 448 | 'code' => 'team_emoji', 449 | 'image' => 'base64...', 450 | ], 451 | ], 452 | ])->willReturn([]); 453 | 454 | $api = $this->getApiObject(); 455 | $api->createEmoji([ 456 | 'code' => 'team_emoji', 457 | 'image' => 'base64...', 458 | ]); 459 | 460 | $this->client->request('POST', 'teams/test/emojis', [ 461 | 'json' => [ 462 | 'emoji' => [ 463 | 'code' => 'team_emoji', 464 | 'image' => 'base64...', 465 | ], 466 | ], 467 | ])->shouldHaveBeenCalled(); 468 | } 469 | 470 | public function testDeleteEmoji(): void 471 | { 472 | $this->client->request('DELETE', 'teams/test/emojis/code')->willReturn([]); 473 | 474 | $api = $this->getApiObject(); 475 | $api->deleteEmoji('code'); 476 | $this->client->request('DELETE', 'teams/test/emojis/code')->shouldHaveBeenCalled(); 477 | } 478 | 479 | private function getApiObject(): Api 480 | { 481 | return new Api($this->client->reveal(), 'test'); 482 | } 483 | } 484 | --------------------------------------------------------------------------------