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