ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DataTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
34 
35 class DataTest extends TestCase
36 {
37  protected function getElementData(
38  Type $type,
39  string $value,
40  SlotIdentifier $vocab_slot = SlotIdentifier::NULL
41  ): ElementData {
42  return new class ($type, $value, $vocab_slot) extends NullData {
43  public function __construct(
44  protected Type $type,
45  protected string $value,
46  protected SlotIdentifier $vocab_slot
47  ) {
48  $this->type = $type;
49  $this->value = $value;
50  $this->vocab_slot = $vocab_slot;
51  }
52 
53  public function type(): Type
54  {
55  return $this->type;
56  }
57 
58  public function value(): string
59  {
60  return $this->value;
61  }
62 
63  public function vocabularySlot(): SlotIdentifier
64  {
65  return $this->vocab_slot;
66  }
67  };
68  }
69 
70  protected function getData(): Data
71  {
72  $format = $this->createMock(DateFormat::class);
73  $format->method('applyTo')->willReturnCallback(function (\DateTimeImmutable $arg) {
74  return $arg->format('d:m:Y');
75  });
76 
77  $util = new class ($format) extends NullUtilities {
78  protected DateFormat $format;
79 
80  public function __construct(DateFormat $format)
81  {
82  $this->format = $format;
83  }
84 
85  public function getUserDateFormat(): DateFormat
86  {
87  return $this->format;
88  }
89 
90  public function txt(string $key): string
91  {
92  return 'translated ' . $key;
93  }
94  };
95 
96  $helper = new class () extends NullDataHelper {
97  public function durationToIterator(string $duration): \Generator
98  {
99  foreach (explode(':', $duration) as $v) {
100  if ($v === '') {
101  yield null;
102  } else {
103  yield $v;
104  }
105  }
106  }
107 
108  public function datetimeToObject(string $datetime): \DateTimeImmutable
109  {
110  return new \DateTimeImmutable($datetime);
111  }
112  };
113 
114  $vocab_presentation = new class () extends NullVocabulariesPresentation {
115  public function presentableLabels(
116  PresentationUtilities $presentation_utilities,
117  SlotIdentifier $slot,
118  bool $with_unknown_vocab_flag,
119  string ...$values
120  ): \Generator {
121  foreach ($values as $value) {
122  yield new class ($value, $slot, $with_unknown_vocab_flag) extends NullLabelledValue {
123  public function __construct(
124  protected string $value,
125  protected SlotIdentifier $slot,
126  protected bool $with_unknown_vocab_flag
127  ) {
128  }
129 
130  public function value(): string
131  {
132  return $this->value;
133  }
134 
135  public function label(): string
136  {
137  return 'vocab ' . $this->slot->value . ' ' .
138  $this->value . ($this->with_unknown_vocab_flag ? ' flagged' : '');
139  }
140  };
141  }
142  }
143  };
144 
145  return new Data($util, $helper, $vocab_presentation);
146  }
147 
148  public function testVocabularyValue(): void
149  {
150  $data = $this->getData();
151  $this->assertSame(
152  'vocab rights_cost SomeKey',
153  $data->vocabularyValue('SomeKey', SlotIdentifier::RIGHTS_COST),
154  );
155  }
156 
157  public function testLanguage(): void
158  {
159  $data = $this->getData();
160  $this->assertSame(
161  'translated meta_l_key',
162  $data->language('key')
163  );
164  }
165 
166  public function testDatetime(): void
167  {
168  $data = $this->getData();
169  $this->assertSame(
170  '31:12:2012',
171  $data->datetime('2012-12-31')
172  );
173  }
174 
175  public function testDuration(): void
176  {
177  $data = $this->getData();
178  $this->assertSame(
179  '89 translated years, 0 translated months, 1 translated second',
180  $data->duration('89:0::::1')
181  );
182  }
183 
184  public function testDataValue(): void
185  {
186  $data = $this->getData();
187  $this->assertSame(
188  'vocab rights_cost SomeKey',
189  $data->dataValue($this->getElementData(
190  Type::VOCAB_VALUE,
191  'SomeKey',
192  SlotIdentifier::RIGHTS_COST
193  ))
194  );
195  $this->assertSame(
196  'vocab rights_cost SomeKey',
197  $data->dataValue($this->getElementData(
198  Type::STRING,
199  'SomeKey',
200  SlotIdentifier::RIGHTS_COST
201  ))
202  );
203  $this->assertSame(
204  'translated meta_l_key',
205  $data->dataValue($this->getElementData(Type::LANG, 'key'))
206  );
207  $this->assertSame(
208  '31:12:2012',
209  $data->dataValue($this->getElementData(Type::DATETIME, '2012-12-31'))
210  );
211  $this->assertSame(
212  '89 translated years, 5 translated months, 1 translated second',
213  $data->dataValue($this->getElementData(Type::DURATION, '89:5::::1'))
214  );
215  $this->assertSame(
216  'This should just go through.',
217  $data->dataValue($this->getElementData(Type::VOCAB_SOURCE, 'This should just go through.'))
218  );
219  }
220 }
getElementData(Type $type, string $value, SlotIdentifier $vocab_slot=SlotIdentifier::NULL)
Definition: DataTest.php:37
$datetime
$duration
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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:76