ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
DataHelperTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 
30 class DataHelperTest extends TestCase
31 {
32  protected function getData(string $value): ElementsDataInterface
33  {
34  return new class ($value) extends NullElementsData {
35  public function __construct(protected string $value)
36  {
37  }
38 
39  public function value(): string
40  {
41  return $this->value;
42  }
43  };
44  }
45 
46  protected function getDataHelper(): DataHelper
47  {
48  $internal_helper = new class () extends NullDataHelper {
49  public function durationToIterator(string $duration): \Generator
50  {
51  foreach (explode(':', $duration) as $v) {
52  yield $v === '' ? null : $v;
53  }
54  }
55 
56  public function durationToSeconds(string $duration): int
57  {
58  $r = 0;
59  foreach ($this->durationToIterator($duration) as $v) {
60  $r += $v;
61  }
62  return $r;
63  }
64 
65  public function datetimeToObject(string $datetime): \DateTimeImmutable
66  {
67  return new \DateTimeImmutable($datetime);
68  }
69 
70  public function durationFromIntegers(
71  ?int $years,
72  ?int $months,
73  ?int $days,
74  ?int $hours,
75  ?int $minutes,
76  ?int $seconds
77  ): string {
78  $array = [$years, $months, $days, $hours, $minutes, $seconds];
79  return implode(':', $array);
80  }
81 
82  public function datetimeFromObject(\DateTimeImmutable $object): string
83  {
84  return $object->format('Y-m-d');
85  }
86  };
87 
88  $data_presentation = new class () extends NullData {
89  public function dataValue(ElementsDataInterface $data): string
90  {
91  return 'presentable ' . $data->value();
92  }
93  };
94 
96  }
97 
98  public function testMakePresentable(): void
99  {
100  $helper = $this->getDataHelper();
101 
102  $this->assertSame(
103  'presentable value',
104  $helper->makePresentable($this->getData('value'))
105  );
106  }
107 
108  public function testMakePresentableAsList(): void
109  {
110  $helper = $this->getDataHelper();
111 
112  $this->assertSame(
113  'presentable value1,? ,.presentable value2,? ,.presentable value3',
114  $helper->makePresentableAsList(
115  ',? ,.',
116  $this->getData('value1'),
117  $this->getData('value2'),
118  $this->getData('value3')
119  )
120  );
121  }
122 
123  public function testDurationToArray(): void
124  {
125  $helper = $this->getDataHelper();
126 
127  $this->assertSame(
128  [89, 0, null, null, null, 1],
129  $helper->durationToArray('89:0::::1')
130  );
131  }
132 
133  public function testDurationToSeconds(): void
134  {
135  $helper = $this->getDataHelper();
136 
137  $this->assertSame(
138  89 + 5 + 1,
139  $helper->durationToSeconds('89:5::::1')
140  );
141  }
142 
143  public function testDatetimeToObject(): void
144  {
145  $helper = $this->getDataHelper();
146 
147  $this->assertSame(
148  '2013-01-20',
149  $helper->datetimeToObject('2013-01-20')->format('Y-m-d')
150  );
151  }
152 
153  public function testDurationFromIntegers(): void
154  {
155  $helper = $this->getDataHelper();
156 
157  $this->assertSame(
158  '89:0::::1',
159  $helper->durationFromIntegers(89, 0, null, null, null, 1)
160  );
161  }
162 
163  public function testDatetimeFromObject(): void
164  {
165  $helper = $this->getDataHelper();
166 
167  $this->assertSame(
168  '2013-01-20',
169  $helper->datetimeFromObject(new \DateTimeImmutable('2013-01-20'))
170  );
171  }
172 }
$datetime
$duration
datetimeToObject(string $datetime)
Translates strings in the LOM-internal datetime format to datetime objects.
Definition: DataHelper.php:71
durationToSeconds(string $duration)
Translates strings in the LOM-internal duration format to seconds.
Definition: DataHelper.php:66
datetimeFromObject(\DateTimeImmutable $object)
Translates datetime objects to strings in the LOM-internal datetime format.
Definition: DataHelper.php:94
durationFromIntegers(?int $years, ?int $months, ?int $days, ?int $hours, ?int $minutes, ?int $seconds)
Get a string in the LOM-internal duration format as specified by the provided integers.
Definition: DataHelper.php:76
__construct(InternalDataHelper $internal_helper, DataPresentation $data_presentation)
Definition: DataHelper.php:32
$r