Passed
Pull Request — master (#161)
by Jeroen
02:48
created

VCardParserTest::testLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace JeroenDesloovere\VCard\tests;
4
5
use JeroenDesloovere\VCard\VCard;
6
use JeroenDesloovere\VCard\VCardParser;
7
use PHPUnit\Framework\TestCase;
8
9
/**
10
 * Unit tests for our VCard parser.
11
 */
12
class VCardParserTest extends TestCase
13
{
14
    /**
15
     * @expectedException OutOfBoundsException
16
     */
17
    public function testOutOfRangeException()
18
    {
19
        $parser = new VCardParser('');
20
        $parser->getCardAtIndex(2);
21
    }
22
23
    public function testSimpleVcard()
24
    {
25
        $vcard = new VCard();
26
        $vcard->addName("Desloovere", "Jeroen");
27
        $parser = new VCardParser($vcard->buildVCard());
28
        $this->assertEquals($parser->getCardAtIndex(0)->firstname, "Jeroen");
29
        $this->assertEquals($parser->getCardAtIndex(0)->lastname, "Desloovere");
30
        $this->assertEquals($parser->getCardAtIndex(0)->fullname, "Jeroen Desloovere");
31
    }
32
33
    public function testBDay()
34
    {
35
        $vcard = new VCard();
36
        $vcard->addBirthday('31-12-2015');
37
        $parser = new VCardParser($vcard->buildVCard());
38
        $this->assertEquals($parser->getCardAtIndex(0)->birthday->format('Y-m-d'), '2015-12-31');
39
    }
40
41
    public function testAddress()
42
    {
43
        $vcard = new VCard();
44
        $vcard->addAddress(
45
            "Lorem Corp.",
46
            "(extended info)",
47
            "54th Ipsum Street",
48
            "PHPsville",
49
            "Guacamole",
50
            "01158",
51
            "Gitland",
52
            'WORK;POSTAL'
53
        );
54
        $vcard->addAddress(
55
            "Jeroen Desloovere",
56
            "(extended info, again)",
57
            "25th Some Address",
58
            "Townsville",
59
            "Area 51",
60
            "045784",
61
            "Europe (is a country, right?)",
62
            'WORK;PERSONAL'
63
        );
64
        $vcard->addAddress(
65
            "Georges Desloovere",
66
            "(extended info, again, again)",
67
            "26th Some Address",
68
            "Townsville-South",
69
            "Area 51B",
70
            "04554",
71
            "Europe (no, it isn't)",
72
            'WORK;PERSONAL'
73
        );
74
        $parser = new VCardParser($vcard->buildVCard());
75
        $this->assertEquals($parser->getCardAtIndex(0)->address['WORK;POSTAL'][0], (object) array(
76
            'name' => "Lorem Corp.",
77
            'extended' => "(extended info)",
78
            'street' => "54th Ipsum Street",
79
            'city' => "PHPsville",
80
            'region' => "Guacamole",
81
            'zip' => "01158",
82
            'country' => "Gitland",
83
        ));
84
        $this->assertEquals($parser->getCardAtIndex(0)->address['WORK;PERSONAL'][0], (object) array(
85
            'name' => "Jeroen Desloovere",
86
            'extended' => "(extended info, again)",
87
            'street' => "25th Some Address",
88
            'city' => "Townsville",
89
            'region' => "Area 51",
90
            'zip' => "045784",
91
            'country' => "Europe (is a country, right?)",
92
        ));
93
        $this->assertEquals($parser->getCardAtIndex(0)->address['WORK;PERSONAL'][1], (object) array(
94
            'name' => "Georges Desloovere",
95
            'extended' => "(extended info, again, again)",
96
            'street' => "26th Some Address",
97
            'city' => "Townsville-South",
98
            'region' => "Area 51B",
99
            'zip' => "04554",
100
            'country' => "Europe (no, it isn't)",
101
        ));
102
    }
103
104
    public function testPhone()
105
    {
106
        $vcard = new VCard();
107
        $vcard->addPhoneNumber('0984456123');
108
        $vcard->addPhoneNumber('2015123487', 'WORK');
109
        $vcard->addPhoneNumber('4875446578', 'WORK');
110
        $vcard->addPhoneNumber('9875445464', 'PREF;WORK;VOICE');
111
        $parser = new VCardParser($vcard->buildVCard());
112
        $this->assertEquals($parser->getCardAtIndex(0)->phone['default'][0], '0984456123');
113
        $this->assertEquals($parser->getCardAtIndex(0)->phone['WORK'][0], '2015123487');
114
        $this->assertEquals($parser->getCardAtIndex(0)->phone['WORK'][1], '4875446578');
115
        $this->assertEquals($parser->getCardAtIndex(0)->phone['PREF;WORK;VOICE'][0], '9875445464');
116
    }
117
118
    public function testEmail()
119
    {
120
        $vcard = new VCard();
121
        $vcard->addEmail('[email protected]');
122
        $vcard->addEmail('[email protected]', 'WORK');
123
        $vcard->addEmail('[email protected]', 'WORK');
124
        $vcard->addEmail('[email protected]', 'PREF;WORK');
125
        $parser = new VCardParser($vcard->buildVCard());
126
        // The VCard class uses a default type of "INTERNET", so we do not test
127
        // against the "default" key.
128
        $this->assertEquals($parser->getCardAtIndex(0)->email['INTERNET'][0], '[email protected]');
129
        $this->assertEquals($parser->getCardAtIndex(0)->email['INTERNET;WORK'][0], '[email protected]');
130
        $this->assertEquals($parser->getCardAtIndex(0)->email['INTERNET;WORK'][1], '[email protected]');
131
        $this->assertEquals($parser->getCardAtIndex(0)->email['INTERNET;PREF;WORK'][0], '[email protected]');
132
    }
133
134
    public function testOrganization()
135
    {
136
        $vcard = new VCard();
137
        $vcard->addCompany('Lorem Corp.');
138
        $parser = new VCardParser($vcard->buildVCard());
139
        $this->assertEquals($parser->getCardAtIndex(0)->organization, 'Lorem Corp.');
140
    }
141
142
    public function testUrl()
143
    {
144
        $vcard = new VCard();
145
        $vcard->addUrl('http://www.jeroendesloovere.be');
146
        $vcard->addUrl('http://home.example.com', 'HOME');
147
        $vcard->addUrl('http://work1.example.com', 'PREF;WORK');
148
        $vcard->addUrl('http://work2.example.com', 'PREF;WORK');
149
        $parser = new VCardParser($vcard->buildVCard());
150
        $this->assertEquals($parser->getCardAtIndex(0)->url['default'][0], 'http://www.jeroendesloovere.be');
151
        $this->assertEquals($parser->getCardAtIndex(0)->url['HOME'][0], 'http://home.example.com');
152
        $this->assertEquals($parser->getCardAtIndex(0)->url['PREF;WORK'][0], 'http://work1.example.com');
153
        $this->assertEquals($parser->getCardAtIndex(0)->url['PREF;WORK'][1], 'http://work2.example.com');
154
    }
155
156
    public function testNote()
157
    {
158
        $vcard = new VCard();
159
        $vcard->addNote('This is a testnote');
160
        $parser = new VCardParser($vcard->buildVCard());
161
162
        $vcardMultiline = new VCard();
163
        $vcardMultiline->addNote("This is a multiline note\nNew line content!\r\nLine 2");
164
        $parserMultiline = new VCardParser($vcardMultiline->buildVCard());
165
166
        $this->assertEquals($parser->getCardAtIndex(0)->note, 'This is a testnote');
167
        $this->assertEquals(nl2br($parserMultiline->getCardAtIndex(0)->note), nl2br("This is a multiline note" . PHP_EOL . "New line content!" . PHP_EOL . "Line 2"));
168
    }
169
170
    public function testCategories()
171
    {
172
        $vcard = new VCard();
173
        $vcard->addCategories([
174
            'Category 1',
175
            'cat-2',
176
            'another long category!'
177
        ]);
178
        $parser = new VCardParser($vcard->buildVCard());
179
180
        $this->assertEquals($parser->getCardAtIndex(0)->categories[0], 'Category 1');
181
        $this->assertEquals($parser->getCardAtIndex(0)->categories[1], 'cat-2');
182
        $this->assertEquals($parser->getCardAtIndex(0)->categories[2], 'another long category!');
183
    }
184
185
    public function testTitle()
186
    {
187
        $vcard = new VCard();
188
        $vcard->addJobtitle('Ninja');
189
        $parser = new VCardParser($vcard->buildVCard());
190
        $this->assertEquals($parser->getCardAtIndex(0)->title, 'Ninja');
191
    }
192
193
    public function testLogo()
194
    {
195
        $image = __DIR__ . '/image.jpg';
196
        $imageUrl = 'https://raw.githubusercontent.com/jeroendesloovere/vcard/master/tests/image.jpg';
197
198
        $vcard = new VCard();
199
        $vcard->addLogo($image, true);
200
        $parser = new VCardParser($vcard->buildVCard());
201
        $this->assertEquals($parser->getCardAtIndex(0)->rawLogo, file_get_contents($image));
202
203
        $vcard = new VCard();
204
        $vcard->addLogo($image, false);
205
        $parser = new VCardParser($vcard->buildVCard());
206
        $this->assertEquals($parser->getCardAtIndex(0)->logo, __DIR__ . '/image.jpg');
207
208
        $vcard = new VCard();
209
        $vcard->addLogo($imageUrl, false);
210
        $parser = new VCardParser($vcard->buildVCard());
211
        $this->assertEquals($parser->getCardAtIndex(0)->logo, $imageUrl);
212
    }
213
214
    public function testPhoto()
215
    {
216
        $image = __DIR__ . '/image.jpg';
217
        $imageUrl = 'https://raw.githubusercontent.com/jeroendesloovere/vcard/master/tests/image.jpg';
218
219
        $vcard = new VCard();
220
        $vcard->addPhoto($image, true);
221
        $parser = new VCardParser($vcard->buildVCard());
222
        $this->assertEquals($parser->getCardAtIndex(0)->rawPhoto, file_get_contents($image));
223
224
        $vcard = new VCard();
225
        $vcard->addPhoto($image, false);
226
        $parser = new VCardParser($vcard->buildVCard());
227
        $this->assertEquals($parser->getCardAtIndex(0)->photo, __DIR__ . '/image.jpg');
228
229
        $vcard = new VCard();
230
        $vcard->addPhoto($imageUrl, false);
231
        $parser = new VCardParser($vcard->buildVCard());
232
        $this->assertEquals($parser->getCardAtIndex(0)->photo, $imageUrl);
233
    }
234
235
    public function testVcardDB()
236
    {
237
        $db = '';
238
        $vcard = new VCard();
239
        $vcard->addName("Desloovere", "Jeroen");
240
        $db .= $vcard->buildVCard();
241
242
        $vcard = new VCard();
243
        $vcard->addName("Lorem", "Ipsum");
244
        $db .= $vcard->buildVCard();
245
246
        $parser = new VCardParser($db);
247
        $this->assertEquals($parser->getCardAtIndex(0)->fullname, "Jeroen Desloovere");
248
        $this->assertEquals($parser->getCardAtIndex(1)->fullname, "Ipsum Lorem");
249
    }
250
251
    public function testIteration()
252
    {
253
        // Prepare a VCard DB.
254
        $db = '';
255
        $vcard = new VCard();
256
        $vcard->addName("Desloovere", "Jeroen");
257
        $db .= $vcard->buildVCard();
258
259
        $vcard = new VCard();
260
        $vcard->addName("Lorem", "Ipsum");
261
        $db .= $vcard->buildVCard();
262
263
        $parser = new VCardParser($db);
264
        foreach ($parser as $i => $card) {
265
            $this->assertEquals($card->fullname, $i == 0 ? "Jeroen Desloovere" : "Ipsum Lorem");
266
        }
267
    }
268
269
    public function testFromFile()
270
    {
271
        $parser = VCardParser::parseFromFile(__DIR__ . '/example.vcf');
272
        // Use this opportunity to test fetching all cards directly.
273
        $cards = $parser->getCards();
274
        $this->assertEquals($cards[0]->firstname, "Jeroen");
275
        $this->assertEquals($cards[0]->lastname, "Desloovere");
276
        $this->assertEquals($cards[0]->fullname, "Jeroen Desloovere");
277
        // Check the parsing of grouped items as well, which are present in the
278
        // example file.
279
        $this->assertEquals($cards[0]->url['default'][0], 'http://www.jeroendesloovere.be');
280
        $this->assertEquals($cards[0]->email['INTERNET'][0], '[email protected]');
281
    }
282
283
    /**
284
     * @expectedException \RuntimeException
285
     */
286
    public function testFileNotFound()
287
    {
288
        $parser = VCardParser::parseFromFile(__DIR__ . '/does-not-exist.vcf');
0 ignored issues
show
Unused Code introduced by
The assignment to $parser is dead and can be removed.
Loading history...
289
    }
290
291
    public function testLabel()
292
    {
293
        $label = 'street, worktown, workpostcode Belgium';
294
        $vcard = new VCard();
295
        $vcard->addLabel($label, 'work');
296
        $parser = new VCardParser($vcard->buildVCard());
297
        $this->assertEquals($parser->getCardAtIndex(0)->label, $label);
298
    }
299
}
300