ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ChartBarTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/../../../Base.php");
23 
24 use ILIAS\UI\Component as C;
26 
31 {
32  protected function getFactory(): C\Chart\Bar\Factory
33  {
34  return new I\Component\Chart\Bar\Factory();
35  }
36 
37  public function getDataFactory(): ILIAS\Data\Factory
38  {
39  return new ILIAS\Data\Factory();
40  }
41 
42  public function getUIFactory(): NoUIFactory
43  {
44  return new class () extends NoUIFactory {
45  public function listing(): I\Component\Listing\Factory
46  {
47  return new I\Component\Listing\Factory();
48  }
49  };
50  }
51 
52  protected function getSimpleDataset(): \ILIAS\Data\Chart\Dataset
53  {
54  $df = $this->getDataFactory();
55 
56  $c_dimension = $df->dimension()->cardinal();
57 
58  $dataset = $df->dataset(["Dataset" => $c_dimension]);
59  $dataset = $dataset->withPoint("Item", ["Dataset" => 0]);
60 
61  return $dataset;
62  }
63 
64  protected function getExtendedDataset(): \ILIAS\Data\Chart\Dataset
65  {
66  $df = $this->getDataFactory();
67 
68  $c_dimension = $df->dimension()->cardinal();
69  $t_dimension = $df->dimension()->range($c_dimension);
70 
71  $dataset = $df->dataset(["Dataset 1" => $c_dimension, "Dataset 2" => $t_dimension]);
72  $dataset = $dataset->withPoint("Item 1", ["Dataset 1" => -1.25, "Dataset 2" => [-2, -1]]);
73  $dataset = $dataset->withPoint("Item 2", ["Dataset 1" => null, "Dataset 2" => [0, 0.5]]);
74 
75  return $dataset;
76  }
77 
78  public function testImplementsFactoryInterface(): void
79  {
80  $f = $this->getFactory();
81 
82  $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Factory", $f);
83  }
84 
85  public function testGetInstances(): void
86  {
87  $f = $this->getFactory();
88 
89  $dataset = $this->getSimpleDataset();
90 
91  $horizontal = $f->horizontal(
92  "Horizontal Bar",
93  $dataset
94  );
95 
96  $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Bar", $horizontal);
97  $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Horizontal", $horizontal);
98 
99  $vertical = $f->vertical(
100  "Vertical Bar",
101  $dataset
102  );
103  $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Bar", $vertical);
104  $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Vertical", $vertical);
105  }
106 
107  public function testEmptyDataset(): void
108  {
109  $f = $this->getFactory();
110  $df = $this->getDataFactory();
111 
112  $c_dimension = $df->dimension()->cardinal();
113 
114  $dataset = $df->dataset(["Dataset" => $c_dimension]);
115 
116  $this->expectException(LogicException::class);
117  $this->expectExceptionMessage("Dataset must not be empty.");
118  $horizontal = $f->horizontal(
119  "Horizontal Bar",
120  $dataset
121  );
122  }
123 
124  // This test can only be performed when there will be a Dimension in the future, which is not compatible with Bar Charts
125  /*
126  public function testInvalidDatasetDimension() : void
127  {
128  $f = $this->getFactory();
129  $df = $this->getDataFactory();
130 
131  $o_dimension = $df->dimension()->ordinal();
132 
133  $dataset = $df->dataset(["Dataset" => $o_dimension]);
134 
135  $this->expectException(InvalidArgumentException::class);
136  $this->expectExceptionMessage("Expected parameter to be a CardinalDimension or RangeDimension.");
137  $horizontal = $f->horizontal(
138  "Horizontal Bar",
139  $dataset
140  );
141  }
142  */
143 
144  public function testWithTitle(): void
145  {
146  $f = $this->getFactory();
147 
148  $dataset = $this->getSimpleDataset();
149 
150  $horizontal = $f->horizontal(
151  "Horizontal Bar",
152  $dataset
153  );
154  $horizontal1 = $horizontal->withTitle("Alternative title for Horizontal Bar");
155 
156  $this->assertEquals("Horizontal Bar", $horizontal->getTitle());
157  $this->assertEquals("Alternative title for Horizontal Bar", $horizontal1->getTitle());
158  }
159 
160  public function testWithTitleInvisible(): void
161  {
162  $f = $this->getFactory();
163 
164  $dataset = $this->getSimpleDataset();
165 
166  $horizontal = $f->horizontal(
167  "Horizontal Bar",
168  $dataset
169  );
170  $horizontal1 = $horizontal->withTitleVisible(false);
171 
172  $this->assertEquals(true, $horizontal->isTitleVisible());
173  $this->assertEquals(false, $horizontal1->isTitleVisible());
174  }
175 
176  public function testWithTooltipsInvisible(): void
177  {
178  $f = $this->getFactory();
179 
180  $dataset = $this->getSimpleDataset();
181 
182  $horizontal = $f->horizontal(
183  "Horizontal Bar",
184  $dataset
185  );
186  $horizontal1 = $horizontal->withTooltipsVisible(false);
187 
188  $this->assertEquals(true, $horizontal->isTooltipsVisible());
189  $this->assertEquals(false, $horizontal1->isTooltipsVisible());
190  }
191 
192  public function testWithLegendInvisible(): void
193  {
194  $f = $this->getFactory();
195 
196  $dataset = $this->getSimpleDataset();
197 
198  $horizontal = $f->horizontal(
199  "Horizontal Bar",
200  $dataset
201  );
202  $horizontal1 = $horizontal->withLegendVisible(false);
203 
204  $this->assertEquals(true, $horizontal->isLegendVisible());
205  $this->assertEquals(false, $horizontal1->isLegendVisible());
206  }
207 
208  public function testWithLegendPosition(): void
209  {
210  $f = $this->getFactory();
211 
212  $dataset = $this->getSimpleDataset();
213 
214  $horizontal = $f->horizontal(
215  "Horizontal Bar",
216  $dataset
217  );
218  $horizontal1 = $horizontal->withLegendPosition("left");
219 
220  $this->assertEquals("top", $horizontal->getLegendPosition());
221  $this->assertEquals("left", $horizontal1->getLegendPosition());
222  }
223 
224  public function testWithInvalidLegendPosition(): void
225  {
226  $f = $this->getFactory();
227 
228  $dataset = $this->getSimpleDataset();
229 
230  $horizontal = $f->horizontal(
231  "Horizontal Bar",
232  $dataset
233  );
234 
235  $this->expectException(InvalidArgumentException::class);
236  $this->expectExceptionMessage("Position must be 'bottom', 'top', 'left' or 'right'.");
237 
238  $horizontal = $horizontal->withLegendPosition("middle");
239  }
240 
241  public function testWithDataset(): void
242  {
243  $f = $this->getFactory();
244 
245  $s_dataset = $this->getSimpleDataset();
246  $e_dataset = $this->getExtendedDataset();
247 
248  $horizontal = $f->horizontal(
249  "Horizontal Bar",
250  $s_dataset
251  );
252  $horizontal1 = $horizontal->withDataset($e_dataset);
253 
254  $this->assertEquals($s_dataset, $horizontal->getDataset());
255  $this->assertEquals($e_dataset, $horizontal1->getDataset());
256  }
257 
258  public function testWithBarConfigs(): void
259  {
260  $f = $this->getFactory();
261  $df = $this->getDataFactory();
262 
263  $dataset = $this->getSimpleDataset();
264 
265  $bc = new C\Chart\Bar\BarConfig();
266  $bc = $bc->withColor($df->color("#d38000"));
267 
268  $bars = [
269  "Dataset" => $bc,
270  ];
271 
272  $horizontal = $f->horizontal(
273  "Horizontal Bar",
274  $dataset
275  );
276  $horizontal1 = $horizontal->withBarConfigs($bars);
277 
278  $this->assertEquals([], $horizontal->getBarConfigs());
279  $this->assertEquals($bars, $horizontal1->getBarConfigs());
280  }
281 
282  public function testWithGroupConfigs(): void
283  {
284  $f = $this->getFactory();
285  $df = $this->getDataFactory();
286 
287  $dataset = $this->getSimpleDataset();
288 
289  $gc = new C\Chart\Bar\GroupConfig();
290  $gc = $gc->withStacked();
291 
292  $groups = [
293  "Group" => $gc,
294  ];
295 
296  $horizontal = $f->horizontal(
297  "Horizontal Bar",
298  $dataset
299  );
300  $horizontal1 = $horizontal->withGroupConfigs($groups);
301 
302  $this->assertEquals([], $horizontal->getGroupConfigs());
303  $this->assertEquals($groups, $horizontal1->getGroupConfigs());
304  }
305 
306  public function testIndexAxis(): void
307  {
308  $f = $this->getFactory();
309  $df = $this->getDataFactory();
310 
311  $dataset = $this->getSimpleDataset();
312 
313  $horizontal = $f->horizontal(
314  "Horizontal Bar",
315  $dataset
316  );
317 
318  $this->assertEquals("y", $horizontal->getIndexAxis());
319 
320  $vertical = $f->vertical(
321  "Vertical Bar",
322  $dataset
323  );
324 
325  $this->assertEquals("x", $vertical->getIndexAxis());
326  }
327 
328  public function testRenderHorizontal(): void
329  {
330  $r = $this->getDefaultRenderer();
331  $f = $this->getFactory();
332 
333  $dataset = $this->getSimpleDataset();
334 
335  $horizontal = $f->horizontal(
336  "bar123",
337  $dataset
338  );
339 
340  $html = $r->render($horizontal);
341 
342  $expected_html = <<<EOT
343 <div class="il-chart-bar-horizontal">
344  <canvas id="id_1" height="150px" aria-label="bar123" role="img"></canvas>
345 </div>
346 <div class="sr-only">
347  <dl>
348  <dt>Dataset</dt>
349  <dd>
350  <ul>
351  <li>Item: 0</li>
352  </ul>
353  </dd>
354  </dl>
355 </div>
356 EOT;
357 
358  $this->assertHTMLEquals("<div>" . $expected_html . "</div>", "<div>" . $html . "</div>");
359  }
360 
361  public function testRenderVertical(): void
362  {
363  $r = $this->getDefaultRenderer();
364  $f = $this->getFactory();
365 
366  $dataset = $this->getExtendedDataset();
367 
368  $vertical = $f->vertical(
369  "bar123",
370  $dataset
371  );
372 
373  $html = $r->render($vertical);
374 
375  $expected_html = <<<EOT
376 <div class="il-chart-bar-vertical">
377  <canvas id="id_1" height="165px" aria-label="bar123" role="img"></canvas>
378 </div>
379 <div class="sr-only">
380  <dl>
381  <dt>Dataset 1</dt>
382  <dd>
383  <ul>
384  <li>Item 1: -1.25</li>
385  <li>Item 2: -</li>
386  </ul>
387  </dd>
388  <dt>Dataset 2</dt>
389  <dd>
390  <ul>
391  <li>Item 1: -2 - -1</li>
392  <li>Item 2: 0 - 0.5</li>
393  </ul>
394  </dd>
395  </dl>
396 </div>
397 EOT;
398 
399  $this->assertHTMLEquals("<div>" . $expected_html . "</div>", "<div>" . $html . "</div>");
400  }
401 }
testImplementsFactoryInterface()
Test on Bar Chart implementation.
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testWithTooltipsInvisible()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testWithInvalidLegendPosition()
$r