Completed
Pull Request — master (#40)
by Marko
116:46 queued 51:48
created

CKEditorInstallerTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 224
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 2
dl 32
loc 224
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 6 2
A testInstall() 0 6 1
A testInstallWithPath() 0 7 1
A testInstallWithRelease() 0 6 1
A testInstallWithVersion() 0 6 1
A testInstallWithExcludes() 0 6 1
A testInstallWithHttpProxy() 0 8 1
A testInstallWithHttpsProxy() 0 8 1
A testInstallWithHttpProxyRequestFullUri() 12 12 1
A testInstallWithHttpsProxyRequestFullUri() 12 12 1
A testReinstall() 0 7 1
A testReinstallWithClearDrop() 0 10 1
A testReinstallWithClearKeep() 0 11 1
A testReinstallWithClearSkip() 0 7 1
A assertInstall() 0 18 4
A assertRelease() 8 18 4
A assertVersion() 0 8 1
A assertExcludes() 0 6 2

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
3
/*
4
 * This file is part of the Ivory CKEditor package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\CKEditorBundle\Tests\Installer;
13
14
use FOS\CKEditorBundle\Installer\CKEditorInstaller;
15
use FOS\CKEditorBundle\Tests\AbstractTestCase;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class CKEditorInstallerTest extends AbstractTestCase
21
{
22
    /**
23
     * @var CKEditorInstaller
24
     */
25
    private $installer;
26
27
    /**
28
     * @var string
29
     */
30
    private $path;
31
32
    /**
33
     * @var string
34
     */
35
    private $proxy;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function setUp()
41
    {
42
        $this->installer = new CKEditorInstaller();
43
        $this->path = __DIR__.'/../../src/Resources/public';
44
        $this->proxy = 'http://178.32.218.91:80';
45
46
        $this->tearDown();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function tearDown()
53
    {
54
        if (file_exists($this->path)) {
55
            exec('rm -rf '.$this->path);
56
        }
57
    }
58
59
    public function testInstall()
60
    {
61
        $this->installer->install();
62
63
        $this->assertInstall();
64
    }
65
66
    public function testInstallWithPath()
67
    {
68
        $this->path = sys_get_temp_dir().'/ivory-ckeditor-installer-test';
69
        $this->installer->install(['path' => $this->path]);
70
71
        $this->assertInstall();
72
    }
73
74
    public function testInstallWithRelease()
75
    {
76
        $this->installer->install($options = ['release' => CKEditorInstaller::RELEASE_BASIC]);
77
78
        $this->assertInstall($options);
79
    }
80
81
    public function testInstallWithVersion()
82
    {
83
        $this->installer->install($options = ['version' => '4.6.0']);
84
85
        $this->assertInstall($options);
86
    }
87
88
    public function testInstallWithExcludes()
89
    {
90
        $this->installer->install($options = ['excludes' => ['adapters', 'samples']]);
91
92
        $this->assertInstall($options);
93
    }
94
95
    public function testInstallWithHttpProxy()
96
    {
97
        putenv('http_proxy='.$this->proxy);
98
        $this->installer->install();
99
        putenv('http_proxy');
100
101
        $this->assertInstall();
102
    }
103
104
    public function testInstallWithHttpsProxy()
105
    {
106
        putenv('https_proxy='.$this->proxy);
107
        $this->installer->install();
108
        putenv('https_proxy');
109
110
        $this->assertInstall();
111
    }
112
113 View Code Duplication
    public function testInstallWithHttpProxyRequestFullUri()
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...
114
    {
115
        putenv('http_proxy='.$this->proxy);
116
        putenv('http_proxy_request_fulluri=true');
117
118
        $this->installer->install();
119
120
        putenv('http_proxy');
121
        putenv('http_proxy_request_fulluri');
122
123
        $this->assertInstall();
124
    }
125
126 View Code Duplication
    public function testInstallWithHttpsProxyRequestFullUri()
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...
127
    {
128
        putenv('https_proxy='.$this->proxy);
129
        putenv('https_proxy_request_fulluri=true');
130
131
        $this->installer->install();
132
133
        putenv('https_proxy');
134
        putenv('https_proxy_request_fulluri');
135
136
        $this->assertInstall();
137
    }
138
139
    public function testReinstall()
140
    {
141
        $this->installer->install();
142
        $this->installer->install();
143
144
        $this->assertInstall();
145
    }
146
147
    public function testReinstallWithClearDrop()
148
    {
149
        $this->installer->install();
150
        $this->installer->install($options = [
151
            'release' => CKEditorInstaller::RELEASE_BASIC,
152
            'clear' => CKEditorInstaller::CLEAR_DROP,
153
        ]);
154
155
        $this->assertInstall($options);
156
    }
157
158
    public function testReinstallWithClearKeep()
159
    {
160
        $this->installer->install(['release' => CKEditorInstaller::RELEASE_BASIC]);
161
        $this->installer->install($options = [
162
            'version' => '4.6.0',
163
            'release' => CKEditorInstaller::RELEASE_FULL,
164
            'clear' => CKEditorInstaller::CLEAR_KEEP,
165
        ]);
166
167
        $this->assertInstall($options);
168
    }
169
170
    public function testReinstallWithClearSkip()
171
    {
172
        $this->installer->install($options = ['version' => '4.6.0']);
173
        $this->installer->install(['clear' => CKEditorInstaller::CLEAR_SKIP]);
174
175
        $this->assertInstall($options);
176
    }
177
178
    /**
179
     * @param mixed[] $options
180
     */
181
    private function assertInstall(array $options = [])
182
    {
183
        $this->assertFileExists($this->path.'/ckeditor.js');
184
185
        if (isset($options['release'])) {
186
            $this->assertRelease($options['release']);
187
        }
188
189
        if (isset($options['version'])) {
190
            $this->assertVersion($options['version']);
191
        }
192
193
        if (!isset($options['excludes'])) {
194
            $options['excludes'] = ['samples'];
195
        }
196
197
        $this->assertExcludes($options['excludes']);
198
    }
199
200
    /**
201
     * @param string $release
202
     */
203
    private function assertRelease($release)
204
    {
205
        switch ($release) {
206
            case CKEditorInstaller::RELEASE_FULL:
207
                $this->assertFileExists($this->path.'/plugins/copyformatting');
208
                break;
209
210 View Code Duplication
            case CKEditorInstaller::RELEASE_BASIC:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
211
                $this->assertFileExists($this->path.'/plugins/link');
212
                $this->assertFileNotExists($this->path.'/plugins/image');
213
                break;
214
215 View Code Duplication
            case CKEditorInstaller::RELEASE_STANDARD:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
216
                $this->assertFileExists($this->path.'/plugins/image');
217
                $this->assertFileNotExists($this->path.'/plugins/copyformatting');
218
                break;
219
        }
220
    }
221
222
    /**
223
     * @param string $version
224
     */
225
    private function assertVersion($version)
226
    {
227
        $package = json_decode(file_get_contents($this->path.'/package.json'), true);
228
229
        $this->assertInternalType('array', $package);
230
        $this->assertArrayHasKey('version', $package);
231
        $this->assertSame($version, $package['version']);
232
    }
233
234
    /**
235
     * @param string[] $excludes
236
     */
237
    private function assertExcludes(array $excludes)
238
    {
239
        foreach ($excludes as $exclude) {
240
            $this->assertFileNotExists($this->path.'/'.$exclude);
241
        }
242
    }
243
}
244