Test Failed
Pull Request — master (#46)
by David
02:47
created

GistsTest::testPublicListGists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 0
1
<?php
2
namespace FlexyProject\GitHub\Tests\Receiver;
3
4
use FlexyProject\GitHub\{
5
    Client, Receiver\Gists, Tests\AbstractTest
6
};
7
8
/**
9
 * Class GistsTest
10
 *
11
 * @package FlexyProject\GitHub\Tests
12
 */
13
class GistsTest extends AbstractTest
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 View Code Duplication
    public function testListGists()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testPublicListGists()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testListUsersStarredGists()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testListGistsCommit(string $gistId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
338
}