]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToRfc3339TransformerTest.php
gitignore vendor
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / Extension / Core / DataTransformer / DateTimeToRfc3339TransformerTest.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer;
13
14 use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToRfc3339Transformer;
15
16 class DateTimeToRfc3339TransformerTest extends DateTimeTestCase
17 {
18 protected $dateTime;
19 protected $dateTimeWithoutSeconds;
20
21 protected function setUp()
22 {
23 parent::setUp();
24
25 $this->dateTime = new \DateTime('2010-02-03 04:05:06 UTC');
26 $this->dateTimeWithoutSeconds = new \DateTime('2010-02-03 04:05:00 UTC');
27 }
28
29 protected function tearDown()
30 {
31 $this->dateTime = null;
32 $this->dateTimeWithoutSeconds = null;
33 }
34
35 public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE)
36 {
37 if ($expected instanceof \DateTime && $actual instanceof \DateTime) {
38 $expected = $expected->format('c');
39 $actual = $actual->format('c');
40 }
41
42 parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
43 }
44
45 public function allProvider()
46 {
47 return array(
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'),
54 );
55 }
56
57 public function transformProvider()
58 {
59 return $this->allProvider();
60 }
61
62 public function reverseTransformProvider()
63 {
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'),
68 ));
69 }
70
71 /**
72 * @dataProvider transformProvider
73 */
74 public function testTransform($fromTz, $toTz, $from, $to)
75 {
76 $transformer = new DateTimeToRfc3339Transformer($fromTz, $toTz);
77
78 $this->assertSame($to, $transformer->transform(null !== $from ? new \DateTime($from) : null));
79 }
80
81 /**
82 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
83 */
84 public function testTransformRequiresValidDateTime()
85 {
86 $transformer = new DateTimeToRfc3339Transformer();
87 $transformer->transform('2010-01-01');
88 }
89
90 /**
91 * @dataProvider reverseTransformProvider
92 */
93 public function testReverseTransform($toTz, $fromTz, $to, $from)
94 {
95 $transformer = new DateTimeToRfc3339Transformer($toTz, $fromTz);
96
97 if (null !== $to) {
98 $this->assertDateTimeEquals(new \DateTime($to), $transformer->reverseTransform($from));
99 } else {
100 $this->assertSame($to, $transformer->reverseTransform($from));
101 }
102 }
103
104 /**
105 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
106 */
107 public function testReverseTransformRequiresString()
108 {
109 $transformer = new DateTimeToRfc3339Transformer();
110 $transformer->reverseTransform(12345);
111 }
112
113 /**
114 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
115 */
116 public function testReverseTransformWithNonExistingDate()
117 {
118 $transformer = new DateTimeToRfc3339Transformer('UTC', 'UTC');
119
120 $transformer->reverseTransform('2010-04-31T04:05Z');
121 }
122
123 /**
124 * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
125 */
126 public function testReverseTransformExpectsValidDateString()
127 {
128 $transformer = new DateTimeToRfc3339Transformer('UTC', 'UTC');
129
130 $transformer->reverseTransform('2010-2010-2010');
131 }
132 }