VCardTest::testAddLogoWithJpgImageNoInclude()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 6.

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.

Loading history...
2
3
namespace JeroenDesloovere\VCard\tests;
4
5
// required to load
6
require_once __DIR__ . '/../vendor/autoload.php';
7
8
/*
9
 * This file is part of the VCard PHP Class from Jeroen Desloovere.
10
 *
11
 * For the full copyright and license information, please view the license
12
 * file that was distributed with this source code.
13
 */
14
15
use JeroenDesloovere\VCard\VCard;
16
17
/**
18
 * This class will test our VCard PHP Class which can generate VCards.
19
 */
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';
0 ignored issues
show
Bug Best Practice introduced by
The property firstName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55
        $this->lastName = 'Desloovere';
0 ignored issues
show
Bug Best Practice introduced by
The property lastName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
56
        $this->additional = '&';
0 ignored issues
show
Bug Best Practice introduced by
The property additional does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57
        $this->prefix = 'Mister';
0 ignored issues
show
Bug Best Practice introduced by
The property prefix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
58
        $this->suffix = 'Junior';
0 ignored issues
show
Bug Best Practice introduced by
The property suffix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
        
60
        $this->emailAddress1 = '';
0 ignored issues
show
Bug Best Practice introduced by
The property emailAddress1 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
61
        $this->emailAddress2 = '';
0 ignored issues
show
Bug Best Practice introduced by
The property emailAddress2 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
63
        $this->firstName2 = 'Ali';
0 ignored issues
show
Bug Best Practice introduced by
The property firstName2 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
        $this->lastName2 = 'ÖZSÜT';
0 ignored issues
show
Bug Best Practice introduced by
The property lastName2 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
65
66
        $this->firstName3 = 'Garçon';
0 ignored issues
show
Bug Best Practice introduced by
The property firstName3 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
        $this->lastName3 = 'Jéroèn';
0 ignored issues
show
Bug Best Practice introduced by
The property lastName3 does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
68
    }
69
70
    /**
71
     * Tear down after class
72
     */
73
    public function tearDown()
74
    {
75
        $this->vcard = null;
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()
355
    {
356
        $this->assertEquals($this->vcard, $this->vcard->addCategories(['1']));
357
        $this->assertEquals($this->vcard, $this->vcard->addCategories(['2']));
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()
377
    {
378
        $this->assertEquals($this->vcard, $this->vcard->addJobtitle('1'));
379
        $this->assertEquals($this->vcard, $this->vcard->addJobtitle('2'));
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()
454
    {
455
        $class_handler  = new \ReflectionClass('JeroenDesloovere\VCard\VCard');
456
        $method_handler = $class_handler->getMethod('chunk_split_unicode');
457
        $method_handler->setAccessible(true);
458
459
        $ascii_input="Lorem ipsum dolor sit amet,";
460
        $ascii_output = $method_handler->invokeArgs(new VCard(), [$ascii_input,10,'|']);
461
        $unicode_input='Τη γλώσσα μου έδωσαν ελληνική το σπίτι φτωχικό στις αμμουδιές του Ομήρου.';
462
        $unicode_output = $method_handler->invokeArgs(new VCard(), [$unicode_input,10,'|']);
463
464
        $this->assertEquals(
465
            "Lorem ipsu|m dolor si|t amet,|",
466
            $ascii_output);
467
        $this->assertEquals(
468
            "Τη γλώσσα |μου έδωσαν| ελληνική |το σπίτι φ|τωχικό στι|ς αμμουδιέ|ς του Ομήρ|ου.|",
469
            $unicode_output);
470
    }
471
}
472