forked from jeroendesloovere/vcard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVCardTest.php
399 lines (340 loc) · 13.9 KB
/
VCardTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
declare(strict_types=1);
namespace JeroenDesloovere\Tests\VCard;
use JeroenDesloovere\VCard\Formatter\Formatter;
use JeroenDesloovere\VCard\Formatter\VcfFormatter;
use JeroenDesloovere\VCard\Parser\Parser;
use JeroenDesloovere\VCard\Parser\VcfParser;
use JeroenDesloovere\VCard\Property\Address;
use JeroenDesloovere\VCard\Property\Anniversary;
use JeroenDesloovere\VCard\Property\Birthdate;
use JeroenDesloovere\VCard\Property\Email;
use JeroenDesloovere\VCard\Property\Gender;
use JeroenDesloovere\VCard\Property\Logo;
use JeroenDesloovere\VCard\Property\Name;
use JeroenDesloovere\VCard\Property\Nickname;
use JeroenDesloovere\VCard\Property\Note;
use JeroenDesloovere\VCard\Property\Parameter\Kind;
use JeroenDesloovere\VCard\Property\Parameter\Revision;
use JeroenDesloovere\VCard\Property\Parameter\Type;
use JeroenDesloovere\VCard\Property\Parameter\Version;
use JeroenDesloovere\VCard\Property\Photo;
use JeroenDesloovere\VCard\Property\Telephone;
use JeroenDesloovere\VCard\Property\Title;
use JeroenDesloovere\VCard\Property\Role;
use JeroenDesloovere\VCard\Property\Url;
use JeroenDesloovere\VCard\VCard;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\TestCase;
/**
* How to execute all tests: `vendor/bin/phpunit tests`
*/
final class VCardTest extends TestCase
{
/** @var VCard */
private $firstVCard;
/** @var VCard */
private $secondVCard;
/** @var VCard */
private $thirdVCard;
/** @var vfsStreamDirectory - We save the generated vCard to a virtual storage */
private $virtualStorage;
public function setUp(): void
{
$this->setUpFirstVCard();
$this->setUpSecondVCard();
$this->setUpThirdVCard();
$this->virtualStorage = vfsStream::setup();
}
private function setUpFirstVCard(): void
{
$this->firstVCard = (new VCard())
->add(Gender::male('Dude'))
->add(new Nickname('Web developer'))
->add(new Name('Desloovere', 'Jeroen'))
->add(new Address(null, null, 'Markt 1', 'Brugge', 'West-Vlaanderen', '8000', 'België', Type::work()))
->add(new Address(null, 'Penthouse', 'Korenmarkt 1', 'Gent', 'Oost-Vlaanderen', '9000', 'België', Type::home()))
->add(new Email('test@test.nl', Type::work()))
->add(new Email('example@example.nl', Type::home()))
->add(new Note('VCard library is amazing.'))
->add(new Birthdate(new \DateTime('2015-12-05')))
->add(new Anniversary(new \DateTime('2017-12-05')))
->add(new Telephone('+33 01 23 45 67'))
->add(new Telephone('+33-05-42-41-96', Type::work()))
->add(new Url('https://www.jeroendesloovere.be'))
;
}
private function setUpSecondVCard(): void
{
$this->secondVCard = (new VCard())
->add(new Name('Doe', 'John'))
->add(new Address(null, 'Penthouse', 'Korenmarkt 1', 'Gent', 'Oost-Vlaanderen', '9000', 'België', Type::work()));
}
private function setUpThirdVCard(): void
{
$this->thirdVCard = (new VCard(Kind::organization()))
->add(new Title('Apple'))
->add(new Role('Fruit'))
->add(new Photo(__DIR__ . '/assets/landscape.jpeg'))
->add(new Logo(__DIR__ . '/assets/landscape.jpeg'))
->add(new Telephone('+32 486 00 00 00'));
}
public function testFormatterSavingMultipleVCardsToVcfFile(): void
{
// Saving "vcards-export.vcf"
$formatter = new Formatter(new VcfFormatter(), 'vcards-export');
$formatter->addVCard($this->firstVCard);
$formatter->addVCard($this->secondVCard);
$this->assertFalse($this->virtualStorage->hasChild('vcards-export.vcf'));
$formatter->save($this->virtualStorage->url());
$this->assertTrue($this->virtualStorage->hasChild('vcards-export.vcf'));
}
public function testFormatterSavingOneVCardToVcfFile(): void
{
// Saving "vcard-export.vcf"
$formatter = new Formatter(new VcfFormatter(), 'vcard-export');
$formatter->addVCard($this->firstVCard);
$this->assertFalse($this->virtualStorage->hasChild('vcard-export.vcf'));
$formatter->save($this->virtualStorage->url());
$this->assertTrue($this->virtualStorage->hasChild('vcard-export.vcf'));
}
/**
* @expectedException \JeroenDesloovere\VCard\Exception\VCardException
*/
public function testMultipleNotAllowedProperties(): void
{
(new VCard())
->add(new Nickname('Jeroen'))
->add(new Nickname('Jeroen2'));
}
/**
* @expectedException \JeroenDesloovere\VCard\Exception\VCardException
*/
public function testMultipleNotAllowedPropertyParameters(): void
{
(new VCard())
->add(new Revision(new \DateTime))
->add(new Revision(new \DateTime));
}
/**
* @expectedException \JeroenDesloovere\VCard\Exception\ParserException
* @expectedExceptionMessage File "Lorem ipsum dolor sit amet, consectetur adipiscing elit." is not readable, or doesn't exist.
*/
public function testParserCorruptVCard(): void
{
new Parser(new VcfParser(), 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
}
/**
* @expectedException \JeroenDesloovere\VCard\Exception\ParserException
* @expectedExceptionMessage File "" is not readable, or doesn't exist.
*/
public function testParserEmptyVCard(): void
{
new Parser(new VcfParser(), '');
}
/**
* @expectedException \JeroenDesloovere\VCard\Exception\ParserException
*/
public function testParserGetFileContentsException(): void
{
Parser::getFileContents(__DIR__ . '/not-existing');
}
/**
* Test the Telephone parser independently.
*/
public function testTelephoneParser(): void
{
// Given
$vcard = (new Vcard())->add(new Telephone('+33-01-23-45-67'));
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nTEL;VALUE=uri;TYPE=home:tel:+33-01-23-45-67\r\nEND:VCARD";
// When
$parser = new Parser(new VcfParser(), $content);
// Then
$this->assertEquals($vcard->getProperties(Telephone::class), $parser->getVCards()[0]->getProperties(Telephone::class));
}
/**
* Test the Version parameter parser independently.
* With version 4 and version 3, should result in not equal (bad weather)
*/
public function testVersionParameterParserBadWeather(): void
{
// Given
// Version 4
$vcard = new Vcard(null, Version::version4());
// Version 3
$content = "BEGIN:VCARD\r\nVERSION:3.0\r\nEND:VCARD";
// When
$parser = new Parser(new VcfParser(), $content);
// Then
$this->assertNotEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}
/**
* Test the Version parameter parser independently.
* Both version 4 (good weather)
*/
public function testVersionParameterParserGoodWeather(): void
{
// Given
$vcard = new Vcard(null, Version::version4());
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nEND:VCARD";
// When
$parser = new Parser(new VcfParser(), $content);
// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}
/**
* Test the Version parameter parser independently.
* Without any version specified, should use the default Version value (4.0)
*/
public function testVersionParameterParserWithoutVersion(): void
{
// Given
$vcard = new Vcard();
$content = "BEGIN:VCARD\r\nEND:VCARD";
// When
$parser = new Parser(new VcfParser(), $content);
// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}
/**
* Test the Kind parameter parser independently.
* Test vcard with individual person (is default kind)
*/
public function testKindIndividual(): void
{
// Given
$vcard = new Vcard();
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nKIND:individual\r\nEND:VCARD";
// When
$parser = new Parser(new VcfParser(), $content);
// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}
/**
* Test the Kind parameter parser independently.
* Test vcard with department/organization
*/
public function testKindOrganization(): void
{
// Given
$vcard = new Vcard(Kind::organization());
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nKIND:org\r\nEND:VCARD";
// When
$parser = new Parser(new VcfParser(), $content);
// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}
/**
* Test the Kind parameter parser independently.
* Test vcard with group
*/
public function testKindGroup(): void
{
// Given
$vcard = new Vcard(Kind::group());
$content = "BEGIN:VCARD\r\nVERSION:4.0\r\nKIND:group\r\nEND:VCARD";
// When
$parser = new Parser(new VcfParser(), $content);
// Then
$this->assertEquals($vcard->getParameters(), $parser->getVCards()[0]->getParameters());
}
public function testParserMultipleVCardsFromVcfFile(): void
{
$parser = new Parser(new VcfParser(), Parser::getFileContents(__DIR__ . '/assets/vcards.vcf'));
$this->assertEquals($this->firstVCard->getProperties(), $parser->getVCards()[0]->getProperties());
$this->assertEquals($this->secondVCard->getProperties(), $parser->getVCards()[1]->getProperties());
}
public function testParserOneVCardFromVcfFile(): void
{
$parser = new Parser(new VcfParser(), Parser::getFileContents(__DIR__ . '/assets/vcard.vcf'));
$this->assertEquals($this->firstVCard->getProperties(), $parser->getVCards()[0]->getProperties());
}
/**
* Integration test:
* Validate the number of properties from the created vCards in the Setup.
*/
public function testVCardGetProperties(): void
{
$this->assertCount(13, $this->firstVCard->getProperties());
$this->assertCount(1, $this->firstVCard->getProperties(Gender::class));
$this->assertCount(1, $this->firstVCard->getProperties(Nickname::class));
$this->assertCount(1, $this->firstVCard->getProperties(Name::class));
$this->assertCount(2, $this->firstVCard->getProperties(Address::class));
$this->assertCount(2, $this->firstVCard->getProperties(Email::class));
$this->assertCount(1, $this->firstVCard->getProperties(Note::class));
$this->assertCount(1, $this->firstVCard->getProperties(Birthdate::class));
$this->assertCount(1, $this->firstVCard->getProperties(Anniversary::class));
$this->assertCount(2, $this->firstVCard->getProperties(Telephone::class));
$this->assertCount(1, $this->firstVCard->getProperties(Url::class));
$this->assertCount(2, $this->secondVCard->getProperties());
$this->assertCount(1, $this->secondVCard->getProperties(Name::class));
$this->assertCount(1, $this->secondVCard->getProperties(Address::class));
$this->assertCount(5, $this->thirdVCard->getProperties());
$this->assertCount(1, $this->thirdVCard->getProperties(Title::class));
$this->assertCount(1, $this->thirdVCard->getProperties(Role::class));
$this->assertCount(1, $this->thirdVCard->getProperties(Photo::class));
$this->assertCount(1, $this->thirdVCard->getProperties(Logo::class));
$this->assertCount(1, $this->thirdVCard->getProperties(Telephone::class));
}
/**
* Verify if multiple telephone numbers are correctly formatted
*/
public function testTelephonePropertyContent(): void
{
// Given
$expectedContent = "BEGIN:VCARD\r\n" .
"VERSION:4.0\r\n" .
"KIND:individual\r\n" .
"TEL;TYPE=home;VALUE=uri:tel:+33-01-23-45-67\r\n" .
"TEL;TYPE=work;VALUE=uri:tel:+33-05-42-41-96\r\n" .
"END:VCARD\r\n";
$formatter = new Formatter(new VcfFormatter(), '');
$vcard = (new VCard())
->add(new Telephone('+33 01 23 45 67'))
->add(new Telephone('+33-05-42-41-96', Type::work()));
// When
$formatter->addVCard($vcard);
// Then
$this->assertEquals($expectedContent, $formatter->getContent());
}
/**
* Verify if a full name gets correctly formatted
*/
public function testNamePropertyContent(): void
{
// Given
$expectedContent = "BEGIN:VCARD\r\n" .
"VERSION:4.0\r\n" .
"KIND:individual\r\n" .
"N:van den Berg;Melroy;Antoine;Mr.;\r\n" .
"END:VCARD\r\n";
$formatter = new Formatter(new VcfFormatter(), '');
$vcard = (new VCard())->add(new Name('van den Berg', 'Melroy', 'Antoine', 'Mr.'));
// When
$formatter->addVCard($vcard);
// Then
$this->assertEquals($expectedContent, $formatter->getContent());
}
/**
* Verify if an address gets correctly formatted,
* even with a long text over the 75 chars limit (excl. line breaks)
*/
public function testAddressPropertyContentWithLineBreak() : void
{
// Given
$expectedContent = "BEGIN:VCARD\r\n" .
"VERSION:4.0\r\n" .
"KIND:individual\r\n" .
"ADR;TYPE=home:42;Villa;Main Street 500;London;Barnet;EN4 0AG;United Kingd\r\n" .
// Line break because of 75 octets width limit, immediately followed by a single white space.
" om\r\n" .
"END:VCARD\r\n";
$formatter = new Formatter(new VcfFormatter(), '');
$vcard = (new VCard())->add(new Address('42', 'Villa', 'Main Street 500', 'London', 'Barnet', 'EN4 0AG', 'United Kingdom'));
// When
$formatter->addVCard($vcard);
// Then
$this->assertEquals($expectedContent, $formatter->getContent());
}
}