ILIAS  release_8 Revision v8.24
DateFormatTest.php
Go to the documentation of this file.
1<?php
18declare(strict_types=1);
19
20require_once("libs/composer/vendor/autoload.php");
21
23use PHPUnit\Framework\TestCase;
24
25class DateFormatTest extends TestCase
26{
27 public function setUp(): void
28 {
29 $f = new ILIAS\Data\Factory();
30 $this->df = $f->dateFormat();
31 }
32
33 public function testDateFormatFactory(): void
34 {
35 $this->assertInstanceOf(DateFormat\DateFormat::class, $this->df->standard());
36 $this->assertInstanceOf(DateFormat\DateFormat::class, $this->df->germanShort());
37 $this->assertInstanceOf(DateFormat\DateFormat::class, $this->df->germanLong());
38 $this->assertInstanceOf(DateFormat\FormatBuilder::class, $this->df->custom());
39 }
40
41 public function testDateFormatBuilderAndGetters(): void
42 {
43 $expect = [
44 '.', ',', '-', '/', ' ', ':', 'd', 'jS', 'l', 'D', 'W', 'm', 'F', 'M', 'Y', 'y', 'h','H', 'i', 's', 'a'
45 ];
46 $format = $this->df->custom()
47 ->dot()->comma()->dash()->slash()->space()->colon()
48 ->day()->dayOrdinal()->weekday()->weekdayShort()
49 ->week()->month()->monthSpelled()->monthSpelledShort()
50 ->year()->twoDigitYear()
51 ->hours12()->hours24()->minutes()->seconds()->meridiem()
52 ->get();
53
54 $this->assertEquals(
55 $expect,
56 $format->toArray()
57 );
58
59 $this->assertEquals(
60 implode('', $expect),
61 $format->toString()
62 );
63
64 $this->assertEquals(
65 $format->toString(),
66 (string) $format
67 );
68 }
69
70 public function testDateFormatInvalidTokens(): void
71 {
72 $this->expectException(InvalidArgumentException::class);
73 new DateFormat\DateFormat(['x', '2']);
74 }
75
76 public function testDateFormatApplyTo(): void
77 {
78 $dt = new DateTimeImmutable("1985-04-05");
79 $format = $this->df->germanShort();
80 $this->assertEquals("05.04.1985", $format->applyTo($dt));
81 $this->assertEquals("05.04.1985", $dt->format((string) $format));
82 }
83
84 public function testDateFormatApplyToWithTime(): void
85 {
86 $dt = new DateTimeImmutable("1985-04-05 21:12:30");
87 $format = $this->df->custom()
88 ->day()->dot()->month()->dot()->year()
89 ->space()->hours12()->colon()->minutes()->space()->meridiem()
90 ->get();
91 $this->assertEquals("05.04.1985 09:12 pm", $format->applyTo($dt));
92 $this->assertEquals("05.04.1985 09:12 pm", $dt->format((string) $format));
93 $format = $this->df->custom()
94 ->day()->dot()->month()->dot()->year()
95 ->space()->hours24()->colon()->minutes()->colon()->seconds()
96 ->get();
97 $this->assertEquals("05.04.1985 21:12:30", $format->applyTo($dt));
98 }
99
100 public function testDateFormatExpand(): void
101 {
102 $format = $this->df->germanShort();
103 $appended = $this->df->amend($format)->dot()->dot()->get();
104 $this->assertInstanceOf(DateFormat\DateFormat::class, $appended);
105 $this->assertEquals(
106 array_merge($format->toArray(), ['.', '.']),
107 $appended->toArray()
108 );
109 }
110}
testDateFormatBuilderAndGetters()
A Date Format provides a format definition akin to PHP's date formatting options, but stores the sing...
Definition: DateFormat.php:27
Builds data types.
Definition: Factory.php:21
$format
Definition: metadata.php:235