Completed
Push — 1.1 ( 8fee57...bd876d )
by David
03:10
created

GistsTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 235
Duplicated Lines 25.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 60
loc 235
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A testListGists() 14 14 2
A testPublicListGists() 14 14 2
A testListUsersStarredGists() 14 14 2
A testGetGistById() 0 11 1
A testCreateGist() 0 17 1
A testUpdateGist() 0 10 1
A testListGistsCommit() 11 11 1
A testStarGist() 0 4 1
A testGistIsStarred() 0 4 1
A testUnStarGist() 0 4 1
A testForkGist() 0 8 1
A testListGistForks() 0 8 1
A testDeleteGist() 0 4 1
A testDeleteForkedGist() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    const PUBLIC_GIST = '76e253825bb3c6c084cf31f92997eb72';
16
17
    /** @var Gists */
18
    protected $gists;
19
20
    /**
21
     * GistsTest constructor.
22
     *
23
     * @param null   $name
24
     * @param array  $data
25
     * @param string $dataName
26
     */
27 View Code Duplication
    public function __construct($name = null, array $data = [], $dataName = '')
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...
28
    {
29
        parent::__construct($name, $data, $dataName);
30
31
        // Gists
32
        $this->gists = $this->client->getReceiver(Client::GISTS);
33
    }
34
35
    /**
36
     * Test list gists of current users
37
     */
38 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...
39
    {
40
        $gists = $this->gists->listGists();
41
        if (!empty($gists)) {
42
            $gist = array_pop($gists);
43
44
            $this->assertArrayHasKey('url', $gist);
45
            $this->assertArrayHasKey('files', $gist);
46
            $this->assertArrayHasKey('comments', $gist);
47
            $this->assertArrayHasKey('created_at', $gist);
48
            $this->assertArrayHasKey('updated_at', $gist);
49
            $this->assertArrayHasKey('user', $gist);
50
        }
51
    }
52
53
    /**
54
     * Test list public gists
55
     */
56 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...
57
    {
58
        $gists = $this->gists->listPublicGists();
59
        if (!empty($gists)) {
60
            $gist = array_pop($gists);
61
62
            $this->assertArrayHasKey('url', $gist);
63
            $this->assertArrayHasKey('files', $gist);
64
            $this->assertArrayHasKey('comments', $gist);
65
            $this->assertArrayHasKey('created_at', $gist);
66
            $this->assertArrayHasKey('updated_at', $gist);
67
            $this->assertArrayHasKey('user', $gist);
68
        }
69
    }
70
71
    /**
72
     * Test list user's starred gists
73
     */
74 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...
75
    {
76
        $gists = $this->gists->listUsersStarredGists();
77
        if (!empty($gists)) {
78
            $gist = array_pop($gists);
79
80
            $this->assertArrayHasKey('url', $gist);
81
            $this->assertArrayHasKey('files', $gist);
82
            $this->assertArrayHasKey('comments', $gist);
83
            $this->assertArrayHasKey('created_at', $gist);
84
            $this->assertArrayHasKey('updated_at', $gist);
85
            $this->assertArrayHasKey('user', $gist);
86
        }
87
    }
88
89
    /**
90
     * Test getting gist by ID
91
     */
92
    public function testGetGistById()
93
    {
94
        $gist = $this->gists->getGist(1);
95
96
        $this->assertArrayHasKey('url', $gist);
97
        $this->assertArrayHasKey('files', $gist);
98
        $this->assertArrayHasKey('comments', $gist);
99
        $this->assertArrayHasKey('created_at', $gist);
100
        $this->assertArrayHasKey('updated_at', $gist);
101
        $this->assertArrayHasKey('user', $gist);
102
    }
103
104
    /**
105
     * Test creating a new gist
106
     *
107
     * @return string
108
     */
109
    public function testCreateGist(): string
