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