ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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;
29
30class DateTimeTransformationTest extends TestCase
31{
33
34 protected function setUp(): void
35 {
36 $this->transformation = new DateTimeTransformation();
37 }
38
39 #[DataProvider('DateTimeTransformationDataProvider')]
40 public function testDateTimeISOTransformation(mixed $originVal, DateTimeImmutable $expectedVal): void
41 {
42 $transformedValue = $this->transformation->transform($originVal);
43 $this->assertIsObject($transformedValue);
44 $this->assertInstanceOf(DateTimeImmutable::class, $transformedValue);
45 $this->assertEquals($expectedVal, $transformedValue);
46 }
47
48 #[DataProvider('TransformationFailureDataProvider')]
49 public function testTransformIsInvalid(string $failingValue): void
50 {
51 $this->expectException(ConstraintViolationException::class);
52 $this->transformation->transform($failingValue);
53 }
54
55 public static function DateTimeTransformationDataProvider(): array
56 {
57 $now = new DateTimeImmutable();
58 return [
59 'datetime' => [$now, $now],
60 'iso8601' => ['2020-07-06T12:23:05+0000',
61 DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '2020-07-06T12:23:05+0000')],
62 'atom' => ['2020-07-06T12:23:05+00:00',
63 DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM, '2020-07-06T12:23:05+00:00')],
64 'rfc3339_ext' => ['2020-07-06T12:23:05.000+00:00',
65 DateTimeImmutable::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, '2020-07-06T12:23:05.000+00:00')],
66 'cookie' => ['Monday, 06-Jul-2020 12:23:05 GMT+0000',
67 DateTimeImmutable::createFromFormat(DateTimeInterface::COOKIE, 'Monday, 06-Jul-2020 12:23:05 GMT+0000')],
68 'rfc822' => ['Mon, 06 Jul 20 12:23:05 +0000',
69 DateTimeImmutable::createFromFormat(DateTimeInterface::RFC822, 'Mon, 06 Jul 20 12:23:05 +0000')],
70 'rfc7231' => ['Mon, 06 Jul 2020 12:23:05 GMT',
71 DateTimeImmutable::createFromFormat(DateTimeInterface::RFC7231, 'Mon, 06 Jul 2020 12:23:05 GMT')],
72 'unix_timestamp' => [481556262, DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '1985-04-05T13:37:42+0000')],
73 'unix_timestamp_float' => [481556262.4, DateTimeImmutable::createFromFormat(DateTimeInterface::ISO8601, '1985-04-05T13:37:42+0000')]
74 ];
75 }
76
77 public static function TransformationFailureDataProvider(): array
78 {
79 return [
80 'no_matching_string_format' => ['hello']
81 ];
82 }
83}
Transform date format to DateTimeImmutable Please note:
testDateTimeISOTransformation(mixed $originVal, DateTimeImmutable $expectedVal)