VCardTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. namespace JeroenDesloovere\VCard\tests;
  3. // required to load
  4. require_once __DIR__ . '/../vendor/autoload.php';
  5. /*
  6. * This file is part of the VCard PHP Class from Jeroen Desloovere.
  7. *
  8. * For the full copyright and license information, please view the license
  9. * file that was distributed with this source code.
  10. */
  11. use JeroenDesloovere\VCard\VCard;
  12. /**
  13. * This class will test our VCard PHP Class which can generate VCards.
  14. */
  15. class VCardTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var VCard
  19. */
  20. protected $vcard = null;
  21. /**
  22. * Data provider for testEmail()
  23. *
  24. * @return array
  25. */
  26. public function emailDataProvider()
  27. {
  28. return [
  29. [['john@doe.com']],
  30. [['john@doe.com', 'WORK' => 'john@work.com']],
  31. [['WORK' => 'john@work.com', 'HOME' => 'john@home.com']],
  32. [['PREF;WORK' => 'john@work.com', 'HOME' => 'john@home.com']],
  33. ];
  34. }
  35. /**
  36. * Set up before class
  37. *
  38. * @return void
  39. */
  40. public function setUp()
  41. {
  42. // set timezone
  43. date_default_timezone_set('Europe/Brussels');
  44. $this->vcard = new VCard();
  45. $this->firstName = 'Jeroen';
  46. $this->lastName = 'Desloovere';
  47. $this->additional = '&';
  48. $this->prefix = 'Mister';
  49. $this->suffix = 'Junior';
  50. $this->emailAddress1 = '';
  51. $this->emailAddress2 = '';
  52. $this->firstName2 = 'Ali';
  53. $this->lastName2 = 'ÖZSÜT';
  54. $this->firstName3 = 'Garçon';
  55. $this->lastName3 = 'Jéroèn';
  56. }
  57. /**
  58. * Tear down after class
  59. */
  60. public function tearDown()
  61. {
  62. $this->vcard = null;
  63. }
  64. public function testAddAddress()
  65. {
  66. $this->assertEquals($this->vcard, $this->vcard->addAddress(
  67. '',
  68. '88th Floor',
  69. '555 East Flours Street',
  70. 'Los Angeles',
  71. 'CA',
  72. '55555',
  73. 'USA'
  74. ));
  75. $this->assertContains('ADR;WORK;POSTAL;CHARSET=utf-8:;88th Floor;555 East Flours Street;Los Angele', $this->vcard->getOutput());
  76. // Should fold on row 75, so we should not see the full address.
  77. $this->assertNotContains('ADR;WORK;POSTAL;CHARSET=utf-8:;88th Floor;555 East Flours Street;Los Angeles;CA;55555;', $this->vcard->getOutput());
  78. }
  79. public function testAddBirthday()
  80. {
  81. $this->assertEquals($this->vcard, $this->vcard->addBirthday(''));
  82. }
  83. public function testAddCompany()
  84. {
  85. $this->assertEquals($this->vcard, $this->vcard->addCompany(''));
  86. }
  87. public function testAddCategories()
  88. {
  89. $this->assertEquals($this->vcard, $this->vcard->addCategories([]));
  90. }
  91. public function testAddEmail()
  92. {
  93. $this->assertEquals($this->vcard, $this->vcard->addEmail($this->emailAddress1));
  94. $this->assertEquals($this->vcard, $this->vcard->addEmail($this->emailAddress2));
  95. $this->assertEquals(2, count($this->vcard->getProperties()));
  96. }
  97. public function testAddJobTitle()
  98. {
  99. $this->assertEquals($this->vcard, $this->vcard->addJobtitle(''));
  100. }
  101. public function testAddRole()
  102. {
  103. $this->assertEquals($this->vcard, $this->vcard->addRole(''));
  104. }
  105. public function testAddName()
  106. {
  107. $this->assertEquals($this->vcard, $this->vcard->addName(''));
  108. }
  109. public function testAddNote()
  110. {
  111. $this->assertEquals($this->vcard, $this->vcard->addNote(''));
  112. }
  113. public function testAddPhoneNumber()
  114. {
  115. $this->assertEquals($this->vcard, $this->vcard->addPhoneNumber(''));
  116. $this->assertEquals($this->vcard, $this->vcard->addPhoneNumber(''));
  117. $this->assertEquals(2, count($this->vcard->getProperties()));
  118. }
  119. public function testAddPhotoWithJpgPhoto()
  120. {
  121. $return = $this->vcard->addPhoto(__DIR__ . '/image.jpg', true);
  122. $this->assertEquals($this->vcard, $return);
  123. }
  124. public function testAddPhotoWithRemoteJpgPhoto()
  125. {
  126. $return = $this->vcard->addPhoto(
  127. 'https://raw.githubusercontent.com/jeroendesloovere/vcard/master/tests/image.jpg',
  128. true
  129. );
  130. $this->assertEquals($this->vcard, $return);
  131. }
  132. /**
  133. * Test adding remote empty photo
  134. *
  135. * @expectedException Exception
  136. * @expectedExceptionMessage Returned data is not an image.
  137. */
  138. public function testAddPhotoWithRemoteEmptyJpgPhoto()
  139. {
  140. $this->vcard->addPhoto(
  141. 'https://raw.githubusercontent.com/jeroendesloovere/vcard/master/tests/empty.jpg',
  142. true
  143. );
  144. }
  145. public function testAddPhotoContentWithJpgPhoto()
  146. {
  147. $return = $this->vcard->addPhotoContent(file_get_contents(__DIR__ . '/image.jpg'));
  148. $this->assertEquals($this->vcard, $return);
  149. }
  150. /**
  151. * Test adding empty photo
  152. *
  153. * @expectedException Exception
  154. * @expectedExceptionMessage Returned data is not an image.
  155. */
  156. public function testAddPhotoContentWithEmptyContent()
  157. {
  158. $this->vcard->addPhotoContent('');
  159. }
  160. public function testAddLogoWithJpgImage()
  161. {
  162. $return = $this->vcard->addLogo(__DIR__ . '/image.jpg', true);
  163. $this->assertEquals($this->vcard, $return);
  164. }
  165. public function testAddLogoWithJpgImageNoInclude()
  166. {
  167. $return = $this->vcard->addLogo(__DIR__ . '/image.jpg', false);
  168. $this->assertEquals($this->vcard, $return);
  169. }
  170. public function testAddLogoContentWithJpgImage()
  171. {
  172. $return = $this->vcard->addLogoContent(file_get_contents(__DIR__ . '/image.jpg'));
  173. $this->assertEquals($this->vcard, $return);
  174. }
  175. /**
  176. * Test adding empty photo
  177. *
  178. * @expectedException Exception
  179. * @expectedExceptionMessage Returned data is not an image.
  180. */
  181. public function testAddLogoContentWithEmptyContent()
  182. {
  183. $this->vcard->addLogoContent('');
  184. }
  185. public function testAddUrl()
  186. {
  187. $this->assertEquals($this->vcard, $this->vcard->addUrl('1'));
  188. $this->assertEquals($this->vcard, $this->vcard->addUrl('2'));
  189. $this->assertEquals(2, count($this->vcard->getProperties()));
  190. }
  191. /**
  192. * Test adding local photo using an empty file
  193. *
  194. * @expectedException Exception
  195. * @expectedExceptionMessage Returned data is not an image.
  196. */
  197. public function testAddPhotoWithEmptyFile()
  198. {
  199. $this->vcard->addPhoto(__DIR__ . '/emptyfile', true);
  200. }
  201. /**
  202. * Test adding logo with no value
  203. *
  204. * @expectedException Exception
  205. * @expectedExceptionMessage Returned data is not an image.
  206. */
  207. public function testAddLogoWithNoValue()
  208. {
  209. $this->vcard->addLogo(__DIR__ . '/emptyfile', true);
  210. }
  211. /**
  212. * Test adding photo with no photo
  213. *
  214. * @expectedException Exception
  215. * @expectedExceptionMessage Returned data is not an image.
  216. */
  217. public function testAddPhotoWithNoPhoto()
  218. {
  219. $this->vcard->addPhoto(__DIR__ . '/wrongfile', true);
  220. }
  221. /**
  222. * Test adding logo with no image
  223. *
  224. * @expectedException Exception
  225. * @expectedExceptionMessage Returned data is not an image.
  226. */
  227. public function testAddLogoWithNoImage()
  228. {
  229. $this->vcard->addLogo(__DIR__ . '/wrongfile', true);
  230. }
  231. /**
  232. * Test charset
  233. */
  234. public function testCharset()
  235. {
  236. $charset = 'ISO-8859-1';
  237. $this->vcard->setCharset($charset);
  238. $this->assertEquals($charset, $this->vcard->getCharset());
  239. }
  240. /**
  241. * Test Email
  242. *
  243. * @dataProvider emailDataProvider $emails
  244. */
  245. public function testEmail($emails = [])
  246. {
  247. foreach ($emails as $key => $email) {
  248. if (is_string($key)) {
  249. $this->vcard->addEmail($email, $key);
  250. } else {
  251. $this->vcard->addEmail($email);
  252. }
  253. }
  254. foreach ($emails as $key => $email) {
  255. if (is_string($key)) {
  256. $this->assertContains('EMAIL;INTERNET;' . $key . ':' . $email, $this->vcard->getOutput());
  257. } else {
  258. $this->assertContains('EMAIL;INTERNET:' . $email, $this->vcard->getOutput());
  259. }
  260. }
  261. }
  262. /**
  263. * Test first name and last name
  264. */
  265. public function testFirstNameAndLastName()
  266. {
  267. $this->vcard->addName(
  268. $this->lastName,
  269. $this->firstName
  270. );
  271. $this->assertEquals('jeroen-desloovere', $this->vcard->getFilename());
  272. }
  273. /**
  274. * Test full blown name
  275. */
  276. public function testFullBlownName()
  277. {
  278. $this->vcard->addName(
  279. $this->lastName,
  280. $this->firstName,
  281. $this->additional,
  282. $this->prefix,
  283. $this->suffix
  284. );
  285. $this->assertEquals('mister-jeroen-desloovere-junior', $this->vcard->getFilename());
  286. }
  287. /**
  288. * Test multiple birthdays
  289. *
  290. * @expectedException Exception
  291. */
  292. public function testMultipleBirthdays()
  293. {
  294. $this->assertEquals($this->vcard, $this->vcard->addBirthday('1'));
  295. $this->assertEquals($this->vcard, $this->vcard->addBirthday('2'));
  296. }
  297. /**
  298. * Test multiple categories
  299. *
  300. * @expectedException Exception
  301. */
  302. public function testMultipleCategories()
  303. {
  304. $this->assertEquals($this->vcard, $this->vcard->addCategories(['1']));
  305. $this->assertEquals($this->vcard, $this->vcard->addCategories(['2']));
  306. }
  307. /**
  308. * Test multiple companies
  309. *
  310. * @expectedException Exception
  311. */
  312. public function testMultipleCompanies()
  313. {
  314. $this->assertEquals($this->vcard, $this->vcard->addCompany('1'));
  315. $this->assertEquals($this->vcard, $this->vcard->addCompany('2'));
  316. }
  317. /**
  318. * Test multiple job titles
  319. *
  320. * @expectedException Exception
  321. */
  322. public function testMultipleJobtitles()
  323. {
  324. $this->assertEquals($this->vcard, $this->vcard->addJobtitle('1'));
  325. $this->assertEquals($this->vcard, $this->vcard->addJobtitle('2'));
  326. }
  327. /**
  328. * Test multiple roles
  329. *
  330. * @expectedException Exception
  331. */
  332. public function testMultipleRoles()
  333. {
  334. $this->assertEquals($this->vcard, $this->vcard->addRole('1'));
  335. $this->assertEquals($this->vcard, $this->vcard->addRole('2'));
  336. }
  337. /**
  338. * Test multiple names
  339. *
  340. * @expectedException Exception
  341. */
  342. public function testMultipleNames()
  343. {
  344. $this->assertEquals($this->vcard, $this->vcard->addName('1'));
  345. $this->assertEquals($this->vcard, $this->vcard->addName('2'));
  346. }
  347. /**
  348. * Test multiple notes
  349. *
  350. * @expectedException Exception
  351. */
  352. public function testMultipleNotes()
  353. {
  354. $this->assertEquals($this->vcard, $this->vcard->addNote('1'));
  355. $this->assertEquals($this->vcard, $this->vcard->addNote('2'));
  356. }
  357. /**
  358. * Test special first name and last name
  359. */
  360. public function testSpecialFirstNameAndLastName()
  361. {
  362. $this->vcard->addName(
  363. $this->lastName2,
  364. $this->firstName2
  365. );
  366. $this->assertEquals('ali-ozsut', $this->vcard->getFilename());
  367. }
  368. /**
  369. * Test special first name and last name
  370. */
  371. public function testSpecialFirstNameAndLastName2()
  372. {
  373. $this->vcard->addName(
  374. $this->lastName3,
  375. $this->firstName3
  376. );
  377. $this->assertEquals('garcon-jeroen', $this->vcard->getFilename());
  378. }
  379. /**
  380. * Test multiple labels
  381. */
  382. public function testMultipleLabels()
  383. {
  384. $this->assertSame($this->vcard, $this->vcard->addLabel('My label'));
  385. $this->assertSame($this->vcard, $this->vcard->addLabel('My work label', 'WORK'));
  386. $this->assertSame(2, count($this->vcard->getProperties()));
  387. $this->assertContains('LABEL:My label', $this->vcard->getOutput());
  388. $this->assertContains('LABEL;WORK:My work label', $this->vcard->getOutput());
  389. }
  390. public function testChunkSplitUnicode()
  391. {
  392. $class_handler = new \ReflectionClass('JeroenDesloovere\VCard\VCard');
  393. $method_handler = $class_handler->getMethod('chunk_split_unicode');
  394. $method_handler->setAccessible(true);
  395. $ascii_input="Lorem ipsum dolor sit amet,";
  396. $ascii_output = $method_handler->invokeArgs(new VCard(), [$ascii_input,10,'|']);
  397. $unicode_input='Τη γλώσσα μου έδωσαν ελληνική το σπίτι φτωχικό στις αμμουδιές του Ομήρου.';
  398. $unicode_output = $method_handler->invokeArgs(new VCard(), [$unicode_input,10,'|']);
  399. $this->assertEquals(
  400. "Lorem ipsu|m dolor si|t amet,|",
  401. $ascii_output);
  402. $this->assertEquals(
  403. "Τη γλώσσα |μου έδωσαν| ελληνική |το σπίτι φ|τωχικό στι|ς αμμουδιέ|ς του Ομήρ|ου.|",
  404. $unicode_output);
  405. }
  406. }