ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
DateTimeTransformationTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use DateTimeImmutable;
24use DateTimeInterface;
27use PHPUnit\Framework\TestCase;
28use PHPUnit\Framework\Attributes\DataProvider;
29use DateTimeZone;
30
31class DateTimeTransformationTest extends TestCase
32{
34
35 protected function setUp(): void
36 {
37 $this->transformation = new DateTimeTransformation();
38 }
39
40 #[DataProvider('DateTimeTransformationDataProvider')]
41 public function testDateTimeISOTransformation(mixed $originVal, DateTimeImmutable $expectedVal): void
42 {
43 $transformedValue = $this->transformation->transform($originVal);
44 $this->assertIsObject($transformedValue);
45 $this->assertInstanceOf(DateTimeImmutable::class, $transformedValue);
46 $this->assertEquals($expectedVal, $transformedValue);
47 }
48
54 {
55 $gmt_format = 'D, d M Y H:i:s \G\M\T'; // former DateTimeInterface::RFC7231
56 $test_gmt_date_time = 'Mon, 06 Jul 2020 12:23:05 GMT';
57
58 $exptected = DateTimeImmutable::createFromFormat(
59 $gmt_format,
60 $test_gmt_date_time
61 );
62
63 $actual = $this->transformation->transform($test_gmt_date_time);
64 $this->assertEquals($exptected, $actual);
65
66 $actual = $actual->setTimezone(new DateTimeZone('Europe/Berlin'));
67 // GMT in the provided format is just a string, it does not effect the presented timezone
68 $this->assertSame('Mon, 06 Jul 2020 14:23:05 GMT', $actual->format($gmt_format));
69 $this->assertEquals($exptected, $actual);
70 }
71
72 #[DataProvider('TransformationFailureDataProvider')]
73 public function testTransformIsInvalid(string $failingValue): void
74 {
75 $this->expectException(ConstraintViolationException::class);
76 $this->transformation->transform($failingValue);
77 }
78
79 public static function DateTimeTransformationDataProvider(): array
80 {
81 $now = new DateTimeImmutable();
82 return [
83 'datetime' => [$now, $now],
84 'iso8601' => [
85 '2020-07-06T12:23:05+0000',
86 DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2020-07-06T12:23:05+0000')
87 ],
88 'atom' => [
89 '2020-07-06T12:23:05+00:00',
90 DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, '2020-07-06T12:23:05+00:00')
91 ],
92 'rfc3339_ext' => [
93 '2020-07-06T12:23:05.000+00:00',
94 DateTimeImmutable::createFromFormat(
95 DateTimeInterface::RFC3339_EXTENDED,
96 '2020-07-06T12:23:05.000+00:00'
97 )
98 ],
99 'cookie' => [
100 'Monday, 06-Jul-2020 12:23:05 GMT+0000',
101 DateTimeImmutable::createFromFormat(DateTimeInterface::COOKIE, 'Monday, 06-Jul-2020 12:23:05 GMT+0000')
102 ],
103 'rfc822' => [
104 'Mon, 06 Jul 20 12:23:05 +0000',
105 DateTimeImmutable::createFromFormat(DateTimeInterface::RFC822, 'Mon, 06 Jul 20 12:23:05 +0000')
106 ],
107 'rfc7231' => [
108 'Mon, 06 Jul 2020 12:23:05 GMT',
109 DateTimeImmutable::createFromFormat('D, d M Y H:i:s \G\M\T', 'Mon, 06 Jul 2020 12:23:05 GMT')
110 ],
111 'unix_timestamp' => [
112 481556262,
113 DateTimeImmutable::createFromFormat(
114 DateTimeInterface::ISO8601,
115 '1985-04-05T13:37:42+0000'
116 )
117 ],
118 'unix_timestamp_float' => [
119 481556262.4,
120 DateTimeImmutable::createFromFormat(
121 DateTimeInterface::ISO8601,
122 '1985-04-05T13:37:42+0000'
123 )
124 ]
125 ];
126 }
127
128 public static function TransformationFailureDataProvider(): array
129 {
130 return [
131 'no_matching_string_format' => ['hello']
132 ];
133 }
134}
Transform date format to DateTimeImmutable Please note:
testDateTimeISOTransformation(mixed $originVal, DateTimeImmutable $expectedVal)