VCardParserTest.php 11 KB

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