ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
DataTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 
30 class DataTest extends TestCase
31 {
32  protected function getElementData(Type $type, string $value): ElementData
33  {
34  return new class ($type, $value) extends NullData {
35  protected Type $type;
36  protected string $value;
37 
38  public function __construct(Type $type, string $value)
39  {
40  $this->type = $type;
41  $this->value = $value;
42  }
43 
44  public function type(): Type
45  {
46  return $this->type;
47  }
48 
49  public function value(): string
50  {
51  return $this->value;
52  }
53  };
54  }
55 
56  protected function getData(): Data
57  {
58  $format = $this->createMock(DateFormat::class);
59  $format->method('applyTo')->willReturnCallback(function (\DateTimeImmutable $arg) {
60  return $arg->format('d:m:Y');
61  });
62 
63  $util = new class ($format) extends NullUtilities {
64  protected DateFormat $format;
65 
66  public function __construct(DateFormat $format)
67  {
68  $this->format = $format;
69  }
70 
71  public function getUserDateFormat(): DateFormat
72  {
73  return $this->format;
74  }
75 
76  public function txt(string $key): string
77  {
78  return 'translated ' . $key;
79  }
80  };
81 
82  $helper = new class () extends NullDataHelper {
83  public function durationToIterator(string $duration): \Generator
84  {
85  foreach (explode(':', $duration) as $v) {
86  if ($v === '') {
87  yield null;
88  } else {
89  yield $v;
90  }
91  }
92  }
93 
94  public function datetimeToObject(string $datetime): \DateTimeImmutable
95  {
96  return new \DateTimeImmutable($datetime);
97  }
98  };
99 
100  return new Data($util, $helper);
101  }
102 
103  public function testVocabularyValue(): void
104  {
105  $data = $this->getData();
106  $this->assertSame(
107  'translated meta_some_key',
108  $data->vocabularyValue('SomeKey')
109  );
110  $this->assertSame(
111  'translated meta_subjectmatterexpert',
112  $data->vocabularyValue('subjectMatterExpert')
113  );
114  }
115 
116  public function testLanguage(): void
117  {
118  $data = $this->getData();
119  $this->assertSame(
120  'translated meta_l_key',
121  $data->language('key')
122  );
123  }
124 
125  public function testDatetime(): void
126  {
127  $data = $this->getData();
128  $this->assertSame(
129  '31:12:2012',
130  $data->datetime('2012-12-31')
131  );
132  }
133 
134  public function testDuration(): void
135  {
136  $data = $this->getData();
137  $this->assertSame(
138  '89 translated years, 0 translated months, 1 translated second',
139  $data->duration('89:0::::1')
140  );
141  }
142 
143  public function testDataValue(): void
144  {
145  $data = $this->getData();
146  $this->assertSame(
147  'translated meta_some_key',
148  $data->dataValue($this->getElementData(Type::VOCAB_VALUE, 'SomeKey'))
149  );
150  $this->assertSame(
151  'translated meta_subjectmatterexpert',
152  $data->dataValue($this->getElementData(Type::VOCAB_VALUE, 'subjectMatterExpert'))
153  );
154  $this->assertSame(
155  'translated meta_l_key',
156  $data->dataValue($this->getElementData(Type::LANG, 'key'))
157  );
158  $this->assertSame(
159  '31:12:2012',
160  $data->dataValue($this->getElementData(Type::DATETIME, '2012-12-31'))
161  );
162  $this->assertSame(
163  '89 translated years, 5 translated months, 1 translated second',
164  $data->dataValue($this->getElementData(Type::DURATION, '89:5::::1'))
165  );
166  $this->assertSame(
167  'This should just go through.',
168  $data->dataValue($this->getElementData(Type::STRING, 'This should just go through.'))
169  );
170  }
171 }
getElementData(Type $type, string $value)
Definition: DataTest.php:32
$datetime
$duration
A Date Format provides a format definition akin to PHP&#39;s date formatting options, but stores the sing...
Definition: DateFormat.php:26
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:62
string $key
Consumer key/client ID value.
Definition: System.php:193