ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DictionaryTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
42 
43 class DictionaryTest extends TestCase
44 {
45  protected function getTagAssignment(
46  int $path_id,
47  string $tag_name,
48  int ...$indices
50  return new class ($path_id, $tag_name, $indices) extends NullTagAssignment {
51  public function __construct(
52  protected int $path_id,
53  protected string $tag_name,
54  protected array $indices
55  ) {
56  }
57 
58  public function tag(): TagInterface
59  {
60  return new class ($this->tag_name, $this->indices) extends NullTag {
61  public function __construct(
62  protected string $name,
63  protected array $indices
64  ) {
65  }
66 
67  public function name()
68  {
69  return $this->name;
70  }
71 
72  public function indices(): \Generator
73  {
74  yield from $this->indices;
75  }
76 
77  public function isRestrictedToIndices(): bool
78  {
79  return !empty($this->indices);
80  }
81  };
82  }
83 
84  public function matchesPath(PathInterface $path): bool
85  {
86  return (string) $this->path_id === $path->toString();
87  }
88  };
89  }
90 
91  protected function getDictionary(): Dictionary
92  {
93  $tag_assignments = [
94  $this->getTagAssignment(0, 'tag 0'),
95  $this->getTagAssignment(0, 'tag 0 index 0', 0),
96  $this->getTagAssignment(0, 'tag 0 index 3', 3),
97  $this->getTagAssignment(1, 'tag 1 index 1', 1),
98  $this->getTagAssignment(1, 'tag 1 index -2', -2),
99  $this->getTagAssignment(1, 'tag 1 index 0 and 1', 0, 1),
100  $this->getTagAssignment(1, 'tag 1 index -3 and 1', -3, 1),
101  $this->getTagAssignment(2, 'tag 2'),
102  $this->getTagAssignment(2, 'tag 2 number 2')
103  ];
104 
105  $path_factory = new class () extends NullFactory {
106  public function toElement(
108  bool $leads_to_exactly_one = false
109  ): PathInterface {
110  return new class ($to->getMDID()) extends NullPath {
111  public function __construct(protected int $id)
112  {
113  }
114 
115  public function toString(): string
116  {
117  return (string) $this->id;
118  }
119  };
120  }
121  };
122 
123  $navigator_factory = new class () extends NullNavigatorFactory {
124  public function navigator(
126  ElementInterface $start_element
127  ): NavigatorInterface {
128  return new class ($path, $start_element) extends NullNavigator {
129  protected ElementInterface $start_element;
130 
131  public function __construct(
132  protected PathInterface $path,
133  ElementInterface $start_element
134  ) {
135  if ($start_element->getMDID() === 0) {
136  $this->start_element = $start_element;
137  } else {
138  $this->start_element = $start_element->getSuperElement();
139  }
140  }
141 
142  public function elementsAtFinalStep(): \Generator
143  {
144  if ($this->path->toString() === '0') {
145  yield $this->start_element;
146  return;
147  }
148 
149  foreach ($this->start_element->getSubElements() as $sub) {
150  if ((string) $sub->getMDID() === $this->path->toString()) {
151  yield $sub;
152  }
153  }
154  }
155  };
156  }
157  };
158 
159  return new class ($path_factory, $navigator_factory, ...$tag_assignments) extends Dictionary {
160  public function __construct(
161  PathFactoryInterface $path_factory,
162  NavigatorFactoryInterface $navigator_factory,
163  TagAssignmentInterface ...$tag_assignments
164  ) {
165  parent::__construct($path_factory, $navigator_factory, ...$tag_assignments);
166  }
167  };
168  }
169 
170  protected function getElement(
171  int $id,
172  ElementInterface ...$subs
173  ): ElementInterface {
174  return new class ($id, $subs) extends NullElement {
175  protected ?ElementInterface $super = null;
176  public function __construct(
177  protected int $id,
178  protected array $subs
179  ) {
180  }
181 
182  public function getDefinition(): DefinitionInterface
183  {
184  return new class ($this->id) extends NullDefinition {
185  public function __construct(protected int $id)
186  {
187  }
188 
189  public function name(): string
190  {
191  return (string) $this->id;
192  }
193  };
194  }
195 
196  public function getSubElements(): \Generator
197  {
198  yield from $this->subs;
199  }
200 
201  public function getSuperElement(): ?ElementInterface
202  {
203  return $this->super;
204  }
205 
206  public function setSuperElement(ElementInterface $super): void
207  {
208  $this->super = $super;
209  }
210 
211  public function getMDID(): int|NoID
212  {
213  return $this->id;
214  }
215  };
216  }
217 
218  protected function getElements(): array
219  {
220  $el2 = $this->getElement(2);
221  $el10 = $this->getElement(1);
222  $el11 = $this->getElement(1);
223  $el12 = $this->getElement(1);
224  $el3 = $this->getElement(3);
225  $el0 = $this->getElement(0, $el2, $el10, $el11, $el12, $el3);
226 
227  $el2->setSuperElement($el0);
228  $el10->setSuperElement($el0);
229  $el11->setSuperElement($el0);
230  $el12->setSuperElement($el0);
231  $el3->setSuperElement($el0);
232 
233  return [
234  'el0' => $el0,
235  'el2' => $el2,
236  'el10' => $el10,
237  'el11' => $el11,
238  'el12' => $el12,
239  'el3' => $el3
240  ];
241  }
242 
243  public function testTagForElement(): void
244  {
245  $dict = $this->getDictionary();
246  $tags = $dict->tagsForElement($this->getElements()['el2']);
247 
248  $this->assertSame(
249  'tag 2',
250  $tags->current()->name()
251  );
252  $tags->next();
253  $this->assertSame(
254  'tag 2 number 2',
255  $tags->current()->name()
256  );
257  $tags->next();
258  $this->assertNull($tags->current());
259 
260  $tags = $dict->tagsForElement($this->getElements()['el3']);
261  $this->assertNull($tags->current());
262  }
263 
264  public function testTagForElementWithIndices(): void
265  {
266  $dict = $this->getDictionary();
267  $els = $this->getElements();
268  $tags10 = $dict->tagsForElement($this->getElements()['el10']);
269  $tags11 = $dict->tagsForElement($this->getElements()['el11']);
270  $tags12 = $dict->tagsForElement($this->getElements()['el12']);
271 
272  $this->assertSame(
273  'tag 1 index 0 and 1',
274  $tags10->current()->name()
275  );
276  $tags10->next();
277  $this->assertNull($tags10->current());
278 
279  $this->assertSame(
280  'tag 1 index 1',
281  $tags11->current()->name()
282  );
283  $tags11->next();
284  $this->assertSame(
285  'tag 1 index 0 and 1',
286  $tags11->current()->name()
287  );
288  $tags11->next();
289  $this->assertSame(
290  'tag 1 index -3 and 1',
291  $tags11->current()->name()
292  );
293  $tags11->next();
294  $this->assertNull($tags11->current());
295 
296  $this->assertNull($tags12->current());
297  }
298 
299  public function testTagForElementAtRoot(): void
300  {
301  $dict = $this->getDictionary();
302  $tags = $dict->tagsForElement($this->getElements()['el0']);
303 
304  $this->assertSame(
305  'tag 0',
306  $tags->current()->name()
307  );
308  $tags->next();
309  $this->assertSame(
310  'tag 0 index 0',
311  $tags->current()->name()
312  );
313  $tags->next();
314  $this->assertNull($tags->current());
315  }
316 }
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
getTagAssignment(int $path_id, string $tag_name, int ... $indices)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(Container $dic, ilPlugin $plugin)
getElement(int $id, ElementInterface ... $subs)