Completed
Push — master ( c52a8c...2ff9bf )
by Jeroen
04:43 queued 02:23
created

VCardTest::testMultipleLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
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
83
    public function testAddBirthday()
84
    {
85
        $this->assertEquals($this->vcard, $this->vcard->addBirthday(''));
86
    }
87
88
    public function testAddCompany()
89
    {
90
        $this->assertEquals($this->vcard, $this->vcard->addCompany(''));
91
    }
92
93
    public function testAddCategories()
94
    {
95
        $this->assertEquals($this->vcard, $this->vcard->addCategories([]));
96
    }
97
98
    public function testAddEmail()
99
    {
100
        $this->assertEquals($this->vcard, $this->vcard->addEmail($this->emailAddress1));
101
        $this->assertEquals($this->vcard, $this->vcard->addEmail($this->emailAddress2));
102
        $this->assertEquals(2, count($this->vcard->getProperties()));
103
    }
104
105
    public function testAddJobTitle()
106
    {
107
        $this->assertEquals($this->vcard, $this->vcard->addJobtitle(''));
108
    }
109
110
    public function testAddRole()
111
    {
112
        $this->assertEquals($this->vcard, $this->vcard->addRole(''));
113
    }
114
115
    public function testAddName()
116
    {
117
        $this->assertEquals($this->vcard, $this->vcard->addName(''));
118
    }
119
120
    public function testAddNote()
121
    {
122
        $this->assertEquals($this->vcard, $this->vcard->addNote(''));
123
    }
124
125
    public function testAddPhoneNumber()
126
    {
127
        $this->assertEquals($this->vcard, $this->vcard->addPhoneNumber(''));
128
        $this->assertEquals($this->vcard, $this->vcard->addPhoneNumber(''));
129
        $this->assertEquals(2, count($this->vcard->getProperties()));
130
    }
131
132
    public function testAddPhotoWithJpgPhoto()
133
    {
134
        $return = $this->vcard->addPhoto(__DIR__ . '/image.jpg', true);
135
136
        $this->assertEquals($this->vcard, $return);
137
    }
138
139
    public function testAddPhotoWithRemoteJpgPhoto()
140
    {
141
        $return = $this->vcard->addPhoto(
142
            'https://raw.githubusercontent.com/jeroendesloovere/vcard/master/tests/image.jpg',
143
            true
144
        );
145
146
        $this->assertEquals($this->vcard, $return);
147
    }
148
149
    /**
150
     * Test adding remote empty photo
151
     *
152
     * @expectedException Exception
153
     * @expectedExceptionMessage Returned data is not an image.
154
     */
155
    public function testAddPhotoWithRemoteEmptyJpgPhoto()
156
    {
157
        $this->vcard->addPhoto(
158
            'https://raw.githubusercontent.com/jeroendesloovere/vcard/master/tests/empty.jpg',
159
            true
160
        );
161
    }
162
163
    public function testAddPhotoContentWithJpgPhoto()
164
    {
165
        $return = $this->vcard->addPhotoContent(file_get_contents(__DIR__ . '/image.jpg'));
166
167
        $this->assertEquals($this->vcard, $return);
168
    }
169
170
    /**
171
     * Test adding empty photo
172
     *
173
     * @expectedException Exception
174
     * @expectedExceptionMessage Returned data is not an image.
175
     */
176
    public function testAddPhotoContentWithEmptyContent()
177
    {
178
        $this->vcard->addPhotoContent('');
179
    }
180
181
    public function testAddLogoWithJpgImage()
182
    {
183
        $return = $this->vcard->addLogo(__DIR__ . '/image.jpg', true);
184
185
        $this->assertEquals($this->vcard, $return);
186
    }
187
188
    public function testAddLogoWithJpgImageNoInclude()
189
    {
190
        $return = $this->vcard->addLogo(__DIR__ . '/image.jpg', false);
191
192
        $this->assertEquals($this->vcard, $return);
193
    }
194
195
    public function testAddLogoContentWithJpgImage()
196
    {
197
        $return = $this->vcard->addLogoContent(file_get_contents(__DIR__ . '/image.jpg'));
198
199
        $this->assertEquals($this->vcard, $return);
200
    }
