VCardTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace Sabre\VObject\Splitter;
  3. use Sabre\VObject;
  4. class VCardTest extends \PHPUnit_Framework_TestCase {
  5. function createStream($data) {
  6. $stream = fopen('php://memory','r+');
  7. fwrite($stream, $data);
  8. rewind($stream);
  9. return $stream;
  10. }
  11. function testVCardImportValidVCard() {
  12. $data = <<<EOT
  13. BEGIN:VCARD
  14. UID:foo
  15. END:VCARD
  16. EOT;
  17. $tempFile = $this->createStream($data);
  18. $objects = new VCard($tempFile);
  19. $count = 0;
  20. while($objects->getNext()) {
  21. $count++;
  22. }
  23. $this->assertEquals(1, $count);
  24. }
  25. /**
  26. * @expectedException Sabre\VObject\ParseException
  27. */
  28. function testVCardImportWrongType() {
  29. $event[] = <<<EOT
  30. BEGIN:VEVENT
  31. UID:foo1
  32. DTSTAMP:20140122T233226Z
  33. DTSTART:20140101T050000Z
  34. END:VEVENT
  35. EOT;
  36. $event[] = <<<EOT
  37. BEGIN:VEVENT
  38. UID:foo2
  39. DTSTAMP:20140122T233226Z
  40. DTSTART:20140101T060000Z
  41. END:VEVENT
  42. EOT;
  43. $data = <<<EOT
  44. BEGIN:VCALENDAR
  45. $event[0]
  46. $event[1]
  47. END:VCALENDAR
  48. EOT;
  49. $tempFile = $this->createStream($data);
  50. $splitter = new VCard($tempFile);
  51. while($object=$splitter->getNext()) {
  52. }
  53. }
  54. function testVCardImportValidVCardsWithCategories() {
  55. $data = <<<EOT
  56. BEGIN:VCARD
  57. UID:card-in-foo1-and-foo2
  58. CATEGORIES:foo1,foo2
  59. END:VCARD
  60. BEGIN:VCARD
  61. UID:card-in-foo1
  62. CATEGORIES:foo1
  63. END:VCARD
  64. BEGIN:VCARD
  65. UID:card-in-foo3
  66. CATEGORIES:foo3
  67. END:VCARD
  68. BEGIN:VCARD
  69. UID:card-in-foo1-and-foo3
  70. CATEGORIES:foo1\,foo3
  71. END:VCARD
  72. EOT;
  73. $tempFile = $this->createStream($data);
  74. $splitter = new VCard($tempFile);
  75. $count = 0;
  76. while($object=$splitter->getNext()) {
  77. $count++;
  78. }
  79. $this->assertEquals(4, $count);
  80. }
  81. function testVCardImportEndOfData() {
  82. $data = <<<EOT
  83. BEGIN:VCARD
  84. UID:foo
  85. END:VCARD
  86. EOT;
  87. $tempFile = $this->createStream($data);
  88. $objects = new VCard($tempFile);
  89. $object=$objects->getNext();
  90. $this->assertNull($objects->getNext());
  91. }
  92. /**
  93. * @expectedException \Sabre\VObject\ParseException
  94. */
  95. function testVCardImportCheckInvalidArgumentException() {
  96. $data = <<<EOT
  97. BEGIN:FOO
  98. END:FOO
  99. EOT;
  100. $tempFile = $this->createStream($data);
  101. $objects = new VCard($tempFile);
  102. while($objects->getNext()) { }
  103. }
  104. function testVCardImportMultipleValidVCards() {
  105. $data = <<<EOT
  106. BEGIN:VCARD
  107. UID:foo
  108. END:VCARD
  109. BEGIN:VCARD
  110. UID:foo
  111. END:VCARD
  112. EOT;
  113. $tempFile = $this->createStream($data);
  114. $objects = new VCard($tempFile);
  115. $count = 0;
  116. while($objects->getNext()) {
  117. $count++;
  118. }
  119. $this->assertEquals(2, $count);
  120. }
  121. function testImportMultipleSeparatedWithNewLines() {
  122. $data = <<<EOT
  123. BEGIN:VCARD
  124. UID:foo
  125. END:VCARD
  126. BEGIN:VCARD
  127. UID:foo
  128. END:VCARD
  129. EOT;
  130. $tempFile = $this->createStream($data);
  131. $objects = new VCard($tempFile);
  132. $count = 0;
  133. while ($objects->getNext()) {
  134. $count++;
  135. }
  136. $this->assertEquals(2, $count);
  137. }
  138. function testVCardImportVCardWithoutUID() {
  139. $data = <<<EOT
  140. BEGIN:VCARD
  141. END:VCARD
  142. EOT;
  143. $tempFile = $this->createStream($data);
  144. $objects = new VCard($tempFile);
  145. $count = 0;
  146. while($objects->getNext()) {
  147. $count++;
  148. }
  149. $this->assertEquals(1, $count);
  150. }
  151. }