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