4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer
;
14 use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToRfc3339Transformer
;
16 class DateTimeToRfc3339TransformerTest
extends DateTimeTestCase
19 protected $dateTimeWithoutSeconds;
21 protected function setUp()
25 $this->dateTime
= new \
DateTime('2010-02-03 04:05:06 UTC');
26 $this->dateTimeWithoutSeconds
= new \
DateTime('2010-02-03 04:05:00 UTC');
29 protected function tearDown()
31 $this->dateTime
= null;
32 $this->dateTimeWithoutSeconds
= null;
35 public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
37 if ($expected instanceof \DateTime
&& $actual instanceof \DateTime
) {
38 $expected = $expected->format('c');
39 $actual = $actual->format('c');
42 parent
::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
45 public function allProvider()
48 array('UTC', 'UTC', '2010-02-03 04:05:06 UTC', '2010-02-03T04:05:06Z'),
49 array('UTC', 'UTC', null, ''),
50 array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:06 America/New_York', '2010-02-03T17:05:06+08:00'),
51 array('America/New_York', 'Asia/Hong_Kong', null, ''),
52 array('UTC', 'Asia/Hong_Kong', '2010-02-03 04:05:06 UTC', '2010-02-03T12:05:06+08:00'),
53 array('America/New_York', 'UTC', '2010-02-03 04:05:06 America/New_York', '2010-02-03T09:05:06Z'),
57 public function transformProvider()
59 return $this->allProvider();
62 public function reverseTransformProvider()
64 return array_merge($this->allProvider(), array(
65 // format without seconds, as appears in some browsers
66 array('UTC', 'UTC', '2010-02-03 04:05:00 UTC', '2010-02-03T04:05Z'),
67 array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:00 America/New_York', '2010-02-03T17:05+08:00'),
72 * @dataProvider transformProvider
74 public function testTransform($fromTz, $toTz, $from, $to)
76 $transformer = new DateTimeToRfc3339Transformer($fromTz, $toTz);
78 $this->assertSame($to, $transformer->transform(null !== $from ? new \
DateTime($from) : null));
82 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
84 public function testTransformRequiresValidDateTime()
86 $transformer = new DateTimeToRfc3339Transformer();
87 $transformer->transform('2010-01-01');
91 * @dataProvider reverseTransformProvider
93 public function testReverseTransform($toTz, $fromTz, $to, $from)
95 $transformer = new DateTimeToRfc3339Transformer($toTz, $fromTz);
98 $this->assertDateTimeEquals(new \
DateTime($to), $transformer->reverseTransform($from));
100 $this->assertSame($to, $transformer->reverseTransform($from));
105 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
107 public function testReverseTransformRequiresString()
109 $transformer = new DateTimeToRfc3339Transformer();
110 $transformer->reverseTransform(12345);
114 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
116 public function testReverseTransformWithNonExistingDate()
118 $transformer = new DateTimeToRfc3339Transformer('UTC', 'UTC');
120 $transformer->reverseTransform('2010-04-31T04:05Z');
124 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
126 public function testReverseTransformExpectsValidDateString()
128 $transformer = new DateTimeToRfc3339Transformer('UTC', 'UTC');
130 $transformer->reverseTransform('2010-2010-2010');