110
    {
111
        $gist = $this->gists->createGist([
112
            md5('phpunit-testing') . '.txt' => [
113
                'content' => 'String file contents'
114
            ]
115
        ], 'the description for this gist', true);
116
117
        $this->assertArrayHasKey('url', $gist);
118
        $this->assertArrayHasKey('files', $gist);
119
        $this->assertArrayHasKey('comments', $gist);
120
        $this->assertArrayHasKey('created_at', $gist);
121
        $this->assertArrayHasKey('updated_at', $gist);
122
        $this->assertArrayHasKey('user', $gist);
123
124
        return $gist['id'];
125
    }
126
127
    /**
128
     * Test updating an existing gist
129
     *
130
     * @depends testCreateGist
131
     *
132
     * @param string $gistId
133
     */
134
    public function testUpdateGist(string $gistId)
135
    {
136
        $gist = $this->gists->editGist($gistId, 'the description UPDATED for this gist', [
137
            md5('phpunit-testing') . '.txt' => [
138
                'content' => 'String file contents'
139
            ]
140
        ], 'content', 'renamed-file.name');
141
142
        $this->assertEquals('the description UPDATED for this gist', $gist['description']);
143
    }
144
145
    /**
146
     * Test list commits of a gist
147
     *
148
     * @depends testCreateGist
149
     *
150
     * @param string $gistId
151
     */
152 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...
153
    {
154
        $gists = $this->gists->listGistsCommits($gistId);
155
        $gist  = array_pop($gists);
156
157
        $this->assertArrayHasKey('user', $gist);
158
        $this->assertArrayHasKey('version', $gist);
159
        $this->assertArrayHasKey('committed_at', $gist);
160
        $this->assertArrayHasKey('change_status', $gist);
161
        $this->assertArrayHasKey('url', $gist);
162
    }
163
164
    /**
165
     * Test starring a gist
166
     *
167
     * @depends testCreateGist
168
     *
169
     * @param string $gistId
170
     */
171
    public function testStarGist(string $gistId)
172
    {
173
        $this->assertTrue($this->gists->starGist($gistId));
174
    }
175
176
    /**
177
     * Test gist is starred
178
     *
179
     * @depends testCreateGist
180
     *
181
     * @param string $gistId
182
     */
183
    public function testGistIsStarred(string $gistId)
184
    {
185
        $this->assertTrue($this->gists->checkGistIsStarred($gistId));
186
    }
187
188
    /**
189
     * Test unstar a gist
190
     *
191
     * @depends testCreateGist
192
     *
193
     * @param string $gistId
194
     */
195
    public function testUnStarGist(string $gistId)
196
    {
197
        $this->assertTrue($this->gists->unStarGist($gistId));
198
    }
199
200
    /**
201
     * Test fork a public gist
202
     */
203
    public function testForkGist()
204
    {
205
        $gist = $this->gists->forkGist(self::PUBLIC_GIST);
206
207
        $this->assertArrayHasKey('forks_url', $gist);
208
209
        return $gist['id'];
210
    }
211
212
    /**
213
     * Test list forks of a specific gist
214
     */
215
    public function testListGistForks()
216
    {
217
        $gists = $this->gists->listGistForks(self::PUBLIC_GIST);
218
        $gist  = array_pop($gists);
219
220
        $this->assertArrayHasKey('url', $gist);
221
        $this->assertArrayHasKey('id', $gist);
222
    }
223
224
    /**
225
     * Test deleting an existing gist
226
     *
227
     * @depends testCreateGist
228
     *
229
     * @param string $gistId
230
     */
231
    public function testDeleteGist(string $gistId)
232
    {
233
        $this->assertTrue($this->gists->deleteGist($gistId));
234
    }
235
236
    /**
237
     * Test deleting a forked gist
238
     *
239
     * @depends testForkGist
240
     *
241
     * @param string $gistId
242
     */
243
    public function testDeleteForkedGist(string $gistId)
244
    {
245
        $this->assertTrue($this->gists->deleteGist($gistId));
246
    }
247
}