ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DataHelperTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
24
25class DataHelperTest extends TestCase
26{
27 protected const array CORRECT_DURS = [
28 'P12Y4M2DT56H900001M0S',
29 'P4MT56H0S',
30 'P4M67D',
31 'PT4M89S',
32 ];
33
34 protected const array WRONG_DURS = [
35 '4MT56H0S',
36 'P4M56H0S',
37 'just wrong'
38 ];
39
40 protected const array CORRECT_DATES = [
41 '2001',
42 '2013-07-09',
43 '2001-12-01T23:56:01.1234Z',
44 ];
45
46 protected const array WRONG_DATES = [
47 '2001-13897877-01T23:56:01.1234Z',
48 '2001-13897877-0123:56:01.1234Z',
49 'something else',
50 ];
51
52 public function testMatchesDurationPattern(): void
53 {
54 $helper = new DataHelper();
55 foreach (self::CORRECT_DURS as $dur) {
56 $this->assertTrue($helper->matchesDurationPattern($dur));
57 }
58 foreach (self::WRONG_DURS as $dur) {
59 $this->assertFalse($helper->matchesDurationPattern($dur));
60 }
61 }
62
63 public function testMatchesDatetimePattern(): void
64 {
65 $helper = new DataHelper();
66 foreach (self::CORRECT_DATES as $date) {
67 $this->assertTrue($helper->matchesDatetimePattern($date));
68 }
69 foreach (self::WRONG_DATES as $date) {
70 $this->assertFalse($helper->matchesDatetimePattern($date));
71 }
72 }
73
74 public function testDurationToIterator(): void
75 {
76 $helper = new DataHelper();
77 $exp = [
78 ['12', '4', '2', '56', '900001', '0'],
79 [null, '4', null, '56', null, '0'],
80 [null, '4', '67', null, null, null],
81 [null, null, null, null, '4', '89']
82 ];
83 foreach (self::CORRECT_DURS as $index => $dur) {
84 $this->assertSame(
85 $exp[$index],
86 iterator_to_array($helper->durationToIterator($dur))
87 );
88 }
89 $this->assertEmpty(iterator_to_array($helper->durationToIterator(self::WRONG_DURS[0])));
90 }
91
92 public function testDurationToSeconds(): void
93 {
94 $helper = new DataHelper();
95 $this->assertSame(
96 89 + 4 * 60,
97 $helper->durationToSeconds(self::CORRECT_DURS[3])
98 );
99 $this->assertSame(
100 56 * 3600 + 4 * 30 * 24 * 3600,
101 $helper->durationToSeconds(self::CORRECT_DURS[1])
102 );
103 }
104
105 public function testDatetimeToIterator(): void
106 {
107 $helper = new DataHelper();
108 $exp = [
109 ['2001', null, null, null, null, null, null, null],
110 ['2013', '07', '09', null, null, null, null, null],
111 ['2001', '12', '01', '23', '56', '01', '1234', 'Z']
112 ];
113 foreach (self::CORRECT_DATES as $index => $date) {
114 $this->assertSame(
115 $exp[$index],
116 iterator_to_array($helper->datetimeToIterator($date))
117 );
118 }
119 $this->assertEmpty(iterator_to_array($helper->datetimeToIterator(self::WRONG_DATES[0])));
120 }
121
122 public function testDatetimeToObject(): void
123 {
124 $helper = new DataHelper();
125 $exp = [
126 '2001-01-01',
127 '2013-07-09',
128 '2001-12-01',
129 ];
130 foreach (self::CORRECT_DATES as $index => $date) {
131 $this->assertSame(
132 $exp[$index],
133 $helper->datetimeToObject($date)->format('Y-m-d')
134 );
135 }
136 $this->assertSame(
137 '0000-01-01',
138 $helper->datetimeToObject(self::WRONG_DATES[0])->format('Y-m-d')
139 );
140 }
141
142 public function testDurationFromIntegers(): void
143 {
144 $helper = new DataHelper();
145 $int_arrays = [
146 [12, 4, 2, 56, 900001, 0],
147 [null, 4, null, 56, null, 0],
148 [null, 4, 67, null, null, null],
149 [null, null, null, null, 4, 89]
150 ];
151 foreach ($int_arrays as $index => $int_array) {
152 $this->assertSame(
153 self::CORRECT_DURS[$index],
154 $helper->durationFromIntegers(...$int_array)
155 );
156 }
157 }
158
159 public function testDatetimeFromObject(): void
160 {
161 $helper = new DataHelper();
162 $date = new \DateTimeImmutable('2013-07-09');
163 $this->assertSame(
164 '2013-07-09',
165 $helper->datetimeFromObject($date)
166 );
167 }
168}