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