|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JsonSchema\Tests; |
| 4 | + |
| 5 | +use JsonSchema\Rfc3339; |
| 6 | + |
| 7 | +class Rfc3339Test extends \PHPUnit_Framework_TestCase |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @param string $string |
| 11 | + * @param \DateTime|null $expected |
| 12 | + * @dataProvider provideValidFormats |
| 13 | + */ |
| 14 | + public function testCreateFromValidString($string, \DateTime $expected) |
| 15 | + { |
| 16 | + $actual = Rfc3339::createFromString($string); |
| 17 | + |
| 18 | + $this->assertInstanceOf('DateTime', $actual); |
| 19 | + $this->assertEquals($expected->format('U.u'), $actual->format('U.u')); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @param string $string |
| 24 | + * @dataProvider provideInvalidFormats |
| 25 | + */ |
| 26 | + public function testCreateFromInvalidString($string) |
| 27 | + { |
| 28 | + $this->assertNull(Rfc3339::createFromString($string), sprintf('String "%s" should not be converted to DateTime', $string)); |
| 29 | + } |
| 30 | + |
| 31 | + public function provideValidFormats() |
| 32 | + { |
| 33 | + return array( |
| 34 | + array( |
| 35 | + '2000-05-01T12:12:12Z', |
| 36 | + \DateTime::createFromFormat('Y-m-d\TH:i:s', '2000-05-01T12:12:12', new \DateTimeZone('UTC')) |
| 37 | + ), |
| 38 | + array('2000-05-01T12:12:12+0100', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')), |
| 39 | + array('2000-05-01T12:12:12+01:00', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')), |
| 40 | + array( |
| 41 | + '2000-05-01T12:12:12.123456Z', |
| 42 | + \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123456', new \DateTimeZone('UTC')) |
| 43 | + ), |
| 44 | + array( |
| 45 | + '2000-05-01T12:12:12.123Z', |
| 46 | + \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123000', new \DateTimeZone('UTC')) |
| 47 | + ), |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function provideInvalidFormats() |
| 52 | + { |
| 53 | + return array( |
| 54 | + array('1999-1-11T00:00:00Z'), |
| 55 | + array('1999-01-11T00:00:00+100'), |
| 56 | + array('1999-01-11T00:00:00+1:00'), |
| 57 | + ); |
| 58 | + } |
| 59 | +} |
0 commit comments