201
202
    /**
203
     * Test adding empty photo
204
     *
205
     * @expectedException Exception
206
     * @expectedExceptionMessage Returned data is not an image.
207
     */
208
    public function testAddLogoContentWithEmptyContent()
209
    {
210
        $this->vcard->addLogoContent('');
211
    }
212
213
    public function testAddUrl()
214
    {
215
        $this->assertEquals($this->vcard, $this->vcard->addUrl('1'));
216
        $this->assertEquals($this->vcard, $this->vcard->addUrl('2'));
217
        $this->assertEquals(2, count($this->vcard->getProperties()));
218
    }
219
220
    /**
221
     * Test adding local photo using an empty file
222
     *
223
     * @expectedException Exception
224
     * @expectedExceptionMessage Returned data is not an image.
225
     */
226
    public function testAddPhotoWithEmptyFile()
227
    {
228
        $this->vcard->addPhoto(__DIR__ . '/emptyfile', true);
229
    }
230
231
    /**
232
     * Test adding logo with no value
233
     *
234
     * @expectedException Exception
235
     * @expectedExceptionMessage Returned data is not an image.
236
     */
237
    public function testAddLogoWithNoValue()
238
    {
239
        $this->vcard->addLogo(__DIR__ . '/emptyfile', true);
240
    }
241
242
    /**
243
     * Test adding photo with no photo
244
     *
245
     * @expectedException Exception
246
     * @expectedExceptionMessage Returned data is not an image.
247
     */
248
    public function testAddPhotoWithNoPhoto()
249
    {
250
        $this->vcard->addPhoto(__DIR__ . '/wrongfile', true);
251
    }
252
253
    /**
254
     * Test adding logo with no image
255
     *
256
     * @expectedException Exception
257
     * @expectedExceptionMessage Returned data is not an image.
258
     */
259
    public function testAddLogoWithNoImage()
260
    {
261
        $this->vcard->addLogo(__DIR__ . '/wrongfile', true);
262
    }
263
264
    /**
265
     * Test charset
266
     */
267
    public function testCharset()
268
    {
269
        $charset = 'ISO-8859-1';
270
        $this->vcard->setCharset($charset);
271
        $this->assertEquals($charset, $this->vcard->getCharset());
272
    }
273
274
    /**
275
     * Test Email
276
     *
277
     * @dataProvider emailDataProvider $emails
278
     */
279
    public function testEmail($emails = [])
280
    {
281
        foreach ($emails as $key => $email) {
282
            if (is_string($key)) {
283
                $this->vcard->addEmail($email, $key);
284
            } else {
285
                $this->vcard->addEmail($email);
286
            }
287
        }
288
289
        foreach ($emails as $key => $email) {
290
            if (is_string($key)) {
291
                $this->assertContains('EMAIL;INTERNET;' . $key . ':' . $email, $this->vcard->getOutput());
292
            } else {
293
                $this->assertContains('EMAIL;INTERNET:' . $email, $this->vcard->getOutput());
294
            }
295
        }
296
    }
297
298
    /**
299
     * Test first name and last name
300
     */
301
    public function testFirstNameAndLastName()
302
    {
303
        $this->vcard->addName(
304
            $this->lastName,
305
            $this->firstName
306
        );
307
308
        $this->assertEquals('jeroen-desloovere', $this->vcard->getFilename());
309
    }
310
311
    /**
312
     * Test full blown name
313
     */
314
    public function testFullBlownName()
315
    {
316
        $this->vcard->addName(
317
            $this->lastName,
318
            $this->firstName,
319
            $this->additional,
320
            $this->prefix,
321
            $this->suffix
322
        );
323
324
        $this->assertEquals('mister-jeroen-desloovere-junior', $this->vcard->getFilename());
325
    }
326
327
    /**
328
     * Test multiple birthdays
329
     *
330
     * @expectedException Exception
331
     */
332
    public function testMultipleBirthdays()
333
    {
334
        $this->assertEquals($this->vcard, $this->vcard->addBirthday('1'));
335
        $this->assertEquals($this->vcard, $this->vcard->addBirthday('2'));
336
    }
337
338
    /**
339
     * Test multiple categories
340
     *
341
     * @expectedException Exception
342
     */
343
    public function testMultipleCategories()
