DateAndOrTimeTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace Sabre\VObject\Property\VCard;
  3. use
  4. Sabre\VObject,
  5. Sabre\VObject\Reader;
  6. class DateAndOrTimeTest extends \PHPUnit_Framework_TestCase {
  7. /**
  8. * @dataProvider dates
  9. */
  10. function testGetJsonValue($input, $output) {
  11. $vcard = new VObject\Component\VCard();
  12. $prop = $vcard->createProperty('BDAY', $input);
  13. $this->assertEquals(array($output), $prop->getJsonValue());
  14. }
  15. function dates() {
  16. return array(
  17. array(
  18. "19961022T140000",
  19. "1996-10-22T14:00:00",
  20. ),
  21. array(
  22. "--1022T1400",
  23. "--10-22T14:00",
  24. ),
  25. array(
  26. "---22T14",
  27. "---22T14",
  28. ),
  29. array(
  30. "19850412",
  31. "1985-04-12",
  32. ),
  33. array(
  34. "1985-04",
  35. "1985-04",
  36. ),
  37. array(
  38. "1985",
  39. "1985",
  40. ),
  41. array(
  42. "--0412",
  43. "--04-12",
  44. ),
  45. array(
  46. "T102200",
  47. "T10:22:00",
  48. ),
  49. array(
  50. "T1022",
  51. "T10:22",
  52. ),
  53. array(
  54. "T10",
  55. "T10",
  56. ),
  57. array(
  58. "T-2200",
  59. "T-22:00",
  60. ),
  61. array(
  62. "T102200Z",
  63. "T10:22:00Z",
  64. ),
  65. array(
  66. "T102200-0800",
  67. "T10:22:00-0800",
  68. ),
  69. array(
  70. "T--00",
  71. "T--00",
  72. ),
  73. );
  74. }
  75. public function testSetParts() {
  76. $vcard = new VObject\Component\VCard();
  77. $prop = $vcard->createProperty('BDAY');
  78. $prop->setParts(array(
  79. new \DateTime('2014-04-02 18:37:00')
  80. ));
  81. $this->assertEquals('20140402T183700Z', $prop->getValue());
  82. }
  83. /**
  84. * @expectedException InvalidArgumentException
  85. */
  86. public function testSetPartsTooMany() {
  87. $vcard = new VObject\Component\VCard();
  88. $prop = $vcard->createProperty('BDAY');
  89. $prop->setParts(array(
  90. 1,
  91. 2
  92. ));
  93. }
  94. public function testSetPartsString() {
  95. $vcard = new VObject\Component\VCard();
  96. $prop = $vcard->createProperty('BDAY');
  97. $prop->setParts(array(
  98. "20140402T183700Z"
  99. ));
  100. $this->assertEquals('20140402T183700Z', $prop->getValue());
  101. }
  102. public function testSetValueDateTime() {
  103. $vcard = new VObject\Component\VCard();
  104. $prop = $vcard->createProperty('BDAY');
  105. $prop->setValue(
  106. new \DateTime('2014-04-02 18:37:00')
  107. );
  108. $this->assertEquals('20140402T183700Z', $prop->getValue());
  109. }
  110. public function testSetDateTimeOffset() {
  111. $vcard = new VObject\Component\VCard();
  112. $prop = $vcard->createProperty('BDAY');
  113. $prop->setValue(
  114. new \DateTime('2014-04-02 18:37:00', new \DateTimeZone('America/Toronto'))
  115. );
  116. $this->assertEquals('20140402T183700-0400', $prop->getValue());
  117. }
  118. public function testGetDateTime() {
  119. $datetime = new \DateTime('2014-04-02 18:37:00', new \DateTimeZone('America/Toronto'));
  120. $vcard = new VObject\Component\VCard();
  121. $prop = $vcard->createProperty('BDAY', $datetime);
  122. $dt = $prop->getDateTime();
  123. $this->assertEquals('2014-04-02T18:37:00-04:00', $dt->format('c'), "For some reason this one failed. Current default timezone is: " . date_default_timezone_get());
  124. }
  125. public function testGetDate() {
  126. $datetime = new \DateTime('2014-04-02');
  127. $vcard = new VObject\Component\VCard();
  128. $prop = $vcard->createProperty('BDAY', $datetime, null, 'DATE');
  129. $this->assertEquals('DATE', $prop->getValueType());
  130. $this->assertEquals('BDAY:20140402', rtrim($prop->serialize()));
  131. }
  132. public function testGetDateIncomplete() {
  133. $datetime = '--0407';
  134. $vcard = new VObject\Component\VCard();
  135. $prop = $vcard->add('BDAY', $datetime);
  136. $dt = $prop->getDateTime();
  137. // Note: if the year changes between the last line and the next line of
  138. // code, this test may fail.
  139. //
  140. // If that happens, head outside and have a drink.
  141. $current = new \DateTime('now');
  142. $year = $current->format('Y');
  143. $this->assertEquals($year . '0407', $dt->format('Ymd'));
  144. }
  145. public function testGetDateIncompleteFromVCard() {
  146. $vcard = <<<VCF
  147. BEGIN:VCARD
  148. VERSION:4.0
  149. BDAY:--0407
  150. END:VCARD
  151. VCF;
  152. $vcard = Reader::read($vcard);
  153. $prop = $vcard->BDAY;
  154. $dt = $prop->getDateTime();
  155. // Note: if the year changes between the last line and the next line of
  156. // code, this test may fail.
  157. //
  158. // If that happens, head outside and have a drink.
  159. $current = new \DateTime('now');
  160. $year = $current->format('Y');
  161. $this->assertEquals($year . '0407', $dt->format('Ymd'));
  162. }
  163. public function testValidate() {
  164. $datetime = '--0407';
  165. $vcard = new VObject\Component\VCard();
  166. $prop = $vcard->add('BDAY', $datetime);
  167. $this->assertEquals(array(), $prop->validate());
  168. }
  169. public function testValidateBroken() {
  170. $datetime = '123';
  171. $vcard = new VObject\Component\VCard();
  172. $prop = $vcard->add('BDAY', $datetime);
  173. $this->assertEquals(array(array(
  174. 'level' => 3,
  175. 'message' => 'The supplied value (123) is not a correct DATE-AND-OR-TIME property',
  176. 'node' => $prop,
  177. )), $prop->validate());
  178. }
  179. }