123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace Sabre\VObject\Property\VCard;
- use
- Sabre\VObject,
- Sabre\VObject\Reader;
- class DateAndOrTimeTest extends \PHPUnit_Framework_TestCase {
- /**
- * @dataProvider dates
- */
- function testGetJsonValue($input, $output) {
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->createProperty('BDAY', $input);
- $this->assertEquals(array($output), $prop->getJsonValue());
- }
- function dates() {
- return array(
- array(
- "19961022T140000",
- "1996-10-22T14:00:00",
- ),
- array(
- "--1022T1400",
- "--10-22T14:00",
- ),
- array(
- "---22T14",
- "---22T14",
- ),
- array(
- "19850412",
- "1985-04-12",
- ),
- array(
- "1985-04",
- "1985-04",
- ),
- array(
- "1985",
- "1985",
- ),
- array(
- "--0412",
- "--04-12",
- ),
- array(
- "T102200",
- "T10:22:00",
- ),
- array(
- "T1022",
- "T10:22",
- ),
- array(
- "T10",
- "T10",
- ),
- array(
- "T-2200",
- "T-22:00",
- ),
- array(
- "T102200Z",
- "T10:22:00Z",
- ),
- array(
- "T102200-0800",
- "T10:22:00-0800",
- ),
- array(
- "T--00",
- "T--00",
- ),
- );
- }
- public function testSetParts() {
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->createProperty('BDAY');
- $prop->setParts(array(
- new \DateTime('2014-04-02 18:37:00')
- ));
- $this->assertEquals('20140402T183700Z', $prop->getValue());
- }
- /**
- * @expectedException InvalidArgumentException
- */
- public function testSetPartsTooMany() {
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->createProperty('BDAY');
- $prop->setParts(array(
- 1,
- 2
- ));
- }
- public function testSetPartsString() {
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->createProperty('BDAY');
- $prop->setParts(array(
- "20140402T183700Z"
- ));
- $this->assertEquals('20140402T183700Z', $prop->getValue());
- }
- public function testSetValueDateTime() {
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->createProperty('BDAY');
- $prop->setValue(
- new \DateTime('2014-04-02 18:37:00')
- );
- $this->assertEquals('20140402T183700Z', $prop->getValue());
- }
- public function testSetDateTimeOffset() {
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->createProperty('BDAY');
- $prop->setValue(
- new \DateTime('2014-04-02 18:37:00', new \DateTimeZone('America/Toronto'))
- );
- $this->assertEquals('20140402T183700-0400', $prop->getValue());
- }
- public function testGetDateTime() {
- $datetime = new \DateTime('2014-04-02 18:37:00', new \DateTimeZone('America/Toronto'));
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->createProperty('BDAY', $datetime);
- $dt = $prop->getDateTime();
- $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());
- }
- public function testGetDate() {
- $datetime = new \DateTime('2014-04-02');
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->createProperty('BDAY', $datetime, null, 'DATE');
- $this->assertEquals('DATE', $prop->getValueType());
- $this->assertEquals('BDAY:20140402', rtrim($prop->serialize()));
- }
- public function testGetDateIncomplete() {
- $datetime = '--0407';
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->add('BDAY', $datetime);
- $dt = $prop->getDateTime();
- // Note: if the year changes between the last line and the next line of
- // code, this test may fail.
- //
- // If that happens, head outside and have a drink.
- $current = new \DateTime('now');
- $year = $current->format('Y');
- $this->assertEquals($year . '0407', $dt->format('Ymd'));
- }
- public function testGetDateIncompleteFromVCard() {
- $vcard = <<<VCF
- BEGIN:VCARD
- VERSION:4.0
- BDAY:--0407
- END:VCARD
- VCF;
- $vcard = Reader::read($vcard);
- $prop = $vcard->BDAY;
- $dt = $prop->getDateTime();
- // Note: if the year changes between the last line and the next line of
- // code, this test may fail.
- //
- // If that happens, head outside and have a drink.
- $current = new \DateTime('now');
- $year = $current->format('Y');
- $this->assertEquals($year . '0407', $dt->format('Ymd'));
- }
- public function testValidate() {
- $datetime = '--0407';
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->add('BDAY', $datetime);
- $this->assertEquals(array(), $prop->validate());
- }
- public function testValidateBroken() {
- $datetime = '123';
- $vcard = new VObject\Component\VCard();
- $prop = $vcard->add('BDAY', $datetime);
- $this->assertEquals(array(array(
- 'level' => 3,
- 'message' => 'The supplied value (123) is not a correct DATE-AND-OR-TIME property',
- 'node' => $prop,
- )), $prop->validate());
- }
- }
|