344
    {
345
        $this->assertEquals($this->vcard, $this->vcard->addCategories(['1']));
346
        $this->assertEquals($this->vcard, $this->vcard->addCategories(['2']));
347
    }
348
349
    /**
350
     * Test multiple companies
351
     *
352
     * @expectedException Exception
353
     */
354
    public function testMultipleCompanies()
355
    {
356
        $this->assertEquals($this->vcard, $this->vcard->addCompany('1'));
357
        $this->assertEquals($this->vcard, $this->vcard->addCompany('2'));
358
    }
359
360
    /**
361
     * Test multiple job titles
362
     *
363
     * @expectedException Exception
364
     */
365
    public function testMultipleJobtitles()
366
    {
367
        $this->assertEquals($this->vcard, $this->vcard->addJobtitle('1'));
368
        $this->assertEquals($this->vcard, $this->vcard->addJobtitle('2'));
369
    }
370
371
    /**
372
     * Test multiple roles
373
     *
374
     * @expectedException Exception
375
     */
376
    public function testMultipleRoles()
377
    {
378
        $this->assertEquals($this->vcard, $this->vcard->addRole('1'));
379
        $this->assertEquals($this->vcard, $this->vcard->addRole('2'));
380
    }
381
382
    /**
383
     * Test multiple names
384
     *
385
     * @expectedException Exception
386
     */
387
    public function testMultipleNames()
388
    {
389
        $this->assertEquals($this->vcard, $this->vcard->addName('1'));
390
        $this->assertEquals($this->vcard, $this->vcard->addName('2'));
391
    }
392
393
    /**
394
     * Test multiple notes
395
     *
396
     * @expectedException Exception
397
     */
398
    public function testMultipleNotes()
399
    {
400
        $this->assertEquals($this->vcard, $this->vcard->addNote('1'));
401
        $this->assertEquals($this->vcard, $this->vcard->addNote('2'));
402
    }
403
404
    /**
405
     * Test special first name and last name
406
     */
407
    public function testSpecialFirstNameAndLastName()
408
    {
409
        $this->vcard->addName(
410
            $this->lastName2,
411
            $this->firstName2
412
        );
413
414
        $this->assertEquals('ali-ozsut', $this->vcard->getFilename());
415
    }
416
417
    /**
418
     * Test special first name and last name
419
     */
420
    public function testSpecialFirstNameAndLastName2()
421
    {
422
        $this->vcard->addName(
423
            $this->lastName3,
424
            $this->firstName3
425
        );
426
427
        $this->assertEquals('garcon-jeroen', $this->vcard->getFilename());
428
    }
429
430
    /**
431
     * Test multiple labels
432
     */
433
    public function testMultipleLabels()
434
    {
435
        $this->assertSame($this->vcard, $this->vcard->addLabel('My label'));
436
        $this->assertSame($this->vcard, $this->vcard->addLabel('My work label', 'WORK'));
437
        $this->assertSame(2, count($this->vcard->getProperties()));
438
        $this->assertContains('LABEL:My label', $this->vcard->getOutput());
439
        $this->assertContains('LABEL;WORK:My work label', $this->vcard->getOutput());
440
    }
441
442
    public function testChunkSplitUnicode()
443
    {
444
        $class_handler  = new \ReflectionClass('JeroenDesloovere\VCard\VCard');
445
        $method_handler = $class_handler->getMethod('chunk_split_unicode');
446
        $method_handler->setAccessible(true);
447
448
        $ascii_input="Lorem ipsum dolor sit amet,";
449
        $ascii_output = $method_handler->invokeArgs(new VCard(), [$ascii_input,10,'|']);
450
        $unicode_input='Τη γλώσσα μου έδωσαν ελληνική το σπίτι φτωχικό στις αμμουδιές του Ομήρου.';
451
        $unicode_output = $method_handler->invokeArgs(new VCard(), [$unicode_input,10,'|']);
452
453
        $this->assertEquals(
454
            "Lorem ipsu|m dolor si|t amet,|",
455
            $ascii_output);
456
        $this->assertEquals(
457
            "Τη γλώσσα |μου έδωσαν| ελληνική |το σπίτι φ|τωχικό στι|ς αμμουδιέ|ς του Ομήρ|ου.|",
458
            $unicode_output);
459
    }
460
}
461