Mercurial > hg > rc1
comparison vendor/sabre/vobject/tests/VObject/Property/VCard/DateAndOrTimeTest.php @ 7:430dbd5346f7
vendor sabre as distributed
| author | Charlie Root |
|---|---|
| date | Sat, 13 Jan 2018 09:06:10 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 6:cec75ba50afc | 7:430dbd5346f7 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Sabre\VObject\Property\VCard; | |
| 4 | |
| 5 use | |
| 6 Sabre\VObject, | |
| 7 Sabre\VObject\Reader; | |
| 8 | |
| 9 class 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 testGetDateIncomplete() { | |
| 166 | |
| 167 $datetime = '--0407'; | |
| 168 | |
| 169 $vcard = new VObject\Component\VCard(); | |
| 170 $prop = $vcard->add('BDAY', $datetime); | |
| 171 | |
| 172 $dt = $prop->getDateTime(); | |
| 173 // Note: if the year changes between the last line and the next line of | |
| 174 // code, this test may fail. | |
| 175 // | |
| 176 // If that happens, head outside and have a drink. | |
| 177 $current = new \DateTime('now'); | |
| 178 $year = $current->format('Y'); | |
| 179 | |
| 180 $this->assertEquals($year . '0407', $dt->format('Ymd')); | |
| 181 | |
| 182 } | |
| 183 | |
| 184 public function testGetDateIncompleteFromVCard() { | |
| 185 | |
| 186 $vcard = <<<VCF | |
| 187 BEGIN:VCARD | |
| 188 VERSION:4.0 | |
| 189 BDAY:--0407 | |
| 190 END:VCARD | |
| 191 VCF; | |
| 192 $vcard = Reader::read($vcard); | |
| 193 $prop = $vcard->BDAY; | |
| 194 | |
| 195 $dt = $prop->getDateTime(); | |
| 196 // Note: if the year changes between the last line and the next line of | |
| 197 // code, this test may fail. | |
| 198 // | |
| 199 // If that happens, head outside and have a drink. | |
| 200 $current = new \DateTime('now'); | |
| 201 $year = $current->format('Y'); | |
| 202 | |
| 203 $this->assertEquals($year . '0407', $dt->format('Ymd')); | |
| 204 | |
| 205 } | |
| 206 | |
| 207 public function testValidate() { | |
| 208 | |
| 209 $datetime = '--0407'; | |
| 210 | |
| 211 $vcard = new VObject\Component\VCard(); | |
| 212 $prop = $vcard->add('BDAY', $datetime); | |
| 213 | |
| 214 $this->assertEquals(array(), $prop->validate()); | |
| 215 | |
| 216 } | |
| 217 | |
| 218 public function testValidateBroken() { | |
| 219 | |
| 220 $datetime = '123'; | |
| 221 | |
| 222 $vcard = new VObject\Component\VCard(); | |
| 223 $prop = $vcard->add('BDAY', $datetime); | |
| 224 | |
| 225 $this->assertEquals(array(array( | |
| 226 'level' => 3, | |
| 227 'message' => 'The supplied value (123) is not a correct DATE-AND-OR-TIME property', | |
| 228 'node' => $prop, | |
| 229 )), $prop->validate()); | |
| 230 | |
| 231 } | |
| 232 } | |
| 233 |
