Total Complexity | 46 |
Total Lines | 450 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like VCardTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use VCardTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class VCardTest extends \PHPUnit_Framework_TestCase |
||
21 | { |
||
22 | /** |
||
23 | * @var VCard |
||
24 | */ |
||
25 | protected $vcard = null; |
||
26 | |||
27 | /** |
||
28 | * Data provider for testEmail() |
||
29 | * |
||
30 | * @return array |
||
31 | */ |
||
32 | public function emailDataProvider() |
||
33 | { |
||
34 | return [ |
||
35 | [['[email protected]']], |
||
36 | [['[email protected]', 'WORK' => '[email protected]']], |
||
37 | [['WORK' => '[email protected]', 'HOME' => '[email protected]']], |
||
38 | [['PREF;WORK' => '[email protected]', 'HOME' => '[email protected]']], |
||
39 | ]; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Set up before class |
||
44 | * |
||
45 | * @return void |
||
46 | */ |
||
47 | public function setUp() |
||
48 | { |
||
49 | // set timezone |
||
50 | date_default_timezone_set('Europe/Brussels'); |
||
51 | |||
52 | $this->vcard = new VCard(); |
||
53 | |||
54 | $this->firstName = 'Jeroen'; |
||
55 | $this->lastName = 'Desloovere'; |
||
56 | $this->additional = '&'; |
||
57 | $this->prefix = 'Mister'; |
||
58 | $this->suffix = 'Junior'; |
||
59 | |||
60 | $this->emailAddress1 = ''; |
||
61 | $this->emailAddress2 = ''; |
||
62 | |||
63 | $this->firstName2 = 'Ali'; |
||
64 | $this->lastName2 = 'ÖZSÜT'; |
||
65 | |||
66 | $this->firstName3 = 'Garçon'; |
||
67 | $this->lastName3 = 'Jéroèn'; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Tear down after class |
||
72 | */ |
||
73 | public function tearDown() |
||
76 | } |
||
77 | |||
78 | public function testAddAddress() |
||
79 | { |
||
80 | $this->assertEquals($this->vcard, $this->vcard->addAddress( |
||
81 | '', |
||
82 | '88th Floor', |
||
83 | '555 East Flours Street', |
||
84 | 'Los Angeles', |
||
85 | 'CA', |
||
86 | '55555', |
||
87 | 'USA' |
||
88 | )); |
||
89 | $this->assertContains('ADR;WORK;POSTAL;CHARSET=utf-8:;88th Floor;555 East Flours Street;Los Angele', $this->vcard->getOutput()); |
||
90 | // Should fold on row 75, so we should not see the full address. |
||
91 | $this->assertNotContains('ADR;WORK;POSTAL;CHARSET=utf-8:;88th Floor;555 East Flours Street;Los Angeles;CA;55555;', $this->vcard->getOutput()); |
||
92 | } |
||
93 | |||
94 | public function testAddBirthday() |
||
95 | { |
||
96 | $this->assertEquals($this->vcard, $this->vcard->addBirthday('')); |
||
97 | } |
||
98 | |||
99 | public function testAddCompany() |
||
100 | { |
||
101 | $this->assertEquals($this->vcard, $this->vcard->addCompany('')); |
||
102 | } |
||
103 | |||
104 | public function testAddCategories() |
||
105 | { |
||
106 | $this->assertEquals($this->vcard, $this->vcard->addCategories([])); |
||
107 | } |
||
108 | |||
109 | public function testAddEmail() |
||
110 | { |
||
111 | $this->assertEquals($this->vcard, $this->vcard->addEmail($this->emailAddress1)); |
||
112 | $this->assertEquals($this->vcard, $this->vcard->addEmail($this->emailAddress2)); |
||
113 | $this->assertEquals(2, count($this->vcard->getProperties())); |
||
114 | } |
||
115 | |||
116 | public function testAddJobTitle() |
||
117 | { |
||
118 | $this->assertEquals($this->vcard, $this->vcard->addJobtitle('')); |
||
119 | } |
||
120 | |||
121 | public function testAddRole() |
||
122 | { |
||
123 | $this->assertEquals($this->vcard, $this->vcard->addRole('')); |
||
124 | } |
||
125 | |||
126 | public function testAddName() |
||
127 | { |
||
128 | $this->assertEquals($this->vcard, $this->vcard->addName('')); |
||
129 | } |
||
130 | |||
131 | public function testAddNote() |
||
132 | { |
||
133 | $this->assertEquals($this->vcard, $this->vcard->addNote('')); |
||
134 | } |
||
135 | |||
136 | public function testAddPhoneNumber() |
||
137 | { |
||
138 | $this->assertEquals($this->vcard, $this->vcard->addPhoneNumber('')); |
||
139 | $this->assertEquals($this->vcard, $this->vcard->addPhoneNumber('')); |
||
140 | $this->assertEquals(2, count($this->vcard->getProperties())); |
||
141 | } |
||
142 | |||
143 | public function testAddPhotoWithJpgPhoto() |
||
144 | { |
||
145 | $return = $this->vcard->addPhoto(__DIR__ . '/image.jpg', true); |
||
146 | |||
147 | $this->assertEquals($this->vcard, $return); |
||
148 | } |
||
149 | |||
150 | public function testAddPhotoWithRemoteJpgPhoto() |
||
151 | { |
||
152 | $return = $this->vcard->addPhoto( |
||
153 | 'https://raw.githubusercontent.com/jeroendesloovere/vcard/master/tests/image.jpg', |
||
154 | true |
||
155 | ); |
||
156 | |||
157 | $this->assertEquals($this->vcard, $return); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Test adding remote empty photo |
||
162 | * |
||
163 | * @expectedException Exception |
||
164 | * @expectedExceptionMessage Returned data is not an image. |
||
165 | */ |
||
166 | public function testAddPhotoWithRemoteEmptyJpgPhoto() |
||
167 | { |
||
168 | $this->vcard->addPhoto( |
||
169 | 'https://raw.githubusercontent.com/jeroendesloovere/vcard/master/tests/empty.jpg', |
||
170 | true |
||
171 | ); |
||
172 | } |
||
173 | |||
174 | public function testAddPhotoContentWithJpgPhoto() |
||
175 | { |
||
176 | $return = $this->vcard->addPhotoContent(file_get_contents(__DIR__ . '/image.jpg')); |
||
177 | |||
178 | $this->assertEquals($this->vcard, $return); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Test adding empty photo |
||
183 | * |
||
184 | * @expectedException Exception |
||
185 | * @expectedExceptionMessage Returned data is not an image. |
||
186 | */ |
||
187 | public function testAddPhotoContentWithEmptyContent() |
||
188 | { |
||
189 | $this->vcard->addPhotoContent(''); |
||
190 | } |
||
191 | |||
192 | public function testAddLogoWithJpgImage() |
||
193 | { |
||
194 | $return = $this->vcard->addLogo(__DIR__ . '/image.jpg', true); |
||
195 | |||
196 | $this->assertEquals($this->vcard, $return); |
||
197 | } |
||
198 | |||
199 | public function testAddLogoWithJpgImageNoInclude() |
||
200 | { |
||
201 | $return = $this->vcard->addLogo(__DIR__ . '/image.jpg', false); |
||
202 | |||
203 | $this->assertEquals($this->vcard, $return); |
||
204 | } |
||
205 | |||
206 | public function testAddLogoContentWithJpgImage() |
||
207 | { |
||
208 | $return = $this->vcard->addLogoContent(file_get_contents(__DIR__ . '/image.jpg')); |
||
209 | |||
210 | $this->assertEquals($this->vcard, $return); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Test adding empty photo |
||
215 | * |
||
216 | * @expectedException Exception |
||
217 | * @expectedExceptionMessage Returned data is not an image. |
||
218 | */ |
||
219 | public function testAddLogoContentWithEmptyContent() |
||
220 | { |
||
221 | $this->vcard->addLogoContent(''); |
||
222 | } |
||
223 | |||
224 | public function testAddUrl() |
||
225 | { |
||
226 | $this->assertEquals($this->vcard, $this->vcard->addUrl('1')); |
||
227 | $this->assertEquals($this->vcard, $this->vcard->addUrl('2')); |
||
228 | $this->assertEquals(2, count($this->vcard->getProperties())); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Test adding local photo using an empty file |
||
233 | * |
||
234 | * @expectedException Exception |
||
235 | * @expectedExceptionMessage Returned data is not an image. |
||
236 | */ |
||
237 | public function testAddPhotoWithEmptyFile() |
||
238 | { |
||
239 | $this->vcard->addPhoto(__DIR__ . '/emptyfile', true); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Test adding logo with no value |
||
244 | * |
||
245 | * @expectedException Exception |
||
246 | * @expectedExceptionMessage Returned data is not an image. |
||
247 | */ |
||
248 | public function testAddLogoWithNoValue() |
||
249 | { |
||
250 | $this->vcard->addLogo(__DIR__ . '/emptyfile', true); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Test adding photo with no photo |
||
255 | * |
||
256 | * @expectedException Exception |
||
257 | * @expectedExceptionMessage Returned data is not an image. |
||
258 | */ |
||
259 | public function testAddPhotoWithNoPhoto() |
||
260 | { |
||
261 | $this->vcard->addPhoto(__DIR__ . '/wrongfile', true); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Test adding logo with no image |
||
266 | * |
||
267 | * @expectedException Exception |
||
268 | * @expectedExceptionMessage Returned data is not an image. |
||
269 | */ |
||
270 | public function testAddLogoWithNoImage() |
||
271 | { |
||
272 | $this->vcard->addLogo(__DIR__ . '/wrongfile', true); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Test charset |
||
277 | */ |
||
278 | public function testCharset() |
||
279 | { |
||
280 | $charset = 'ISO-8859-1'; |
||
281 | $this->vcard->setCharset($charset); |
||
282 | $this->assertEquals($charset, $this->vcard->getCharset()); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Test Email |
||
287 | * |
||
288 | * @dataProvider emailDataProvider $emails |
||
289 | */ |
||
290 | public function testEmail($emails = []) |
||
291 | { |
||
292 | foreach ($emails as $key => $email) { |
||
293 | if (is_string($key)) { |
||
294 | $this->vcard->addEmail($email, $key); |
||
295 | } else { |
||
296 | $this->vcard->addEmail($email); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | foreach ($emails as $key => $email) { |
||
301 | if (is_string($key)) { |
||
302 | $this->assertContains('EMAIL;INTERNET;' . $key . ':' . $email, $this->vcard->getOutput()); |
||
303 | } else { |
||
304 | $this->assertContains('EMAIL;INTERNET:' . $email, $this->vcard->getOutput()); |
||
305 | } |
||
306 | } |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Test first name and last name |
||
311 | */ |
||
312 | public function testFirstNameAndLastName() |
||
313 | { |
||
314 | $this->vcard->addName( |
||
315 | $this->lastName, |
||
316 | $this->firstName |
||
317 | ); |
||
318 | |||
319 | $this->assertEquals('jeroen-desloovere', $this->vcard->getFilename()); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Test full blown name |
||
324 | */ |
||
325 | public function testFullBlownName() |
||
326 | { |
||
327 | $this->vcard->addName( |
||
328 | $this->lastName, |
||
329 | $this->firstName, |
||
330 | $this->additional, |
||
331 | $this->prefix, |
||
332 | $this->suffix |
||
333 | ); |
||
334 | |||
335 | $this->assertEquals('mister-jeroen-desloovere-junior', $this->vcard->getFilename()); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Test multiple birthdays |
||
340 | * |
||
341 | * @expectedException Exception |
||
342 | */ |
||
343 | public function testMultipleBirthdays() |
||
344 | { |
||
345 | $this->assertEquals($this->vcard, $this->vcard->addBirthday('1')); |
||
346 | $this->assertEquals($this->vcard, $this->vcard->addBirthday('2')); |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Test multiple categories |
||
351 | * |
||
352 | * @expectedException Exception |
||
353 | */ |
||
354 | public function testMultipleCategories() |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Test multiple companies |
||
362 | * |
||
363 | * @expectedException Exception |
||
364 | */ |
||
365 | public function testMultipleCompanies() |
||
366 | { |
||
367 | $this->assertEquals($this->vcard, $this->vcard->addCompany('1')); |
||
368 | $this->assertEquals($this->vcard, $this->vcard->addCompany('2')); |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Test multiple job titles |
||
373 | * |
||
374 | * @expectedException Exception |
||
375 | */ |
||
376 | public function testMultipleJobtitles() |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Test multiple roles |
||
384 | * |
||
385 | * @expectedException Exception |
||
386 | */ |
||
387 | public function testMultipleRoles() |
||
388 | { |
||
389 | $this->assertEquals($this->vcard, $this->vcard->addRole('1')); |
||
390 | $this->assertEquals($this->vcard, $this->vcard->addRole('2')); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Test multiple names |
||
395 | * |
||
396 | * @expectedException Exception |
||
397 | */ |
||
398 | public function testMultipleNames() |
||
399 | { |
||
400 | $this->assertEquals($this->vcard, $this->vcard->addName('1')); |
||
401 | $this->assertEquals($this->vcard, $this->vcard->addName('2')); |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Test multiple notes |
||
406 | * |
||
407 | * @expectedException Exception |
||
408 | */ |
||
409 | public function testMultipleNotes() |
||
410 | { |
||
411 | $this->assertEquals($this->vcard, $this->vcard->addNote('1')); |
||
412 | $this->assertEquals($this->vcard, $this->vcard->addNote('2')); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Test special first name and last name |
||
417 | */ |
||
418 | public function testSpecialFirstNameAndLastName() |
||
419 | { |
||
420 | $this->vcard->addName( |
||
421 | $this->lastName2, |
||
422 | $this->firstName2 |
||
423 | ); |
||
424 | |||
425 | $this->assertEquals('ali-ozsut', $this->vcard->getFilename()); |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Test special first name and last name |
||
430 | */ |
||
431 | public function testSpecialFirstNameAndLastName2() |
||
432 | { |
||
433 | $this->vcard->addName( |
||
434 | $this->lastName3, |
||
435 | $this->firstName3 |
||
436 | ); |
||
437 | |||
438 | $this->assertEquals('garcon-jeroen', $this->vcard->getFilename()); |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * Test multiple labels |
||
443 | */ |
||
444 | public function testMultipleLabels() |
||
445 | { |
||
446 | $this->assertSame($this->vcard, $this->vcard->addLabel('My label')); |
||
447 | $this->assertSame($this->vcard, $this->vcard->addLabel('My work label', 'WORK')); |
||
448 | $this->assertSame(2, count($this->vcard->getProperties())); |
||
449 | $this->assertContains('LABEL:My label', $this->vcard->getOutput()); |
||
450 | $this->assertContains('LABEL;WORK:My work label', $this->vcard->getOutput()); |
||
451 | } |
||
452 | |||
453 | public function testChunkSplitUnicode() |
||
470 | } |
||
471 | } |
||
472 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.