@@ -13,253 +13,253 @@ |
||
13 | 13 | class Gists extends AbstractReceiver |
14 | 14 | { |
15 | 15 | |
16 | - /** Available sub-Receiver */ |
|
17 | - const COMMENTS = 'Comments'; |
|
16 | + /** Available sub-Receiver */ |
|
17 | + const COMMENTS = 'Comments'; |
|
18 | 18 | |
19 | - /** |
|
20 | - * List a user's gists |
|
21 | - * |
|
22 | - * @link https://developer.github.com/v3/gists/#list-a-users-gists |
|
23 | - * |
|
24 | - * @param string $username |
|
25 | - * @param string $since |
|
26 | - * |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - public function listGists(string $username = null, string $since = '1970-01-01'): array |
|
30 | - { |
|
31 | - $url = '/gists'; |
|
32 | - if (null !== $username) { |
|
33 | - $url = '/users/:username/gists'; |
|
34 | - } |
|
19 | + /** |
|
20 | + * List a user's gists |
|
21 | + * |
|
22 | + * @link https://developer.github.com/v3/gists/#list-a-users-gists |
|
23 | + * |
|
24 | + * @param string $username |
|
25 | + * @param string $since |
|
26 | + * |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + public function listGists(string $username = null, string $since = '1970-01-01'): array |
|
30 | + { |
|
31 | + $url = '/gists'; |
|
32 | + if (null !== $username) { |
|
33 | + $url = '/users/:username/gists'; |
|
34 | + } |
|
35 | 35 | |
36 | - return $this->getApi()->request($this->getApi()->sprintf(':url?:arg', $url, $username, |
|
37 | - http_build_query(['since' => (new DateTime($since))->format(DateTime::ATOM)]))); |
|
38 | - } |
|
36 | + return $this->getApi()->request($this->getApi()->sprintf(':url?:arg', $url, $username, |
|
37 | + http_build_query(['since' => (new DateTime($since))->format(DateTime::ATOM)]))); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * List all public gists: |
|
42 | - * |
|
43 | - * @link https://developer.github.com/v3/gists/#list-all-public-gists |
|
44 | - * |
|
45 | - * @param string $since |
|
46 | - * |
|
47 | - * @return array |
|
48 | - */ |
|
49 | - public function listPublicGists(string $since = '1970-01-01'): array |
|
50 | - { |
|
51 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/public?:arg', |
|
52 | - http_build_query(['since' => (new DateTime($since))->format(DateTime::ATOM)]))); |
|
53 | - } |
|
40 | + /** |
|
41 | + * List all public gists: |
|
42 | + * |
|
43 | + * @link https://developer.github.com/v3/gists/#list-all-public-gists |
|
44 | + * |
|
45 | + * @param string $since |
|
46 | + * |
|
47 | + * @return array |
|
48 | + */ |
|
49 | + public function listPublicGists(string $since = '1970-01-01'): array |
|
50 | + { |
|
51 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/public?:arg', |
|
52 | + http_build_query(['since' => (new DateTime($since))->format(DateTime::ATOM)]))); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * List the authenticated user’s starred gists |
|
57 | - * |
|
58 | - * @link https://developer.github.com/v3/gists/#list-starred-gists |
|
59 | - * |
|
60 | - * @param string $since |
|
61 | - * |
|
62 | - * @return array |
|
63 | - */ |
|
64 | - public function listUsersStarredGists(string $since = '1970-01-01'): array |
|
65 | - { |
|
66 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/starred?:arg', |
|
67 | - http_build_query(['since' => (new DateTime($since))->format(DateTime::ATOM)]))); |
|
68 | - } |
|
55 | + /** |
|
56 | + * List the authenticated user’s starred gists |
|
57 | + * |
|
58 | + * @link https://developer.github.com/v3/gists/#list-starred-gists |
|
59 | + * |
|
60 | + * @param string $since |
|
61 | + * |
|
62 | + * @return array |
|
63 | + */ |
|
64 | + public function listUsersStarredGists(string $since = '1970-01-01'): array |
|
65 | + { |
|
66 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/starred?:arg', |
|
67 | + http_build_query(['since' => (new DateTime($since))->format(DateTime::ATOM)]))); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Get a single gist |
|
72 | - * |
|
73 | - * @link https://developer.github.com/v3/gists/#get-a-single-gist |
|
74 | - * |
|
75 | - * @param string $id |
|
76 | - * |
|
77 | - * @return array |
|
78 | - */ |
|
79 | - public function getGist(string $id): array |
|
80 | - { |
|
81 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:id', $id)); |
|
82 | - } |
|
70 | + /** |
|
71 | + * Get a single gist |
|
72 | + * |
|
73 | + * @link https://developer.github.com/v3/gists/#get-a-single-gist |
|
74 | + * |
|
75 | + * @param string $id |
|
76 | + * |
|
77 | + * @return array |
|
78 | + */ |
|
79 | + public function getGist(string $id): array |
|
80 | + { |
|
81 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:id', $id)); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Get a specific revision of a gist |
|
86 | - * |
|
87 | - * @link https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist |
|
88 | - * |
|
89 | - * @param string $id |
|
90 | - * @param string $sha |
|
91 | - * |
|
92 | - * @return array |
|
93 | - * @throws \Exception |
|
94 | - */ |
|
95 | - public function getGistRevision(string $id, string $sha): array |
|
96 | - { |
|
97 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/:sha', $id, $sha)); |
|
98 | - } |
|
84 | + /** |
|
85 | + * Get a specific revision of a gist |
|
86 | + * |
|
87 | + * @link https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist |
|
88 | + * |
|
89 | + * @param string $id |
|
90 | + * @param string $sha |
|
91 | + * |
|
92 | + * @return array |
|
93 | + * @throws \Exception |
|
94 | + */ |
|
95 | + public function getGistRevision(string $id, string $sha): array |
|
96 | + { |
|
97 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/:sha', $id, $sha)); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Create a gist |
|
102 | - * |
|
103 | - * @link https://developer.github.com/v3/gists/#create-a-gist |
|
104 | - * |
|
105 | - * @param array $files |
|
106 | - * @param string $description |
|
107 | - * @param bool $public |
|
108 | - * |
|
109 | - * @return array |
|
110 | - */ |
|
111 | - public function createGist(array $files, string $description = null, bool $public = false): array |
|
112 | - { |
|
113 | - return $this->getApi()->request($this->getApi()->sprintf('/gists'), Request::METHOD_POST, [ |
|
114 | - 'files' => $files, |
|
115 | - 'description' => $description, |
|
116 | - 'public' => $public |
|
117 | - ]); |
|
118 | - } |
|
100 | + /** |
|
101 | + * Create a gist |
|
102 | + * |
|
103 | + * @link https://developer.github.com/v3/gists/#create-a-gist |
|
104 | + * |
|
105 | + * @param array $files |
|
106 | + * @param string $description |
|
107 | + * @param bool $public |
|
108 | + * |
|
109 | + * @return array |
|
110 | + */ |
|
111 | + public function createGist(array $files, string $description = null, bool $public = false): array |
|
112 | + { |
|
113 | + return $this->getApi()->request($this->getApi()->sprintf('/gists'), Request::METHOD_POST, [ |
|
114 | + 'files' => $files, |
|
115 | + 'description' => $description, |
|
116 | + 'public' => $public |
|
117 | + ]); |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * Edit a gist |
|
122 | - * |
|
123 | - * @link https://developer.github.com/v3/gists/#edit-a-gist |
|
124 | - * |
|
125 | - * @param string $id |
|
126 | - * @param string $description |
|
127 | - * @param array $files |
|
128 | - * @param string $content |
|
129 | - * @param string $filename |
|
130 | - * |
|
131 | - * @return array |
|
132 | - */ |
|
133 | - public function editGist(string $id, string $description = '', array $files = [], string $content = '', |
|
134 | - string $filename = ''): array |
|
135 | - { |
|
136 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:id', $id), Request::METHOD_PATCH, [ |
|
137 | - 'description' => $description, |
|
138 | - 'files' => $files, |
|
139 | - 'content' => $content, |
|
140 | - 'filename' => $filename |
|
141 | - ]); |
|
142 | - } |
|
120 | + /** |
|
121 | + * Edit a gist |
|
122 | + * |
|
123 | + * @link https://developer.github.com/v3/gists/#edit-a-gist |
|
124 | + * |
|
125 | + * @param string $id |
|
126 | + * @param string $description |
|
127 | + * @param array $files |
|
128 | + * @param string $content |
|
129 | + * @param string $filename |
|
130 | + * |
|
131 | + * @return array |
|
132 | + */ |
|
133 | + public function editGist(string $id, string $description = '', array $files = [], string $content = '', |
|
134 | + string $filename = ''): array |
|
135 | + { |
|
136 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:id', $id), Request::METHOD_PATCH, [ |
|
137 | + 'description' => $description, |
|
138 | + 'files' => $files, |
|
139 | + 'content' => $content, |
|
140 | + 'filename' => $filename |
|
141 | + ]); |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * List gist commits |
|
146 | - * |
|
147 | - * @link https://developer.github.com/v3/gists/#list-gist-commits |
|
148 | - * |
|
149 | - * @param string $id |
|
150 | - * |
|
151 | - * @return array |
|
152 | - */ |
|
153 | - public function listGistsCommits(string $id): array |
|
154 | - { |
|
155 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/commits', $id)); |
|
156 | - } |
|
144 | + /** |
|
145 | + * List gist commits |
|
146 | + * |
|
147 | + * @link https://developer.github.com/v3/gists/#list-gist-commits |
|
148 | + * |
|
149 | + * @param string $id |
|
150 | + * |
|
151 | + * @return array |
|
152 | + */ |
|
153 | + public function listGistsCommits(string $id): array |
|
154 | + { |
|
155 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/commits', $id)); |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * Star a gist |
|
160 | - * |
|
161 | - * @link https://developer.github.com/v3/gists/#star-a-gist |
|
162 | - * |
|
163 | - * @param string $id |
|
164 | - * |
|
165 | - * @return bool |
|
166 | - */ |
|
167 | - public function starGist(string $id): bool |
|
168 | - { |
|
169 | - $this->getApi()->request($this->getApi()->sprintf('/gists/:id/star', $id), Request::METHOD_PUT); |
|
158 | + /** |
|
159 | + * Star a gist |
|
160 | + * |
|
161 | + * @link https://developer.github.com/v3/gists/#star-a-gist |
|
162 | + * |
|
163 | + * @param string $id |
|
164 | + * |
|
165 | + * @return bool |
|
166 | + */ |
|
167 | + public function starGist(string $id): bool |
|
168 | + { |
|
169 | + $this->getApi()->request($this->getApi()->sprintf('/gists/:id/star', $id), Request::METHOD_PUT); |
|
170 | 170 | |
171 | - if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
172 | - return true; |
|
173 | - } |
|
171 | + if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
172 | + return true; |
|
173 | + } |
|
174 | 174 | |
175 | - return false; |
|
176 | - } |
|
175 | + return false; |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * Unstar a gist |
|
180 | - * |
|
181 | - * @link https://developer.github.com/v3/gists/#unstar-a-gist |
|
182 | - * |
|
183 | - * @param string $id |
|
184 | - * |
|
185 | - * @return bool |
|
186 | - */ |
|
187 | - public function unStarGist(string $id): bool |
|
188 | - { |
|
189 | - $this->getApi()->request($this->getApi()->sprintf('/gists/:id/star', $id), Request::METHOD_DELETE); |
|
178 | + /** |
|
179 | + * Unstar a gist |
|
180 | + * |
|
181 | + * @link https://developer.github.com/v3/gists/#unstar-a-gist |
|
182 | + * |
|
183 | + * @param string $id |
|
184 | + * |
|
185 | + * @return bool |
|
186 | + */ |
|
187 | + public function unStarGist(string $id): bool |
|
188 | + { |
|
189 | + $this->getApi()->request($this->getApi()->sprintf('/gists/:id/star', $id), Request::METHOD_DELETE); |
|
190 | 190 | |
191 | - if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
192 | - return true; |
|
193 | - } |
|
191 | + if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
192 | + return true; |
|
193 | + } |
|
194 | 194 | |
195 | - return false; |
|
196 | - } |
|
195 | + return false; |
|
196 | + } |
|
197 | 197 | |
198 | - /** |
|
199 | - * Check if a gist is starred |
|
200 | - * |
|
201 | - * @link https://developer.github.com/v3/gists/#check-if-a-gist-is-starred |
|
202 | - * |
|
203 | - * @param string $id |
|
204 | - * |
|
205 | - * @return bool |
|
206 | - */ |
|
207 | - public function checkGistIsStarred(string $id): bool |
|
208 | - { |
|
209 | - $this->getApi()->request($this->getApi()->sprintf('/gists/:id/star', $id)); |
|
198 | + /** |
|
199 | + * Check if a gist is starred |
|
200 | + * |
|
201 | + * @link https://developer.github.com/v3/gists/#check-if-a-gist-is-starred |
|
202 | + * |
|
203 | + * @param string $id |
|
204 | + * |
|
205 | + * @return bool |
|
206 | + */ |
|
207 | + public function checkGistIsStarred(string $id): bool |
|
208 | + { |
|
209 | + $this->getApi()->request($this->getApi()->sprintf('/gists/:id/star', $id)); |
|
210 | 210 | |
211 | - if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
212 | - return true; |
|
213 | - } |
|
211 | + if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
212 | + return true; |
|
213 | + } |
|
214 | 214 | |
215 | - return false; |
|
216 | - } |
|
215 | + return false; |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * Fork a gist |
|
220 | - * |
|
221 | - * @link https://developer.github.com/v3/gists/#fork-a-gist |
|
222 | - * |
|
223 | - * @param string $id |
|
224 | - * |
|
225 | - * @return array |
|
226 | - */ |
|
227 | - public function forkGist(string $id): array |
|
228 | - { |
|
229 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/forks', $id), Request::METHOD_POST); |
|
230 | - } |
|
218 | + /** |
|
219 | + * Fork a gist |
|
220 | + * |
|
221 | + * @link https://developer.github.com/v3/gists/#fork-a-gist |
|
222 | + * |
|
223 | + * @param string $id |
|
224 | + * |
|
225 | + * @return array |
|
226 | + */ |
|
227 | + public function forkGist(string $id): array |
|
228 | + { |
|
229 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/forks', $id), Request::METHOD_POST); |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * List gist forks |
|
234 | - * |
|
235 | - * @link https://developer.github.com/v3/gists/#list-gist-forks |
|
236 | - * |
|
237 | - * @param string $id |
|
238 | - * |
|
239 | - * @return array |
|
240 | - */ |
|
241 | - public function listGistForks(string $id): array |
|
242 | - { |
|
243 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/forks', $id)); |
|
244 | - } |
|
232 | + /** |
|
233 | + * List gist forks |
|
234 | + * |
|
235 | + * @link https://developer.github.com/v3/gists/#list-gist-forks |
|
236 | + * |
|
237 | + * @param string $id |
|
238 | + * |
|
239 | + * @return array |
|
240 | + */ |
|
241 | + public function listGistForks(string $id): array |
|
242 | + { |
|
243 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:id/forks', $id)); |
|
244 | + } |
|
245 | 245 | |
246 | - /** |
|
247 | - * Delete a gist |
|
248 | - * |
|
249 | - * @link https://developer.github.com/v3/gists/#delete-a-gist |
|
250 | - * |
|
251 | - * @param string $id |
|
252 | - * |
|
253 | - * @return bool |
|
254 | - */ |
|
255 | - public function deleteGist(string $id): bool |
|
256 | - { |
|
257 | - $this->getApi()->request($this->getApi()->sprintf('/gists/:id', $id), Request::METHOD_DELETE); |
|
246 | + /** |
|
247 | + * Delete a gist |
|
248 | + * |
|
249 | + * @link https://developer.github.com/v3/gists/#delete-a-gist |
|
250 | + * |
|
251 | + * @param string $id |
|
252 | + * |
|
253 | + * @return bool |
|
254 | + */ |
|
255 | + public function deleteGist(string $id): bool |
|
256 | + { |
|
257 | + $this->getApi()->request($this->getApi()->sprintf('/gists/:id', $id), Request::METHOD_DELETE); |
|
258 | 258 | |
259 | - if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
260 | - return true; |
|
261 | - } |
|
259 | + if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
260 | + return true; |
|
261 | + } |
|
262 | 262 | |
263 | - return false; |
|
264 | - } |
|
263 | + return false; |
|
264 | + } |
|
265 | 265 | } |
266 | 266 | \ No newline at end of file |
@@ -13,572 +13,572 @@ |
||
13 | 13 | abstract class AbstractApi |
14 | 14 | { |
15 | 15 | |
16 | - /** API version */ |
|
17 | - const API_VERSION = 'v3'; |
|
18 | - |
|
19 | - /** API constants */ |
|
20 | - const API_URL = 'https://api.github.com'; |
|
21 | - const API_UPLOADS = 'https://uploads.github.com'; |
|
22 | - const API_RAW_URL = 'https://raw.github.com'; |
|
23 | - const CONTENT_TYPE = 'application/json'; |
|
24 | - const DEFAULT_ACCEPT = 'application/vnd.github.' . self::API_VERSION . '+json'; |
|
25 | - const USER_AGENT = 'scion.github-api'; |
|
26 | - |
|
27 | - /** Archive constants */ |
|
28 | - const ARCHIVE_TARBALL = 'tarball'; |
|
29 | - const ARCHIVE_ZIPBALL = 'zipball'; |
|
30 | - |
|
31 | - /** Authentication constants */ |
|
32 | - const OAUTH_AUTH = 0; |
|
33 | - const OAUTH2_HEADER_AUTH = 1; |
|
34 | - const OAUTH2_PARAMETERS_AUTH = 2; |
|
35 | - |
|
36 | - /** Branch constants */ |
|
37 | - const BRANCH_MASTER = 'master'; |
|
38 | - const BRANCH_DEVELOP = 'develop'; |
|
39 | - |
|
40 | - /** Direction constants */ |
|
41 | - const DIRECTION_ASC = 'asc'; |
|
42 | - const DIRECTION_DESC = 'desc'; |
|
43 | - |
|
44 | - /** Environment constants */ |
|
45 | - const ENVIRONMENT_PRODUCTION = 'production'; |
|
46 | - const ENVIRONMENT_STAGING = 'staging'; |
|
47 | - const ENVIRONMENT_QA = 'qa'; |
|
48 | - |
|
49 | - /** Events constants */ |
|
50 | - const EVENTS_PULL = 'pull'; |
|
51 | - const EVENTS_PULL_REQUEST = 'pull_request'; |
|
52 | - const EVENTS_PUSH = 'push'; |
|
53 | - |
|
54 | - /** Filter constants */ |
|
55 | - const FILTER_ALL = 'all'; |
|
56 | - const FILTER_ASSIGNED = 'assigned'; |
|
57 | - const FILTER_CREATED = 'created'; |
|
58 | - const FILTER_MENTIONED = 'mentioned'; |
|
59 | - const FILTER_SUBSCRIBED = 'subscribed'; |
|
60 | - |
|
61 | - /** Media types constants */ |
|
62 | - const MEDIA_TYPE_JSON = 'json'; |
|
63 | - const MEDIA_TYPE_RAW = 'raw'; |
|
64 | - const MEDIA_TYPE_FULL = 'full'; |
|
65 | - const MEDIA_TYPE_TEXT = 'text'; |
|
66 | - |
|
67 | - /** Modes constants */ |
|
68 | - const MODE_MARKDOWN = 'markdown'; |
|
69 | - const MODE_GFM = 'gfm'; |
|
70 | - |
|
71 | - /** Permissions constants */ |
|
72 | - const PERMISSION_ADMIN = 'admin'; |
|
73 | - const PERMISSION_PULL = 'pull'; |
|
74 | - const PERMISSION_PUSH = 'push'; |
|
75 | - |
|
76 | - /** Sort constants */ |
|
77 | - const SORT_COMPLETENESS = 'completeness'; |
|
78 | - const SORT_CREATED = 'created'; |
|
79 | - const SORT_DUE_DATE = 'due_date'; |
|
80 | - const SORT_FULL_NAME = 'full_name'; |
|
81 | - const SORT_NEWEST = 'newest'; |
|
82 | - const SORT_OLDEST = 'oldest'; |
|
83 | - const SORT_PUSHED = 'pushed'; |
|
84 | - const SORT_STARGAZERS = 'stargazers'; |
|
85 | - const SORT_UPDATED = 'updated'; |
|
86 | - |
|
87 | - /** State constants */ |
|
88 | - const STATE_ACTIVE = 'active'; |
|
89 | - const STATE_ALL = 'all'; |
|
90 | - const STATE_CLOSED = 'closed'; |
|
91 | - const STATE_ERROR = 'error'; |
|
92 | - const STATE_FAILURE = 'failure'; |
|
93 | - const STATE_OPEN = 'open'; |
|
94 | - const STATE_PENDING = 'pending'; |
|
95 | - const STATE_SUCCESS = 'success'; |
|
96 | - |
|
97 | - /** Task constants */ |
|
98 | - const TASK_DEPLOY = 'deploy'; |
|
99 | - const TASK_DEPLOY_MIGRATIONS = 'deploy:migrations'; |
|
100 | - |
|
101 | - /** Type constants */ |
|
102 | - const TYPE_ALL = 'all'; |
|
103 | - const TYPE_COMMENTS = 'comments'; |
|
104 | - const TYPE_GISTS = 'gists'; |
|
105 | - const TYPE_HOOKS = 'hooks'; |
|
106 | - const TYPE_ISSUES = 'issues'; |
|
107 | - const TYPE_MEMBER = 'member'; |
|
108 | - const TYPE_MILESTONES = 'milestones'; |
|
109 | - const TYPE_ORGS = 'orgs'; |
|
110 | - const TYPE_OWNER = 'owner'; |
|
111 | - const TYPE_PAGES = 'pages'; |
|
112 | - const TYPE_PUBLIC = 'public'; |
|
113 | - const TYPE_PULLS = 'pulls'; |
|
114 | - const TYPE_PRIVATE = 'private'; |
|
115 | - const TYPE_REPOS = 'repos'; |
|
116 | - const TYPE_USERS = 'users'; |
|
117 | - |
|
118 | - /** Properties */ |
|
119 | - protected $accept = self::DEFAULT_ACCEPT; |
|
120 | - protected $apiUrl = self::API_URL; |
|
121 | - protected $authentication = self::OAUTH_AUTH; |
|
122 | - protected $clientId; |
|
123 | - protected $clientSecret; |
|
124 | - protected $contentType = self::CONTENT_TYPE; |
|
125 | - protected $failure; |
|
126 | - protected $headers = []; |
|
127 | - protected $httpAuth = ['username' => '', 'password' => '']; |
|
128 | - protected $success; |
|
129 | - protected $timeout = 240; |
|
130 | - protected $token = ''; |
|
131 | - protected $request; |
|
132 | - |
|
133 | - /** |
|
134 | - * Constructor |
|
135 | - */ |
|
136 | - public function __construct() |
|
137 | - { |
|
138 | - $this->request = Request::createFromGlobals(); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Get request |
|
143 | - * |
|
144 | - * @return Request |
|
145 | - */ |
|
146 | - public function getRequest(): Request |
|
147 | - { |
|
148 | - return $this->request; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Get accept |
|
153 | - * |
|
154 | - * @return mixed |
|
155 | - */ |
|
156 | - public function getAccept() |
|
157 | - { |
|
158 | - return $this->accept; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Set accept |
|
163 | - * |
|
164 | - * @param array|string $accept |
|
165 | - * |
|
166 | - * @return AbstractApi |
|
167 | - */ |
|
168 | - public function setAccept($accept): AbstractApi |
|
169 | - { |
|
170 | - $this->accept = $accept; |
|
171 | - |
|
172 | - return $this; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Get authentication |
|
177 | - * |
|
178 | - * @return int |
|
179 | - */ |
|
180 | - public function getAuthentication(): int |
|
181 | - { |
|
182 | - return $this->authentication; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Set authentication |
|
187 | - * |
|
188 | - * @param int $authentication |
|
189 | - * |
|
190 | - * @return AbstractApi |
|
191 | - */ |
|
192 | - public function setAuthentication(int $authentication): AbstractApi |
|
193 | - { |
|
194 | - $this->authentication = $authentication; |
|
195 | - |
|
196 | - return $this; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Get apiUrl |
|
201 | - * |
|
202 | - * @return string |
|
203 | - */ |
|
204 | - public function getApiUrl(): string |
|
205 | - { |
|
206 | - return $this->apiUrl; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Set apiUrl |
|
211 | - * |
|
212 | - * @param mixed $apiUrl |
|
213 | - * |
|
214 | - * @return AbstractApi |
|
215 | - */ |
|
216 | - public function setApiUrl($apiUrl): AbstractApi |
|
217 | - { |
|
218 | - $this->apiUrl = $apiUrl; |
|
219 | - |
|
220 | - return $this; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Get clientId |
|
225 | - * |
|
226 | - * @return null|int |
|
227 | - */ |
|
228 | - public function getClientId() |
|
229 | - { |
|
230 | - return $this->clientId; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Set clientId |
|
235 | - * |
|
236 | - * @param mixed $clientId |
|
237 | - * |
|
238 | - * @return AbstractApi |
|
239 | - */ |
|
240 | - public function setClientId($clientId): AbstractApi |
|
241 | - { |
|
242 | - $this->clientId = $clientId; |
|
243 | - |
|
244 | - return $this; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Get clientSecret |
|
249 | - * |
|
250 | - * @return null|string |
|
251 | - */ |
|
252 | - public function getClientSecret() |
|
253 | - { |
|
254 | - return $this->clientSecret; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Set clientSecret |
|
259 | - * |
|
260 | - * @param mixed $clientSecret |
|
261 | - * |
|
262 | - * @return AbstractApi |
|
263 | - */ |
|
264 | - public function setClientSecret($clientSecret): AbstractApi |
|
265 | - { |
|
266 | - $this->clientSecret = $clientSecret; |
|
267 | - |
|
268 | - return $this; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Get httpAuth |
|
273 | - * |
|
274 | - * @return array |
|
275 | - */ |
|
276 | - public function getHttpAuth(): array |
|
277 | - { |
|
278 | - return $this->httpAuth; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * Set httpAuth |
|
283 | - * |
|
284 | - * @param string $username |
|
285 | - * @param string $password |
|
286 | - * |
|
287 | - * @return AbstractApi |
|
288 | - */ |
|
289 | - public function setHttpAuth(string $username, string $password = ''): AbstractApi |
|
290 | - { |
|
291 | - $this->httpAuth['username'] = $username; |
|
292 | - $this->httpAuth['password'] = $password; |
|
293 | - |
|
294 | - return $this; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Get token |
|
299 | - * |
|
300 | - * @return string |
|
301 | - */ |
|
302 | - public function getToken(): string |
|
303 | - { |
|
304 | - return $this->token; |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Set token |
|
309 | - * |
|
310 | - * @param string $token |
|
311 | - * @param int $authentication |
|
312 | - * |
|
313 | - * @return AbstractApi |
|
314 | - */ |
|
315 | - public function setToken(string $token, int $authentication = self::OAUTH_AUTH): AbstractApi |
|
316 | - { |
|
317 | - $this->token = $token; |
|
318 | - $this->setAuthentication($authentication); |
|
319 | - |
|
320 | - return $this; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Get timeout |
|
325 | - * |
|
326 | - * @return int |
|
327 | - */ |
|
328 | - public function getTimeout(): int |
|
329 | - { |
|
330 | - return $this->timeout; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Set timeout |
|
335 | - * |
|
336 | - * @param int $timeout |
|
337 | - * |
|
338 | - * @return AbstractApi |
|
339 | - */ |
|
340 | - public function setTimeout(int $timeout): AbstractApi |
|
341 | - { |
|
342 | - $this->timeout = $timeout; |
|
343 | - |
|
344 | - return $this; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Get contentType |
|
349 | - * |
|
350 | - * @return string |
|
351 | - */ |
|
352 | - public function getContentType(): string |
|
353 | - { |
|
354 | - return $this->contentType; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Set contentType |
|
359 | - * |
|
360 | - * @param string $contentType |
|
361 | - * |
|
362 | - * @return AbstractApi |
|
363 | - */ |
|
364 | - public function setContentType(string $contentType): AbstractApi |
|
365 | - { |
|
366 | - $this->contentType = $contentType; |
|
367 | - |
|
368 | - return $this; |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Get headers |
|
373 | - * |
|
374 | - * @return array |
|
375 | - */ |
|
376 | - public function getHeaders(): array |
|
377 | - { |
|
378 | - return $this->headers; |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * Curl request |
|
383 | - * |
|
384 | - * @param string $url |
|
385 | - * @param string $method |
|
386 | - * @param array $postFields |
|
387 | - * @param null|string $apiUrl |
|
388 | - * |
|
389 | - * @return array |
|
390 | - */ |
|
391 | - public function request(string $url, string $method = Request::METHOD_GET, array $postFields = [], |
|
392 | - string $apiUrl = null): array |
|
393 | - { |
|
394 | - /** Building url */ |
|
395 | - if (null === $apiUrl) { |
|
396 | - $apiUrl = $this->getApiUrl(); |
|
397 | - } |
|
398 | - $url = $apiUrl . $url; |
|
399 | - |
|
400 | - /** |
|
401 | - * OAuth2 Key/Secret authentication |
|
402 | - * |
|
403 | - * @see https://developer.github.com/v3/#oauth2-keysecret |
|
404 | - */ |
|
405 | - if (null !== $this->getClientId() && null !== $this->getClientSecret()) { |
|
406 | - $url .= (strstr($url, '?') !== false ? '&' : '?'); |
|
407 | - $url .= http_build_query(['client_id' => $this->getClientId(), |
|
408 | - 'client_secret' => $this->getClientSecret() |
|
409 | - ]); |
|
410 | - } /** |
|
411 | - * Basic authentication via OAuth2 Token (sent as a parameter) |
|
412 | - * |
|
413 | - * @see https://developer.github.com/v3/#oauth2-token-sent-as-a-parameter |
|
414 | - */ else if ($this->getAuthentication() === self::OAUTH2_PARAMETERS_AUTH) { |
|
415 | - $url .= http_build_query(['access_token' => $this->getToken()]); |
|
416 | - } |
|
417 | - |
|
418 | - /** Call curl */ |
|
419 | - $curl = new CurlClient(); |
|
420 | - $curl->setOption([ |
|
421 | - CURLOPT_USERAGENT => self::USER_AGENT, |
|
422 | - CURLOPT_TIMEOUT => $this->getTimeout(), |
|
423 | - CURLOPT_HEADER => false, // Use $client->getHeaders() to get full header |
|
424 | - CURLOPT_FOLLOWLOCATION => true, |
|
425 | - CURLOPT_HTTPHEADER => [ |
|
426 | - 'Accept: ' . $this->getAccept(), |
|
427 | - 'Content-Type: ' . $this->getContentType() |
|
428 | - ], |
|
429 | - CURLOPT_URL => $url |
|
430 | - ]); |
|
431 | - |
|
432 | - /** |
|
433 | - * Basic authentication via username and Password |
|
434 | - * |
|
435 | - * @see https://developer.github.com/v3/auth/#via-username-and-password |
|
436 | - */ |
|
437 | - if (!empty($this->getHttpAuth())) { |
|
438 | - if (!isset($this->getHttpAuth()['password']) || empty($this->getHttpAuth()['password'])) { |
|
439 | - $curl->setOption([ |
|
440 | - CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
441 | - CURLOPT_USERPWD => $this->getHttpAuth()['username'] |
|
442 | - ]); |
|
443 | - } else { |
|
444 | - $curl->setOption([ |
|
445 | - CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
446 | - CURLOPT_USERPWD => sprintf('%s:%s', $this->getHttpAuth()['username'], |
|
447 | - $this->getHttpAuth()['password']) |
|
448 | - ]); |
|
449 | - } |
|
450 | - } |
|
451 | - |
|
452 | - if (!empty($this->getToken()) && $this->getAuthentication() !== self::OAUTH2_PARAMETERS_AUTH) { |
|
453 | - /** |
|
454 | - * Basic authentication via OAuth token |
|
455 | - * |
|
456 | - * @see https://developer.github.com/v3/auth/#via-oauth-tokens |
|
457 | - **/ |
|
458 | - if ($this->getAuthentication() === self::OAUTH_AUTH) { |
|
459 | - $curl->setOption([ |
|
460 | - CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
461 | - CURLOPT_USERPWD => sprintf('%s:x-oauth-basic', $this->getToken()) |
|
462 | - ]); |
|
463 | - } /** |
|
464 | - * Basic authentication via OAuth2 Token (sent in a header) |
|
465 | - * |
|
466 | - * @see https://developer.github.com/v3/#oauth2-token-sent-in-a-header |
|
467 | - */ else if ($this->getAuthentication() === self::OAUTH2_HEADER_AUTH) { |
|
468 | - $curl->setOption([ |
|
469 | - CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
470 | - CURLOPT_HTTPHEADER => [sprintf('Authorization: token %s', $this->getToken())] |
|
471 | - ]); |
|
472 | - } |
|
473 | - } |
|
474 | - |
|
475 | - /** Methods */ |
|
476 | - switch ($method) { |
|
477 | - /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7 */ |
|
478 | - case Request::METHOD_DELETE: |
|
479 | - /** @see http://tools.ietf.org/html/rfc5789 */ |
|
480 | - case Request::METHOD_PATCH: |
|
481 | - $curl->setOption([ |
|
482 | - CURLOPT_CUSTOMREQUEST => $method, |
|
483 | - CURLOPT_POST => true, |
|
484 | - CURLOPT_POSTFIELDS => json_encode(array_filter($postFields)) |
|
485 | - ]); |
|
486 | - break; |
|
487 | - |
|
488 | - /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3 */ |
|
489 | - case Request::METHOD_GET: |
|
490 | - $curl->setOption(CURLOPT_HTTPGET, true); |
|
491 | - break; |
|
492 | - |
|
493 | - /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 */ |
|
494 | - case Request::METHOD_HEAD: |
|
495 | - $curl->setOption([ |
|
496 | - CURLOPT_CUSTOMREQUEST => $method, |
|
497 | - CURLOPT_NOBODY => true |
|
498 | - ]); |
|
499 | - break; |
|
500 | - |
|
501 | - /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5 */ |
|
502 | - case Request::METHOD_POST: |
|
503 | - $curl->setOption([ |
|
504 | - CURLOPT_POST => true, |
|
505 | - CURLOPT_POSTFIELDS => json_encode(array_filter($postFields)) |
|
506 | - ]); |
|
507 | - break; |
|
508 | - |
|
509 | - /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 */ |
|
510 | - case Request::METHOD_PUT: |
|
511 | - $curl->setOption([ |
|
512 | - CURLOPT_CUSTOMREQUEST => $method, |
|
513 | - CURLOPT_PUT => true, |
|
514 | - CURLOPT_HTTPHEADER => [ |
|
515 | - 'X-HTTP-Method-Override: PUT', |
|
516 | - 'Content-type: application/x-www-form-urlencoded' |
|
517 | - ] |
|
518 | - ]); |
|
519 | - break; |
|
520 | - |
|
521 | - default: |
|
522 | - break; |
|
523 | - } |
|
524 | - |
|
525 | - $curl->success(function (CurlClient $instance) { |
|
526 | - $this->headers = $instance->getHeaders(); |
|
527 | - $this->success = $instance->getResponse(); |
|
528 | - $data = json_decode($this->success, true); |
|
529 | - if (JSON_ERROR_NONE === json_last_error()) { |
|
530 | - $this->success = $data; |
|
531 | - } |
|
532 | - }); |
|
533 | - $curl->error(function (CurlClient $instance) { |
|
534 | - $this->headers = $instance->getHeaders(); |
|
535 | - $this->failure = $instance->getResponse(); |
|
536 | - $data = json_decode($this->failure, true); |
|
537 | - if (JSON_ERROR_NONE === json_last_error()) { |
|
538 | - $this->failure = $data; |
|
539 | - } |
|
540 | - }); |
|
541 | - $curl->perform(); |
|
542 | - |
|
543 | - return (array)($this->success ?? $this->failure); |
|
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * Return a formatted string. Modified version of sprintf using colon(:) |
|
548 | - * |
|
549 | - * @param string $string |
|
550 | - * @param array $params |
|
551 | - * |
|
552 | - * @return String |
|
553 | - * @throws Exception |
|
554 | - */ |
|
555 | - public function sprintf(string $string, ...$params): string |
|
556 | - { |
|
557 | - preg_match_all('/\:([A-Za-z0-9_]+)/', $string, $matches); |
|
558 | - $matches = $matches[1]; |
|
559 | - |
|
560 | - if (count($matches)) { |
|
561 | - $tokens = []; |
|
562 | - $replaces = []; |
|
563 | - |
|
564 | - foreach ($matches as $key => $value) { |
|
565 | - if (count($params) > 1 || !is_array($params[0])) { |
|
566 | - if (!array_key_exists($key, $params)) { |
|
567 | - throw new Exception('Too few arguments, missing argument: ' . $key); |
|
568 | - } |
|
569 | - $replaces[] = $params[$key]; |
|
570 | - } else { |
|
571 | - if (!array_key_exists($value, $params[0])) { |
|
572 | - throw new Exception('Missing array argument: ' . $key); |
|
573 | - } |
|
574 | - $replaces[] = $params[0][$value]; |
|
575 | - } |
|
576 | - $tokens[] = ':' . $value; |
|
577 | - } |
|
578 | - |
|
579 | - $string = str_replace($tokens, $replaces, $string); |
|
580 | - } |
|
581 | - |
|
582 | - return $string; |
|
583 | - } |
|
16 | + /** API version */ |
|
17 | + const API_VERSION = 'v3'; |
|
18 | + |
|
19 | + /** API constants */ |
|
20 | + const API_URL = 'https://api.github.com'; |
|
21 | + const API_UPLOADS = 'https://uploads.github.com'; |
|
22 | + const API_RAW_URL = 'https://raw.github.com'; |
|
23 | + const CONTENT_TYPE = 'application/json'; |
|
24 | + const DEFAULT_ACCEPT = 'application/vnd.github.' . self::API_VERSION . '+json'; |
|
25 | + const USER_AGENT = 'scion.github-api'; |
|
26 | + |
|
27 | + /** Archive constants */ |
|
28 | + const ARCHIVE_TARBALL = 'tarball'; |
|
29 | + const ARCHIVE_ZIPBALL = 'zipball'; |
|
30 | + |
|
31 | + /** Authentication constants */ |
|
32 | + const OAUTH_AUTH = 0; |
|
33 | + const OAUTH2_HEADER_AUTH = 1; |
|
34 | + const OAUTH2_PARAMETERS_AUTH = 2; |
|
35 | + |
|
36 | + /** Branch constants */ |
|
37 | + const BRANCH_MASTER = 'master'; |
|
38 | + const BRANCH_DEVELOP = 'develop'; |
|
39 | + |
|
40 | + /** Direction constants */ |
|
41 | + const DIRECTION_ASC = 'asc'; |
|
42 | + const DIRECTION_DESC = 'desc'; |
|
43 | + |
|
44 | + /** Environment constants */ |
|
45 | + const ENVIRONMENT_PRODUCTION = 'production'; |
|
46 | + const ENVIRONMENT_STAGING = 'staging'; |
|
47 | + const ENVIRONMENT_QA = 'qa'; |
|
48 | + |
|
49 | + /** Events constants */ |
|
50 | + const EVENTS_PULL = 'pull'; |
|
51 | + const EVENTS_PULL_REQUEST = 'pull_request'; |
|
52 | + const EVENTS_PUSH = 'push'; |
|
53 | + |
|
54 | + /** Filter constants */ |
|
55 | + const FILTER_ALL = 'all'; |
|
56 | + const FILTER_ASSIGNED = 'assigned'; |
|
57 | + const FILTER_CREATED = 'created'; |
|
58 | + const FILTER_MENTIONED = 'mentioned'; |
|
59 | + const FILTER_SUBSCRIBED = 'subscribed'; |
|
60 | + |
|
61 | + /** Media types constants */ |
|
62 | + const MEDIA_TYPE_JSON = 'json'; |
|
63 | + const MEDIA_TYPE_RAW = 'raw'; |
|
64 | + const MEDIA_TYPE_FULL = 'full'; |
|
65 | + const MEDIA_TYPE_TEXT = 'text'; |
|
66 | + |
|
67 | + /** Modes constants */ |
|
68 | + const MODE_MARKDOWN = 'markdown'; |
|
69 | + const MODE_GFM = 'gfm'; |
|
70 | + |
|
71 | + /** Permissions constants */ |
|
72 | + const PERMISSION_ADMIN = 'admin'; |
|
73 | + const PERMISSION_PULL = 'pull'; |
|
74 | + const PERMISSION_PUSH = 'push'; |
|
75 | + |
|
76 | + /** Sort constants */ |
|
77 | + const SORT_COMPLETENESS = 'completeness'; |
|
78 | + const SORT_CREATED = 'created'; |
|
79 | + const SORT_DUE_DATE = 'due_date'; |
|
80 | + const SORT_FULL_NAME = 'full_name'; |
|
81 | + const SORT_NEWEST = 'newest'; |
|
82 | + const SORT_OLDEST = 'oldest'; |
|
83 | + const SORT_PUSHED = 'pushed'; |
|
84 | + const SORT_STARGAZERS = 'stargazers'; |
|
85 | + const SORT_UPDATED = 'updated'; |
|
86 | + |
|
87 | + /** State constants */ |
|
88 | + const STATE_ACTIVE = 'active'; |
|
89 | + const STATE_ALL = 'all'; |
|
90 | + const STATE_CLOSED = 'closed'; |
|
91 | + const STATE_ERROR = 'error'; |
|
92 | + const STATE_FAILURE = 'failure'; |
|
93 | + const STATE_OPEN = 'open'; |
|
94 | + const STATE_PENDING = 'pending'; |
|
95 | + const STATE_SUCCESS = 'success'; |
|
96 | + |
|
97 | + /** Task constants */ |
|
98 | + const TASK_DEPLOY = 'deploy'; |
|
99 | + const TASK_DEPLOY_MIGRATIONS = 'deploy:migrations'; |
|
100 | + |
|
101 | + /** Type constants */ |
|
102 | + const TYPE_ALL = 'all'; |
|
103 | + const TYPE_COMMENTS = 'comments'; |
|
104 | + const TYPE_GISTS = 'gists'; |
|
105 | + const TYPE_HOOKS = 'hooks'; |
|
106 | + const TYPE_ISSUES = 'issues'; |
|
107 | + const TYPE_MEMBER = 'member'; |
|
108 | + const TYPE_MILESTONES = 'milestones'; |
|
109 | + const TYPE_ORGS = 'orgs'; |
|
110 | + const TYPE_OWNER = 'owner'; |
|
111 | + const TYPE_PAGES = 'pages'; |
|
112 | + const TYPE_PUBLIC = 'public'; |
|
113 | + const TYPE_PULLS = 'pulls'; |
|
114 | + const TYPE_PRIVATE = 'private'; |
|
115 | + const TYPE_REPOS = 'repos'; |
|
116 | + const TYPE_USERS = 'users'; |
|
117 | + |
|
118 | + /** Properties */ |
|
119 | + protected $accept = self::DEFAULT_ACCEPT; |
|
120 | + protected $apiUrl = self::API_URL; |
|
121 | + protected $authentication = self::OAUTH_AUTH; |
|
122 | + protected $clientId; |
|
123 | + protected $clientSecret; |
|
124 | + protected $contentType = self::CONTENT_TYPE; |
|
125 | + protected $failure; |
|
126 | + protected $headers = []; |
|
127 | + protected $httpAuth = ['username' => '', 'password' => '']; |
|
128 | + protected $success; |
|
129 | + protected $timeout = 240; |
|
130 | + protected $token = ''; |
|
131 | + protected $request; |
|
132 | + |
|
133 | + /** |
|
134 | + * Constructor |
|
135 | + */ |
|
136 | + public function __construct() |
|
137 | + { |
|
138 | + $this->request = Request::createFromGlobals(); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Get request |
|
143 | + * |
|
144 | + * @return Request |
|
145 | + */ |
|
146 | + public function getRequest(): Request |
|
147 | + { |
|
148 | + return $this->request; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Get accept |
|
153 | + * |
|
154 | + * @return mixed |
|
155 | + */ |
|
156 | + public function getAccept() |
|
157 | + { |
|
158 | + return $this->accept; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Set accept |
|
163 | + * |
|
164 | + * @param array|string $accept |
|
165 | + * |
|
166 | + * @return AbstractApi |
|
167 | + */ |
|
168 | + public function setAccept($accept): AbstractApi |
|
169 | + { |
|
170 | + $this->accept = $accept; |
|
171 | + |
|
172 | + return $this; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Get authentication |
|
177 | + * |
|
178 | + * @return int |
|
179 | + */ |
|
180 | + public function getAuthentication(): int |
|
181 | + { |
|
182 | + return $this->authentication; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Set authentication |
|
187 | + * |
|
188 | + * @param int $authentication |
|
189 | + * |
|
190 | + * @return AbstractApi |
|
191 | + */ |
|
192 | + public function setAuthentication(int $authentication): AbstractApi |
|
193 | + { |
|
194 | + $this->authentication = $authentication; |
|
195 | + |
|
196 | + return $this; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Get apiUrl |
|
201 | + * |
|
202 | + * @return string |
|
203 | + */ |
|
204 | + public function getApiUrl(): string |
|
205 | + { |
|
206 | + return $this->apiUrl; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Set apiUrl |
|
211 | + * |
|
212 | + * @param mixed $apiUrl |
|
213 | + * |
|
214 | + * @return AbstractApi |
|
215 | + */ |
|
216 | + public function setApiUrl($apiUrl): AbstractApi |
|
217 | + { |
|
218 | + $this->apiUrl = $apiUrl; |
|
219 | + |
|
220 | + return $this; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Get clientId |
|
225 | + * |
|
226 | + * @return null|int |
|
227 | + */ |
|
228 | + public function getClientId() |
|
229 | + { |
|
230 | + return $this->clientId; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Set clientId |
|
235 | + * |
|
236 | + * @param mixed $clientId |
|
237 | + * |
|
238 | + * @return AbstractApi |
|
239 | + */ |
|
240 | + public function setClientId($clientId): AbstractApi |
|
241 | + { |
|
242 | + $this->clientId = $clientId; |
|
243 | + |
|
244 | + return $this; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Get clientSecret |
|
249 | + * |
|
250 | + * @return null|string |
|
251 | + */ |
|
252 | + public function getClientSecret() |
|
253 | + { |
|
254 | + return $this->clientSecret; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Set clientSecret |
|
259 | + * |
|
260 | + * @param mixed $clientSecret |
|
261 | + * |
|
262 | + * @return AbstractApi |
|
263 | + */ |
|
264 | + public function setClientSecret($clientSecret): AbstractApi |
|
265 | + { |
|
266 | + $this->clientSecret = $clientSecret; |
|
267 | + |
|
268 | + return $this; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Get httpAuth |
|
273 | + * |
|
274 | + * @return array |
|
275 | + */ |
|
276 | + public function getHttpAuth(): array |
|
277 | + { |
|
278 | + return $this->httpAuth; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * Set httpAuth |
|
283 | + * |
|
284 | + * @param string $username |
|
285 | + * @param string $password |
|
286 | + * |
|
287 | + * @return AbstractApi |
|
288 | + */ |
|
289 | + public function setHttpAuth(string $username, string $password = ''): AbstractApi |
|
290 | + { |
|
291 | + $this->httpAuth['username'] = $username; |
|
292 | + $this->httpAuth['password'] = $password; |
|
293 | + |
|
294 | + return $this; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Get token |
|
299 | + * |
|
300 | + * @return string |
|
301 | + */ |
|
302 | + public function getToken(): string |
|
303 | + { |
|
304 | + return $this->token; |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Set token |
|
309 | + * |
|
310 | + * @param string $token |
|
311 | + * @param int $authentication |
|
312 | + * |
|
313 | + * @return AbstractApi |
|
314 | + */ |
|
315 | + public function setToken(string $token, int $authentication = self::OAUTH_AUTH): AbstractApi |
|
316 | + { |
|
317 | + $this->token = $token; |
|
318 | + $this->setAuthentication($authentication); |
|
319 | + |
|
320 | + return $this; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Get timeout |
|
325 | + * |
|
326 | + * @return int |
|
327 | + */ |
|
328 | + public function getTimeout(): int |
|
329 | + { |
|
330 | + return $this->timeout; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Set timeout |
|
335 | + * |
|
336 | + * @param int $timeout |
|
337 | + * |
|
338 | + * @return AbstractApi |
|
339 | + */ |
|
340 | + public function setTimeout(int $timeout): AbstractApi |
|
341 | + { |
|
342 | + $this->timeout = $timeout; |
|
343 | + |
|
344 | + return $this; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Get contentType |
|
349 | + * |
|
350 | + * @return string |
|
351 | + */ |
|
352 | + public function getContentType(): string |
|
353 | + { |
|
354 | + return $this->contentType; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Set contentType |
|
359 | + * |
|
360 | + * @param string $contentType |
|
361 | + * |
|
362 | + * @return AbstractApi |
|
363 | + */ |
|
364 | + public function setContentType(string $contentType): AbstractApi |
|
365 | + { |
|
366 | + $this->contentType = $contentType; |
|
367 | + |
|
368 | + return $this; |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Get headers |
|
373 | + * |
|
374 | + * @return array |
|
375 | + */ |
|
376 | + public function getHeaders(): array |
|
377 | + { |
|
378 | + return $this->headers; |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * Curl request |
|
383 | + * |
|
384 | + * @param string $url |
|
385 | + * @param string $method |
|
386 | + * @param array $postFields |
|
387 | + * @param null|string $apiUrl |
|
388 | + * |
|
389 | + * @return array |
|
390 | + */ |
|
391 | + public function request(string $url, string $method = Request::METHOD_GET, array $postFields = [], |
|
392 | + string $apiUrl = null): array |
|
393 | + { |
|
394 | + /** Building url */ |
|
395 | + if (null === $apiUrl) { |
|
396 | + $apiUrl = $this->getApiUrl(); |
|
397 | + } |
|
398 | + $url = $apiUrl . $url; |
|
399 | + |
|
400 | + /** |
|
401 | + * OAuth2 Key/Secret authentication |
|
402 | + * |
|
403 | + * @see https://developer.github.com/v3/#oauth2-keysecret |
|
404 | + */ |
|
405 | + if (null !== $this->getClientId() && null !== $this->getClientSecret()) { |
|
406 | + $url .= (strstr($url, '?') !== false ? '&' : '?'); |
|
407 | + $url .= http_build_query(['client_id' => $this->getClientId(), |
|
408 | + 'client_secret' => $this->getClientSecret() |
|
409 | + ]); |
|
410 | + } /** |
|
411 | + * Basic authentication via OAuth2 Token (sent as a parameter) |
|
412 | + * |
|
413 | + * @see https://developer.github.com/v3/#oauth2-token-sent-as-a-parameter |
|
414 | + */ else if ($this->getAuthentication() === self::OAUTH2_PARAMETERS_AUTH) { |
|
415 | + $url .= http_build_query(['access_token' => $this->getToken()]); |
|
416 | + } |
|
417 | + |
|
418 | + /** Call curl */ |
|
419 | + $curl = new CurlClient(); |
|
420 | + $curl->setOption([ |
|
421 | + CURLOPT_USERAGENT => self::USER_AGENT, |
|
422 | + CURLOPT_TIMEOUT => $this->getTimeout(), |
|
423 | + CURLOPT_HEADER => false, // Use $client->getHeaders() to get full header |
|
424 | + CURLOPT_FOLLOWLOCATION => true, |
|
425 | + CURLOPT_HTTPHEADER => [ |
|
426 | + 'Accept: ' . $this->getAccept(), |
|
427 | + 'Content-Type: ' . $this->getContentType() |
|
428 | + ], |
|
429 | + CURLOPT_URL => $url |
|
430 | + ]); |
|
431 | + |
|
432 | + /** |
|
433 | + * Basic authentication via username and Password |
|
434 | + * |
|
435 | + * @see https://developer.github.com/v3/auth/#via-username-and-password |
|
436 | + */ |
|
437 | + if (!empty($this->getHttpAuth())) { |
|
438 | + if (!isset($this->getHttpAuth()['password']) || empty($this->getHttpAuth()['password'])) { |
|
439 | + $curl->setOption([ |
|
440 | + CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
441 | + CURLOPT_USERPWD => $this->getHttpAuth()['username'] |
|
442 | + ]); |
|
443 | + } else { |
|
444 | + $curl->setOption([ |
|
445 | + CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
446 | + CURLOPT_USERPWD => sprintf('%s:%s', $this->getHttpAuth()['username'], |
|
447 | + $this->getHttpAuth()['password']) |
|
448 | + ]); |
|
449 | + } |
|
450 | + } |
|
451 | + |
|
452 | + if (!empty($this->getToken()) && $this->getAuthentication() !== self::OAUTH2_PARAMETERS_AUTH) { |
|
453 | + /** |
|
454 | + * Basic authentication via OAuth token |
|
455 | + * |
|
456 | + * @see https://developer.github.com/v3/auth/#via-oauth-tokens |
|
457 | + **/ |
|
458 | + if ($this->getAuthentication() === self::OAUTH_AUTH) { |
|
459 | + $curl->setOption([ |
|
460 | + CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
461 | + CURLOPT_USERPWD => sprintf('%s:x-oauth-basic', $this->getToken()) |
|
462 | + ]); |
|
463 | + } /** |
|
464 | + * Basic authentication via OAuth2 Token (sent in a header) |
|
465 | + * |
|
466 | + * @see https://developer.github.com/v3/#oauth2-token-sent-in-a-header |
|
467 | + */ else if ($this->getAuthentication() === self::OAUTH2_HEADER_AUTH) { |
|
468 | + $curl->setOption([ |
|
469 | + CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
470 | + CURLOPT_HTTPHEADER => [sprintf('Authorization: token %s', $this->getToken())] |
|
471 | + ]); |
|
472 | + } |
|
473 | + } |
|
474 | + |
|
475 | + /** Methods */ |
|
476 | + switch ($method) { |
|
477 | + /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7 */ |
|
478 | + case Request::METHOD_DELETE: |
|
479 | + /** @see http://tools.ietf.org/html/rfc5789 */ |
|
480 | + case Request::METHOD_PATCH: |
|
481 | + $curl->setOption([ |
|
482 | + CURLOPT_CUSTOMREQUEST => $method, |
|
483 | + CURLOPT_POST => true, |
|
484 | + CURLOPT_POSTFIELDS => json_encode(array_filter($postFields)) |
|
485 | + ]); |
|
486 | + break; |
|
487 | + |
|
488 | + /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3 */ |
|
489 | + case Request::METHOD_GET: |
|
490 | + $curl->setOption(CURLOPT_HTTPGET, true); |
|
491 | + break; |
|
492 | + |
|
493 | + /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4 */ |
|
494 | + case Request::METHOD_HEAD: |
|
495 | + $curl->setOption([ |
|
496 | + CURLOPT_CUSTOMREQUEST => $method, |
|
497 | + CURLOPT_NOBODY => true |
|
498 | + ]); |
|
499 | + break; |
|
500 | + |
|
501 | + /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5 */ |
|
502 | + case Request::METHOD_POST: |
|
503 | + $curl->setOption([ |
|
504 | + CURLOPT_POST => true, |
|
505 | + CURLOPT_POSTFIELDS => json_encode(array_filter($postFields)) |
|
506 | + ]); |
|
507 | + break; |
|
508 | + |
|
509 | + /** @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6 */ |
|
510 | + case Request::METHOD_PUT: |
|
511 | + $curl->setOption([ |
|
512 | + CURLOPT_CUSTOMREQUEST => $method, |
|
513 | + CURLOPT_PUT => true, |
|
514 | + CURLOPT_HTTPHEADER => [ |
|
515 | + 'X-HTTP-Method-Override: PUT', |
|
516 | + 'Content-type: application/x-www-form-urlencoded' |
|
517 | + ] |
|
518 | + ]); |
|
519 | + break; |
|
520 | + |
|
521 | + default: |
|
522 | + break; |
|
523 | + } |
|
524 | + |
|
525 | + $curl->success(function (CurlClient $instance) { |
|
526 | + $this->headers = $instance->getHeaders(); |
|
527 | + $this->success = $instance->getResponse(); |
|
528 | + $data = json_decode($this->success, true); |
|
529 | + if (JSON_ERROR_NONE === json_last_error()) { |
|
530 | + $this->success = $data; |
|
531 | + } |
|
532 | + }); |
|
533 | + $curl->error(function (CurlClient $instance) { |
|
534 | + $this->headers = $instance->getHeaders(); |
|
535 | + $this->failure = $instance->getResponse(); |
|
536 | + $data = json_decode($this->failure, true); |
|
537 | + if (JSON_ERROR_NONE === json_last_error()) { |
|
538 | + $this->failure = $data; |
|
539 | + } |
|
540 | + }); |
|
541 | + $curl->perform(); |
|
542 | + |
|
543 | + return (array)($this->success ?? $this->failure); |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * Return a formatted string. Modified version of sprintf using colon(:) |
|
548 | + * |
|
549 | + * @param string $string |
|
550 | + * @param array $params |
|
551 | + * |
|
552 | + * @return String |
|
553 | + * @throws Exception |
|
554 | + */ |
|
555 | + public function sprintf(string $string, ...$params): string |
|
556 | + { |
|
557 | + preg_match_all('/\:([A-Za-z0-9_]+)/', $string, $matches); |
|
558 | + $matches = $matches[1]; |
|
559 | + |
|
560 | + if (count($matches)) { |
|
561 | + $tokens = []; |
|
562 | + $replaces = []; |
|
563 | + |
|
564 | + foreach ($matches as $key => $value) { |
|
565 | + if (count($params) > 1 || !is_array($params[0])) { |
|
566 | + if (!array_key_exists($key, $params)) { |
|
567 | + throw new Exception('Too few arguments, missing argument: ' . $key); |
|
568 | + } |
|
569 | + $replaces[] = $params[$key]; |
|
570 | + } else { |
|
571 | + if (!array_key_exists($value, $params[0])) { |
|
572 | + throw new Exception('Missing array argument: ' . $key); |
|
573 | + } |
|
574 | + $replaces[] = $params[0][$value]; |
|
575 | + } |
|
576 | + $tokens[] = ':' . $value; |
|
577 | + } |
|
578 | + |
|
579 | + $string = str_replace($tokens, $replaces, $string); |
|
580 | + } |
|
581 | + |
|
582 | + return $string; |
|
583 | + } |
|
584 | 584 | } |
585 | 585 | \ No newline at end of file |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | |
4 | 4 | use FlexyProject\GitHub\Client; |
5 | 5 | use FlexyProject\GitHub\Receiver\{ |
6 | - Activity, Enterprise, Gists, GitData, Issues, Miscellaneous, Organizations, PullRequests, Repositories, Search, Users |
|
6 | + Activity, Enterprise, Gists, GitData, Issues, Miscellaneous, Organizations, PullRequests, Repositories, Search, Users |
|
7 | 7 | }; |
8 | 8 | |
9 | 9 | /** |
@@ -14,99 +14,99 @@ discard block |
||
14 | 14 | class ClientTest extends AbstractTest |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * Test instance of Client's class |
|
19 | - */ |
|
20 | - public function testClient() |
|
21 | - { |
|
22 | - $this->assertInstanceOf(Client::class, $this->client); |
|
23 | - } |
|
17 | + /** |
|
18 | + * Test instance of Client's class |
|
19 | + */ |
|
20 | + public function testClient() |
|
21 | + { |
|
22 | + $this->assertInstanceOf(Client::class, $this->client); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * Test instance of Activity's class |
|
27 | - */ |
|
28 | - public function testActivityReceiver() |
|
29 | - { |
|
30 | - $this->assertInstanceOf(Activity::class, $this->client->getReceiver(Client::ACTIVITY)); |
|
31 | - } |
|
25 | + /** |
|
26 | + * Test instance of Activity's class |
|
27 | + */ |
|
28 | + public function testActivityReceiver() |
|
29 | + { |
|
30 | + $this->assertInstanceOf(Activity::class, $this->client->getReceiver(Client::ACTIVITY)); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Test instance of Enterprise's class |
|
35 | - */ |
|
36 | - public function testEnterpriseReceiver() |
|
37 | - { |
|
38 | - $this->assertInstanceOf(Enterprise::class, $this->client->getReceiver(Client::ENTERPRISE)); |
|
39 | - } |
|
33 | + /** |
|
34 | + * Test instance of Enterprise's class |
|
35 | + */ |
|
36 | + public function testEnterpriseReceiver() |
|
37 | + { |
|
38 | + $this->assertInstanceOf(Enterprise::class, $this->client->getReceiver(Client::ENTERPRISE)); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Test instance of Gists's class |
|
43 | - */ |
|
44 | - public function testGistsReceiver() |
|
45 | - { |
|
46 | - $this->assertInstanceOf(Gists::class, $this->client->getReceiver(Client::GISTS)); |
|
47 | - } |
|
41 | + /** |
|
42 | + * Test instance of Gists's class |
|
43 | + */ |
|
44 | + public function testGistsReceiver() |
|
45 | + { |
|
46 | + $this->assertInstanceOf(Gists::class, $this->client->getReceiver(Client::GISTS)); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Test instance of GitData's class |
|
51 | - */ |
|
52 | - public function testGitDataReceiver() |
|
53 | - { |
|
54 | - $this->assertInstanceOf(GitData::class, $this->client->getReceiver(Client::GIT_DATA)); |
|
55 | - } |
|
49 | + /** |
|
50 | + * Test instance of GitData's class |
|
51 | + */ |
|
52 | + public function testGitDataReceiver() |
|
53 | + { |
|
54 | + $this->assertInstanceOf(GitData::class, $this->client->getReceiver(Client::GIT_DATA)); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Test instance of Issues's class |
|
59 | - */ |
|
60 | - public function testIssuesReceiver() |
|
61 | - { |
|
62 | - $this->assertInstanceOf(Issues::class, $this->client->getReceiver(Client::ISSUES)); |
|
63 | - } |
|
57 | + /** |
|
58 | + * Test instance of Issues's class |
|
59 | + */ |
|
60 | + public function testIssuesReceiver() |
|
61 | + { |
|
62 | + $this->assertInstanceOf(Issues::class, $this->client->getReceiver(Client::ISSUES)); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Test instance of Miscellaneous's class |
|
67 | - */ |
|
68 | - public function testMiscellaneousReceiver() |
|
69 | - { |
|
70 | - $this->assertInstanceOf(Miscellaneous::class, $this->client->getReceiver(Client::MISCELLANEOUS)); |
|
71 | - } |
|
65 | + /** |
|
66 | + * Test instance of Miscellaneous's class |
|
67 | + */ |
|
68 | + public function testMiscellaneousReceiver() |
|
69 | + { |
|
70 | + $this->assertInstanceOf(Miscellaneous::class, $this->client->getReceiver(Client::MISCELLANEOUS)); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Test instance of Organizations's class |
|
75 | - */ |
|
76 | - public function testOrganizationsReceiver() |
|
77 | - { |
|
78 | - $this->assertInstanceOf(Organizations::class, $this->client->getReceiver(Client::ORGANIZATIONS)); |
|
79 | - } |
|
73 | + /** |
|
74 | + * Test instance of Organizations's class |
|
75 | + */ |
|
76 | + public function testOrganizationsReceiver() |
|
77 | + { |
|
78 | + $this->assertInstanceOf(Organizations::class, $this->client->getReceiver(Client::ORGANIZATIONS)); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Test instance of PullRequests's class |
|
83 | - */ |
|
84 | - public function testPullRequestsReceiver() |
|
85 | - { |
|
86 | - $this->assertInstanceOf(PullRequests::class, $this->client->getReceiver(Client::PULL_REQUESTS)); |
|
87 | - } |
|
81 | + /** |
|
82 | + * Test instance of PullRequests's class |
|
83 | + */ |
|
84 | + public function testPullRequestsReceiver() |
|
85 | + { |
|
86 | + $this->assertInstanceOf(PullRequests::class, $this->client->getReceiver(Client::PULL_REQUESTS)); |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Test instance of Repositories's class |
|
91 | - */ |
|
92 | - public function testRepositoriesReceiver() |
|
93 | - { |
|
94 | - $this->assertInstanceOf(Repositories::class, $this->client->getReceiver(Client::REPOSITORIES)); |
|
95 | - } |
|
89 | + /** |
|
90 | + * Test instance of Repositories's class |
|
91 | + */ |
|
92 | + public function testRepositoriesReceiver() |
|
93 | + { |
|
94 | + $this->assertInstanceOf(Repositories::class, $this->client->getReceiver(Client::REPOSITORIES)); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Test instance of Search's class |
|
99 | - */ |
|
100 | - public function testSearchReceiver() |
|
101 | - { |
|
102 | - $this->assertInstanceOf(Search::class, $this->client->getReceiver(Client::SEARCH)); |
|
103 | - } |
|
97 | + /** |
|
98 | + * Test instance of Search's class |
|
99 | + */ |
|
100 | + public function testSearchReceiver() |
|
101 | + { |
|
102 | + $this->assertInstanceOf(Search::class, $this->client->getReceiver(Client::SEARCH)); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Test instance of Users's class |
|
107 | - */ |
|
108 | - public function testUsersReceiver() |
|
109 | - { |
|
110 | - $this->assertInstanceOf(Users::class, $this->client->getReceiver(Client::USERS)); |
|
111 | - } |
|
105 | + /** |
|
106 | + * Test instance of Users's class |
|
107 | + */ |
|
108 | + public function testUsersReceiver() |
|
109 | + { |
|
110 | + $this->assertInstanceOf(Users::class, $this->client->getReceiver(Client::USERS)); |
|
111 | + } |
|
112 | 112 | } |
113 | 113 | \ No newline at end of file |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | namespace FlexyProject\GitHub\Tests\Receiver; |
3 | 3 | |
4 | 4 | use FlexyProject\GitHub\{ |
5 | - Client, Receiver\Miscellaneous, Tests\AbstractTest |
|
5 | + Client, Receiver\Miscellaneous, Tests\AbstractTest |
|
6 | 6 | }; |
7 | 7 | |
8 | 8 | /** |
@@ -13,32 +13,32 @@ discard block |
||
13 | 13 | class MiscellaneousTest extends AbstractTest |
14 | 14 | { |
15 | 15 | |
16 | - /** @var Miscellaneous */ |
|
17 | - protected $miscellaneous; |
|
16 | + /** @var Miscellaneous */ |
|
17 | + protected $miscellaneous; |
|
18 | 18 | |
19 | - /** |
|
20 | - * MiscellaneousTest constructor. |
|
21 | - * |
|
22 | - * @param null $name |
|
23 | - * @param array $data |
|
24 | - * @param string $dataName |
|
25 | - */ |
|
26 | - public function __construct($name = null, array $data = [], $dataName = '') |
|
27 | - { |
|
28 | - parent::__construct($name, $data, $dataName); |
|
19 | + /** |
|
20 | + * MiscellaneousTest constructor. |
|
21 | + * |
|
22 | + * @param null $name |
|
23 | + * @param array $data |
|
24 | + * @param string $dataName |
|
25 | + */ |
|
26 | + public function __construct($name = null, array $data = [], $dataName = '') |
|
27 | + { |
|
28 | + parent::__construct($name, $data, $dataName); |
|
29 | 29 | |
30 | - // Miscellaneous |
|
31 | - $this->miscellaneous = $this->client->getReceiver(Client::MISCELLANEOUS); |
|
32 | - } |
|
30 | + // Miscellaneous |
|
31 | + $this->miscellaneous = $this->client->getReceiver(Client::MISCELLANEOUS); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Test list available Emojis |
|
36 | - */ |
|
37 | - public function testGetListEmojis() |
|
38 | - { |
|
39 | - /** @var Miscellaneous\Emojis $emojis */ |
|
40 | - $emojis = $this->miscellaneous->getReceiver(Miscellaneous::EMOJIS); |
|
34 | + /** |
|
35 | + * Test list available Emojis |
|
36 | + */ |
|
37 | + public function testGetListEmojis() |
|
38 | + { |
|
39 | + /** @var Miscellaneous\Emojis $emojis */ |
|
40 | + $emojis = $this->miscellaneous->getReceiver(Miscellaneous::EMOJIS); |
|
41 | 41 | |
42 | - $this->assertEquals(1508, count($emojis->get())); |
|
43 | - } |
|
42 | + $this->assertEquals(1508, count($emojis->get())); |
|
43 | + } |
|
44 | 44 | } |
45 | 45 | \ No newline at end of file |
@@ -12,28 +12,28 @@ |
||
12 | 12 | abstract class AbstractTest extends TestCase |
13 | 13 | { |
14 | 14 | |
15 | - /** Authentication constants */ |
|
16 | - const USERNAME = 'githubapi-octocat'; |
|
17 | - const PASSWORD = 'S=p}g2E($L#T(K3x'; |
|
15 | + /** Authentication constants */ |
|
16 | + const USERNAME = 'githubapi-octocat'; |
|
17 | + const PASSWORD = 'S=p}g2E($L#T(K3x'; |
|
18 | 18 | |
19 | - /** @var Client */ |
|
20 | - protected $client; |
|
19 | + /** @var Client */ |
|
20 | + protected $client; |
|
21 | 21 | |
22 | - /** |
|
23 | - * AbstractTest constructor. |
|
24 | - * |
|
25 | - * @param null $name |
|
26 | - * @param array $data |
|
27 | - * @param string $dataName |
|
28 | - */ |
|
29 | - public function __construct($name = null, array $data = [], $dataName = '') |
|
30 | - { |
|
31 | - // Create a client object |
|
32 | - $this->client = new Client(); |
|
22 | + /** |
|
23 | + * AbstractTest constructor. |
|
24 | + * |
|
25 | + * @param null $name |
|
26 | + * @param array $data |
|
27 | + * @param string $dataName |
|
28 | + */ |
|
29 | + public function __construct($name = null, array $data = [], $dataName = '') |
|
30 | + { |
|
31 | + // Create a client object |
|
32 | + $this->client = new Client(); |
|
33 | 33 | |
34 | - // Set auth credentials |
|
35 | - $this->client->setHttpAuth(self::USERNAME, self::PASSWORD); |
|
34 | + // Set auth credentials |
|
35 | + $this->client->setHttpAuth(self::USERNAME, self::PASSWORD); |
|
36 | 36 | |
37 | - parent::__construct($name, $data, $dataName); |
|
38 | - } |
|
37 | + parent::__construct($name, $data, $dataName); |
|
38 | + } |
|
39 | 39 | } |
40 | 40 | \ No newline at end of file |
@@ -12,91 +12,91 @@ |
||
12 | 12 | class Comments extends AbstractGists |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * List comments on a gist |
|
17 | - * |
|
18 | - * @link https://developer.github.com/v3/gists/comments/#list-comments-on-a-gist |
|
19 | - * |
|
20 | - * @param string $gistId |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function listComments(string $gistId): array |
|
25 | - { |
|
26 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments', $gistId)); |
|
27 | - } |
|
15 | + /** |
|
16 | + * List comments on a gist |
|
17 | + * |
|
18 | + * @link https://developer.github.com/v3/gists/comments/#list-comments-on-a-gist |
|
19 | + * |
|
20 | + * @param string $gistId |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function listComments(string $gistId): array |
|
25 | + { |
|
26 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments', $gistId)); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Get a single comment |
|
31 | - * |
|
32 | - * @link https://developer.github.com/v3/gists/comments/#get-a-single-comment |
|
33 | - * |
|
34 | - * @param string $gistId |
|
35 | - * @param int $id |
|
36 | - * |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - public function getSingleComment(string $gistId, int $id): array |
|
40 | - { |
|
41 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments/:id', $gistId, (string)$id)); |
|
42 | - } |
|
29 | + /** |
|
30 | + * Get a single comment |
|
31 | + * |
|
32 | + * @link https://developer.github.com/v3/gists/comments/#get-a-single-comment |
|
33 | + * |
|
34 | + * @param string $gistId |
|
35 | + * @param int $id |
|
36 | + * |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + public function getSingleComment(string $gistId, int $id): array |
|
40 | + { |
|
41 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments/:id', $gistId, (string)$id)); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Create a comment |
|
46 | - * |
|
47 | - * @link https://developer.github.com/v3/gists/comments/#create-a-comment |
|
48 | - * |
|
49 | - * @param string $gistId |
|
50 | - * @param string $body |
|
51 | - * |
|
52 | - * @return array |
|
53 | - */ |
|
54 | - public function createComment(string $gistId, string $body): array |
|
55 | - { |
|
56 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments', $gistId), |
|
57 | - Request::METHOD_POST, [ |
|
58 | - 'body' => $body |
|
59 | - ]); |
|
60 | - } |
|
44 | + /** |
|
45 | + * Create a comment |
|
46 | + * |
|
47 | + * @link https://developer.github.com/v3/gists/comments/#create-a-comment |
|
48 | + * |
|
49 | + * @param string $gistId |
|
50 | + * @param string $body |
|
51 | + * |
|
52 | + * @return array |
|
53 | + */ |
|
54 | + public function createComment(string $gistId, string $body): array |
|
55 | + { |
|
56 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments', $gistId), |
|
57 | + Request::METHOD_POST, [ |
|
58 | + 'body' => $body |
|
59 | + ]); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Edit a comment |
|
64 | - * |
|
65 | - * @link https://developer.github.com/v3/gists/comments/#edit-a-comment |
|
66 | - * |
|
67 | - * @param string $gistId |
|
68 | - * @param int $id |
|
69 | - * @param string $body |
|
70 | - * |
|
71 | - * @return array |
|
72 | - */ |
|
73 | - public function editComment(string $gistId, int $id, string $body): array |
|
74 | - { |
|
75 | - return $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments/:id', $gistId, (string)$id), |
|
76 | - Request::METHOD_PATCH, [ |
|
77 | - 'body' => $body |
|
78 | - ]); |
|
79 | - } |
|
62 | + /** |
|
63 | + * Edit a comment |
|
64 | + * |
|
65 | + * @link https://developer.github.com/v3/gists/comments/#edit-a-comment |
|
66 | + * |
|
67 | + * @param string $gistId |
|
68 | + * @param int $id |
|
69 | + * @param string $body |
|
70 | + * |
|
71 | + * @return array |
|
72 | + */ |
|
73 | + public function editComment(string $gistId, int $id, string $body): array |
|
74 | + { |
|
75 | + return $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments/:id', $gistId, (string)$id), |
|
76 | + Request::METHOD_PATCH, [ |
|
77 | + 'body' => $body |
|
78 | + ]); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Delete a comment |
|
83 | - * |
|
84 | - * @link https://developer.github.com/v3/gists/comments/#delete-a-comment |
|
85 | - * |
|
86 | - * @param string $gistId |
|
87 | - * @param int $id |
|
88 | - * |
|
89 | - * @return bool |
|
90 | - */ |
|
91 | - public function deleteComment(string $gistId, int $id): bool |
|
92 | - { |
|
93 | - $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments/:id', $gistId, (string)$id), |
|
94 | - Request::METHOD_DELETE); |
|
81 | + /** |
|
82 | + * Delete a comment |
|
83 | + * |
|
84 | + * @link https://developer.github.com/v3/gists/comments/#delete-a-comment |
|
85 | + * |
|
86 | + * @param string $gistId |
|
87 | + * @param int $id |
|
88 | + * |
|
89 | + * @return bool |
|
90 | + */ |
|
91 | + public function deleteComment(string $gistId, int $id): bool |
|
92 | + { |
|
93 | + $this->getApi()->request($this->getApi()->sprintf('/gists/:gist_id/comments/:id', $gistId, (string)$id), |
|
94 | + Request::METHOD_DELETE); |
|
95 | 95 | |
96 | - if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
97 | - return true; |
|
98 | - } |
|
96 | + if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
97 | + return true; |
|
98 | + } |
|
99 | 99 | |
100 | - return false; |
|
101 | - } |
|
100 | + return false; |
|
101 | + } |
|
102 | 102 | } |
103 | 103 | \ No newline at end of file |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | namespace FlexyProject\GitHub\Tests\Receiver; |
3 | 3 | |
4 | 4 | use FlexyProject\GitHub\{ |
5 | - Client, Receiver\Gists, Tests\AbstractTest |
|
5 | + Client, Receiver\Gists, Tests\AbstractTest |
|
6 | 6 | }; |
7 | 7 | |
8 | 8 | /** |
@@ -12,327 +12,327 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GistsTest extends AbstractTest |
14 | 14 | { |
15 | - /** Public test gist ID */ |
|
16 | - const PUBLIC_GIST = '76e253825bb3c6c084cf31f92997eb72'; |
|
17 | - |
|
18 | - /** @var Gists */ |
|
19 | - protected $gists; |
|
20 | - |
|
21 | - /** @var Gists\Comments */ |
|
22 | - protected $comments; |
|
23 | - |
|
24 | - /** |
|
25 | - * GistsTest constructor. |
|
26 | - * |
|
27 | - * @param null $name |
|
28 | - * @param array $data |
|
29 | - * @param string $dataName |
|
30 | - */ |
|
31 | - public function __construct($name = null, array $data = [], $dataName = '') |
|
32 | - { |
|
33 | - parent::__construct($name, $data, $dataName); |
|
34 | - |
|
35 | - // Gists |
|
36 | - $this->gists = $this->client->getReceiver(Client::GISTS); |
|
37 | - |
|
38 | - // Comments |
|
39 | - $this->comments = $this->gists->getReceiver(Gists::COMMENTS); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Test list gists of current users |
|
44 | - */ |
|
45 | - public function testListGists() |
|
46 | - { |
|
47 | - $gists = $this->gists->listGists(); |
|
48 | - if (!empty($gists)) { |
|
49 | - $gist = array_pop($gists); |
|
50 | - |
|
51 | - $this->assertArrayHasKey('url', $gist); |
|
52 | - $this->assertArrayHasKey('files', $gist); |
|
53 | - $this->assertArrayHasKey('comments', $gist); |
|
54 | - $this->assertArrayHasKey('created_at', $gist); |
|
55 | - $this->assertArrayHasKey('updated_at', $gist); |
|
56 | - $this->assertArrayHasKey('user', $gist); |
|
57 | - } |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Test list public gists |
|
62 | - */ |
|
63 | - public function testPublicListGists() |
|
64 | - { |
|
65 | - $gists = $this->gists->listPublicGists(); |
|
66 | - if (!empty($gists)) { |
|
67 | - $gist = array_pop($gists); |
|
68 | - |
|
69 | - $this->assertArrayHasKey('url', $gist); |
|
70 | - $this->assertArrayHasKey('files', $gist); |
|
71 | - $this->assertArrayHasKey('comments', $gist); |
|
72 | - $this->assertArrayHasKey('created_at', $gist); |
|
73 | - $this->assertArrayHasKey('updated_at', $gist); |
|
74 | - $this->assertArrayHasKey('user', $gist); |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Test list user's starred gists |
|
80 | - */ |
|
81 | - public function testListUsersStarredGists() |
|
82 | - { |
|
83 | - $gists = $this->gists->listUsersStarredGists(); |
|
84 | - if (!empty($gists)) { |
|
85 | - $gist = array_pop($gists); |
|
86 | - |
|
87 | - $this->assertArrayHasKey('url', $gist); |
|
88 | - $this->assertArrayHasKey('files', $gist); |
|
89 | - $this->assertArrayHasKey('comments', $gist); |
|
90 | - $this->assertArrayHasKey('created_at', $gist); |
|
91 | - $this->assertArrayHasKey('updated_at', $gist); |
|
92 | - $this->assertArrayHasKey('user', $gist); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Test creating a new gist |
|
98 | - * |
|
99 | - * @return string |
|
100 | - */ |
|
101 | - public function testCreateGist(): string |
|
102 | - { |
|
103 | - $gist = $this->gists->createGist([ |
|
104 | - md5('phpunit-testing') . '.txt' => [ |
|
105 | - 'content' => 'String file contents' |
|
106 | - ] |
|
107 | - ], 'the description for this gist'); |
|
108 | - |
|
109 | - $this->assertArrayHasKey('url', $gist); |
|
110 | - $this->assertArrayHasKey('files', $gist); |
|
111 | - $this->assertArrayHasKey('comments', $gist); |
|
112 | - $this->assertArrayHasKey('created_at', $gist); |
|
113 | - $this->assertArrayHasKey('updated_at', $gist); |
|
114 | - $this->assertArrayHasKey('user', $gist); |
|
115 | - |
|
116 | - return $gist['id']; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Test getting gist by ID |
|
121 | - * |
|
122 | - * @depends testCreateGist |
|
123 | - * |
|
124 | - * @param string $gistId |
|
125 | - */ |
|
126 | - public function testGetGistById(string $gistId) |
|
127 | - { |
|
128 | - $gist = $this->gists->getGist($gistId); |
|
129 | - |
|
130 | - $this->assertArrayHasKey('url', $gist); |
|
131 | - $this->assertArrayHasKey('files', $gist); |
|
132 | - $this->assertArrayHasKey('comments', $gist); |
|
133 | - $this->assertArrayHasKey('created_at', $gist); |
|
134 | - $this->assertArrayHasKey('updated_at', $gist); |
|
135 | - $this->assertArrayHasKey('user', $gist); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Test updating an existing gist |
|
140 | - * |
|
141 | - * @depends testCreateGist |
|
142 | - * |
|
143 | - * @param string $gistId |
|
144 | - */ |
|
145 | - public function testUpdateGist(string $gistId) |
|
146 | - { |
|
147 | - $gist = $this->gists->editGist($gistId, 'the description UPDATED for this gist', [ |
|
148 | - md5('phpunit-testing') . '.txt' => [ |
|
149 | - 'content' => 'String file contents' |
|
150 | - ] |
|
151 | - ], 'content', 'renamed-file.name'); |
|
152 | - |
|
153 | - $this->assertEquals('the description UPDATED for this gist', $gist['description']); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Test list commits of a gist |
|
158 | - * |
|
159 | - * @depends testCreateGist |
|
160 | - * |
|
161 | - * @param string $gistId |
|
162 | - */ |
|
163 | - public function testListGistsCommit(string $gistId) |
|
164 | - { |
|
165 | - $gists = $this->gists->listGistsCommits($gistId); |
|
166 | - $gist = array_pop($gists); |
|
167 | - |
|
168 | - $this->assertArrayHasKey('user', $gist); |
|
169 | - $this->assertArrayHasKey('version', $gist); |
|
170 | - $this->assertArrayHasKey('committed_at', $gist); |
|
171 | - $this->assertArrayHasKey('change_status', $gist); |
|
172 | - $this->assertArrayHasKey('url', $gist); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Test starring a gist |
|
177 | - * |
|
178 | - * @depends testCreateGist |
|
179 | - * |
|
180 | - * @param string $gistId |
|
181 | - */ |
|
182 | - public function testStarGist(string $gistId) |
|
183 | - { |
|
184 | - $this->assertTrue($this->gists->starGist($gistId)); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Test gist is starred |
|
189 | - * |
|
190 | - * @depends testCreateGist |
|
191 | - * |
|
192 | - * @param string $gistId |
|
193 | - */ |
|
194 | - public function testGistIsStarred(string $gistId) |
|
195 | - { |
|
196 | - $this->assertTrue($this->gists->checkGistIsStarred($gistId)); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Test unstar a gist |
|
201 | - * |
|
202 | - * @depends testCreateGist |
|
203 | - * |
|
204 | - * @param string $gistId |
|
205 | - */ |
|
206 | - public function testUnStarGist(string $gistId) |
|
207 | - { |
|
208 | - $this->assertTrue($this->gists->unStarGist($gistId)); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Test fork a public gist |
|
213 | - */ |
|
214 | - public function testForkGist() |
|
215 | - { |
|
216 | - $gist = $this->gists->forkGist(self::PUBLIC_GIST); |
|
217 | - |
|
218 | - $this->assertArrayHasKey('forks_url', $gist); |
|
219 | - |
|
220 | - return $gist['id']; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Test list forks of a specific gist |
|
225 | - */ |
|
226 | - public function testListGistForks() |
|
227 | - { |
|
228 | - $gists = $this->gists->listGistForks(self::PUBLIC_GIST); |
|
229 | - $gist = array_pop($gists); |
|
230 | - |
|
231 | - $this->assertArrayHasKey('url', $gist); |
|
232 | - $this->assertArrayHasKey('id', $gist); |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Test deleting a forked gist |
|
237 | - * |
|
238 | - * @depends testForkGist |
|
239 | - * |
|
240 | - * @param string $gistId |
|
241 | - */ |
|
242 | - public function testDeleteForkedGist(string $gistId) |
|
243 | - { |
|
244 | - $this->assertTrue($this->gists->deleteGist($gistId)); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Test create a new comment in specific gist |
|
249 | - * |
|
250 | - * @depends testCreateGist |
|
251 | - * |
|
252 | - * @param string $gistId |
|
253 | - */ |
|
254 | - public function testCreateComment(string $gistId) |
|
255 | - { |
|
256 | - $response = $this->comments->createComment($gistId, 'Just commenting for the sake of commenting'); |
|
257 | - |
|
258 | - $this->assertEquals('Just commenting for the sake of commenting', $response['body']); |
|
259 | - |
|
260 | - return $response['id']; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Test listing all comments for specific gist |
|
265 | - * |
|
266 | - * @depends testCreateGist |
|
267 | - * |
|
268 | - * @param string $gistId |
|
269 | - */ |
|
270 | - public function testListComments(string $gistId) |
|
271 | - { |
|
272 | - $comments = $this->comments->listComments($gistId); |
|
273 | - $comment = array_pop($comments); |
|
274 | - |
|
275 | - $this->assertArrayHasKey('id', $comment); |
|
276 | - $this->assertArrayHasKey('url', $comment); |
|
277 | - $this->assertArrayHasKey('body', $comment); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * Test getting a single comment |
|
282 | - * |
|
283 | - * @depends testCreateGist |
|
284 | - * @depends testCreateComment |
|
285 | - * |
|
286 | - * @param string $gistId |
|
287 | - * @param string $commentId |
|
288 | - */ |
|
289 | - public function testGetSingleComment(string $gistId, string $commentId) |
|
290 | - { |
|
291 | - $response = $this->comments->getSingleComment($gistId, $commentId); |
|
292 | - |
|
293 | - $this->assertEquals('Just commenting for the sake of commenting', $response['body']); |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Test editing a gist's comment |
|
298 | - * |
|
299 | - * @depends testCreateGist |
|
300 | - * @depends testCreateComment |
|
301 | - * |
|
302 | - * @param string $gistId |
|
303 | - * @param string $commentId |
|
304 | - */ |
|
305 | - public function testEditComment(string $gistId, string $commentId) |
|
306 | - { |
|
307 | - $response = $this->comments->editComment($gistId, $commentId, 'Just editing this comment!'); |
|
308 | - |
|
309 | - $this->assertEquals('Just editing this comment!', $response['body']); |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Test deleting a comment |
|
314 | - * |
|
315 | - * @depends testCreateGist |
|
316 | - * @depends testCreateComment |
|
317 | - * |
|
318 | - * @param string $gistId |
|
319 | - * @param string $commentId |
|
320 | - */ |
|
321 | - public function testDeleteComment(string $gistId, string $commentId) |
|
322 | - { |
|
323 | - $this->assertTrue($this->comments->deleteComment($gistId, $commentId)); |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Test deleting an existing gist |
|
328 | - * |
|
329 | - * @depends testCreateGist |
|
330 | - * |
|
331 | - * @param string $gistId |
|
332 | - */ |
|
333 | - public function testDeleteGist(string $gistId) |
|
334 | - { |
|
335 | - $this->assertTrue($this->gists->deleteGist($gistId)); |
|
336 | - } |
|
15 | + /** Public test gist ID */ |
|
16 | + const PUBLIC_GIST = '76e253825bb3c6c084cf31f92997eb72'; |
|
17 | + |
|
18 | + /** @var Gists */ |
|
19 | + protected $gists; |
|
20 | + |
|
21 | + /** @var Gists\Comments */ |
|
22 | + protected $comments; |
|
23 | + |
|
24 | + /** |
|
25 | + * GistsTest constructor. |
|
26 | + * |
|
27 | + * @param null $name |
|
28 | + * @param array $data |
|
29 | + * @param string $dataName |
|
30 | + */ |
|
31 | + public function __construct($name = null, array $data = [], $dataName = '') |
|
32 | + { |
|
33 | + parent::__construct($name, $data, $dataName); |
|
34 | + |
|
35 | + // Gists |
|
36 | + $this->gists = $this->client->getReceiver(Client::GISTS); |
|
37 | + |
|
38 | + // Comments |
|
39 | + $this->comments = $this->gists->getReceiver(Gists::COMMENTS); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Test list gists of current users |
|
44 | + */ |
|
45 | + public function testListGists() |
|
46 | + { |
|
47 | + $gists = $this->gists->listGists(); |
|
48 | + if (!empty($gists)) { |
|
49 | + $gist = array_pop($gists); |
|
50 | + |
|
51 | + $this->assertArrayHasKey('url', $gist); |
|
52 | + $this->assertArrayHasKey('files', $gist); |
|
53 | + $this->assertArrayHasKey('comments', $gist); |
|
54 | + $this->assertArrayHasKey('created_at', $gist); |
|
55 | + $this->assertArrayHasKey('updated_at', $gist); |
|
56 | + $this->assertArrayHasKey('user', $gist); |
|
57 | + } |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Test list public gists |
|
62 | + */ |
|
63 | + public function testPublicListGists() |
|
64 | + { |
|
65 | + $gists = $this->gists->listPublicGists(); |
|
66 | + if (!empty($gists)) { |
|
67 | + $gist = array_pop($gists); |
|
68 | + |
|
69 | + $this->assertArrayHasKey('url', $gist); |
|
70 | + $this->assertArrayHasKey('files', $gist); |
|
71 | + $this->assertArrayHasKey('comments', $gist); |
|
72 | + $this->assertArrayHasKey('created_at', $gist); |
|
73 | + $this->assertArrayHasKey('updated_at', $gist); |
|
74 | + $this->assertArrayHasKey('user', $gist); |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Test list user's starred gists |
|
80 | + */ |
|
81 | + public function testListUsersStarredGists() |
|
82 | + { |
|
83 | + $gists = $this->gists->listUsersStarredGists(); |
|
84 | + if (!empty($gists)) { |
|
85 | + $gist = array_pop($gists); |
|
86 | + |
|
87 | + $this->assertArrayHasKey('url', $gist); |
|
88 | + $this->assertArrayHasKey('files', $gist); |
|
89 | + $this->assertArrayHasKey('comments', $gist); |
|
90 | + $this->assertArrayHasKey('created_at', $gist); |
|
91 | + $this->assertArrayHasKey('updated_at', $gist); |
|
92 | + $this->assertArrayHasKey('user', $gist); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Test creating a new gist |
|
98 | + * |
|
99 | + * @return string |
|
100 | + */ |
|
101 | + public function testCreateGist(): string |
|
102 | + { |
|
103 | + $gist = $this->gists->createGist([ |
|
104 | + md5('phpunit-testing') . '.txt' => [ |
|
105 | + 'content' => 'String file contents' |
|
106 | + ] |
|
107 | + ], 'the description for this gist'); |
|
108 | + |
|
109 | + $this->assertArrayHasKey('url', $gist); |
|
110 | + $this->assertArrayHasKey('files', $gist); |
|
111 | + $this->assertArrayHasKey('comments', $gist); |
|
112 | + $this->assertArrayHasKey('created_at', $gist); |
|
113 | + $this->assertArrayHasKey('updated_at', $gist); |
|
114 | + $this->assertArrayHasKey('user', $gist); |
|
115 | + |
|
116 | + return $gist['id']; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Test getting gist by ID |
|
121 | + * |
|
122 | + * @depends testCreateGist |
|
123 | + * |
|
124 | + * @param string $gistId |
|
125 | + */ |
|
126 | + public function testGetGistById(string $gistId) |
|
127 | + { |
|
128 | + $gist = $this->gists->getGist($gistId); |
|
129 | + |
|
130 | + $this->assertArrayHasKey('url', $gist); |
|
131 | + $this->assertArrayHasKey('files', $gist); |
|
132 | + $this->assertArrayHasKey('comments', $gist); |
|
133 | + $this->assertArrayHasKey('created_at', $gist); |
|
134 | + $this->assertArrayHasKey('updated_at', $gist); |
|
135 | + $this->assertArrayHasKey('user', $gist); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Test updating an existing gist |
|
140 | + * |
|
141 | + * @depends testCreateGist |
|
142 | + * |
|
143 | + * @param string $gistId |
|
144 | + */ |
|
145 | + public function testUpdateGist(string $gistId) |
|
146 | + { |
|
147 | + $gist = $this->gists->editGist($gistId, 'the description UPDATED for this gist', [ |
|
148 | + md5('phpunit-testing') . '.txt' => [ |
|
149 | + 'content' => 'String file contents' |
|
150 | + ] |
|
151 | + ], 'content', 'renamed-file.name'); |
|
152 | + |
|
153 | + $this->assertEquals('the description UPDATED for this gist', $gist['description']); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Test list commits of a gist |
|
158 | + * |
|
159 | + * @depends testCreateGist |
|
160 | + * |
|
161 | + * @param string $gistId |
|
162 | + */ |
|
163 | + public function testListGistsCommit(string $gistId) |
|
164 | + { |
|
165 | + $gists = $this->gists->listGistsCommits($gistId); |
|
166 | + $gist = array_pop($gists); |
|
167 | + |
|
168 | + $this->assertArrayHasKey('user', $gist); |
|
169 | + $this->assertArrayHasKey('version', $gist); |
|
170 | + $this->assertArrayHasKey('committed_at', $gist); |
|
171 | + $this->assertArrayHasKey('change_status', $gist); |
|
172 | + $this->assertArrayHasKey('url', $gist); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Test starring a gist |
|
177 | + * |
|
178 | + * @depends testCreateGist |
|
179 | + * |
|
180 | + * @param string $gistId |
|
181 | + */ |
|
182 | + public function testStarGist(string $gistId) |
|
183 | + { |
|
184 | + $this->assertTrue($this->gists->starGist($gistId)); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Test gist is starred |
|
189 | + * |
|
190 | + * @depends testCreateGist |
|
191 | + * |
|
192 | + * @param string $gistId |
|
193 | + */ |
|
194 | + public function testGistIsStarred(string $gistId) |
|
195 | + { |
|
196 | + $this->assertTrue($this->gists->checkGistIsStarred($gistId)); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Test unstar a gist |
|
201 | + * |
|
202 | + * @depends testCreateGist |
|
203 | + * |
|
204 | + * @param string $gistId |
|
205 | + */ |
|
206 | + public function testUnStarGist(string $gistId) |
|
207 | + { |
|
208 | + $this->assertTrue($this->gists->unStarGist($gistId)); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Test fork a public gist |
|
213 | + */ |
|
214 | + public function testForkGist() |
|
215 | + { |
|
216 | + $gist = $this->gists->forkGist(self::PUBLIC_GIST); |
|
217 | + |
|
218 | + $this->assertArrayHasKey('forks_url', $gist); |
|
219 | + |
|
220 | + return $gist['id']; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Test list forks of a specific gist |
|
225 | + */ |
|
226 | + public function testListGistForks() |
|
227 | + { |
|
228 | + $gists = $this->gists->listGistForks(self::PUBLIC_GIST); |
|
229 | + $gist = array_pop($gists); |
|
230 | + |
|
231 | + $this->assertArrayHasKey('url', $gist); |
|
232 | + $this->assertArrayHasKey('id', $gist); |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Test deleting a forked gist |
|
237 | + * |
|
238 | + * @depends testForkGist |
|
239 | + * |
|
240 | + * @param string $gistId |
|
241 | + */ |
|
242 | + public function testDeleteForkedGist(string $gistId) |
|
243 | + { |
|
244 | + $this->assertTrue($this->gists->deleteGist($gistId)); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Test create a new comment in specific gist |
|
249 | + * |
|
250 | + * @depends testCreateGist |
|
251 | + * |
|
252 | + * @param string $gistId |
|
253 | + */ |
|
254 | + public function testCreateComment(string $gistId) |
|
255 | + { |
|
256 | + $response = $this->comments->createComment($gistId, 'Just commenting for the sake of commenting'); |
|
257 | + |
|
258 | + $this->assertEquals('Just commenting for the sake of commenting', $response['body']); |
|
259 | + |
|
260 | + return $response['id']; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Test listing all comments for specific gist |
|
265 | + * |
|
266 | + * @depends testCreateGist |
|
267 | + * |
|
268 | + * @param string $gistId |
|
269 | + */ |
|
270 | + public function testListComments(string $gistId) |
|
271 | + { |
|
272 | + $comments = $this->comments->listComments($gistId); |
|
273 | + $comment = array_pop($comments); |
|
274 | + |
|
275 | + $this->assertArrayHasKey('id', $comment); |
|
276 | + $this->assertArrayHasKey('url', $comment); |
|
277 | + $this->assertArrayHasKey('body', $comment); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * Test getting a single comment |
|
282 | + * |
|
283 | + * @depends testCreateGist |
|
284 | + * @depends testCreateComment |
|
285 | + * |
|
286 | + * @param string $gistId |
|
287 | + * @param string $commentId |
|
288 | + */ |
|
289 | + public function testGetSingleComment(string $gistId, string $commentId) |
|
290 | + { |
|
291 | + $response = $this->comments->getSingleComment($gistId, $commentId); |
|
292 | + |
|
293 | + $this->assertEquals('Just commenting for the sake of commenting', $response['body']); |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Test editing a gist's comment |
|
298 | + * |
|
299 | + * @depends testCreateGist |
|
300 | + * @depends testCreateComment |
|
301 | + * |
|
302 | + * @param string $gistId |
|
303 | + * @param string $commentId |
|
304 | + */ |
|
305 | + public function testEditComment(string $gistId, string $commentId) |
|
306 | + { |
|
307 | + $response = $this->comments->editComment($gistId, $commentId, 'Just editing this comment!'); |
|
308 | + |
|
309 | + $this->assertEquals('Just editing this comment!', $response['body']); |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Test deleting a comment |
|
314 | + * |
|
315 | + * @depends testCreateGist |
|
316 | + * @depends testCreateComment |
|
317 | + * |
|
318 | + * @param string $gistId |
|
319 | + * @param string $commentId |
|
320 | + */ |
|
321 | + public function testDeleteComment(string $gistId, string $commentId) |
|
322 | + { |
|
323 | + $this->assertTrue($this->comments->deleteComment($gistId, $commentId)); |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Test deleting an existing gist |
|
328 | + * |
|
329 | + * @depends testCreateGist |
|
330 | + * |
|
331 | + * @param string $gistId |
|
332 | + */ |
|
333 | + public function testDeleteGist(string $gistId) |
|
334 | + { |
|
335 | + $this->assertTrue($this->gists->deleteGist($gistId)); |
|
336 | + } |
|
337 | 337 | |
338 | 338 | } |
339 | 339 | \ No newline at end of file |