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 |
||
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 | public function testInstallWithHttpProxyRequestFullUri() |
||
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 | public function testInstallWithHttpsProxyRequestFullUri() |
||
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 | case CKEditorInstaller::RELEASE_BASIC: |
||
211 | $this->assertFileExists($this->path.'/plugins/link'); |
||
212 | $this->assertFileNotExists($this->path.'/plugins/image'); |
||
213 | break; |
||
214 | |||
215 | case CKEditorInstaller::RELEASE_STANDARD: |
||
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 |