ILIAS  release_8 Revision v8.24
ChartBarTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/../../../../../libs/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 }
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 test_implements_factory_interface(): void
79 {
80 $f = $this->getFactory();
81
82 $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Factory", $f);
83 }
84
85 public function test_get_instances(): 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 test_empty_dataset(): 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 /*
125 public function test_invalid_dataset_dimension() : void
126 {
127 $f = $this->getFactory();
128 $df = $this->getDataFactory();
129
130 $o_dimension = $df->dimension()->ordinal();
131
132 $dataset = $df->dataset(["Dataset" => $o_dimension]);
133
134 $this->expectException(InvalidArgumentException::class);
135 $this->expectExceptionMessage("Expected parameter to be a CardinalDimension or RangeDimension.");
136 $horizontal = $f->horizontal(
137 "Horizontal Bar",
138 $dataset
139 );
140 }
141 */
142
143 public function test_with_title(): void
144 {
145 $f = $this->getFactory();
146
147 $dataset = $this->getSimpleDataset();
148
149 $horizontal = $f->horizontal(
150 "Horizontal Bar",
151 $dataset
152 );
153 $horizontal1 = $horizontal->withTitle("Alternative title for Horizontal Bar");
154
155 $this->assertEquals("Horizontal Bar", $horizontal->getTitle());
156 $this->assertEquals("Alternative title for Horizontal Bar", $horizontal1->getTitle());
157 }
158
159 public function test_with_title_invisible(): void
160 {
161 $f = $this->getFactory();
162
163 $dataset = $this->getSimpleDataset();
164
165 $horizontal = $f->horizontal(
166 "Horizontal Bar",
167 $dataset
168 );
169 $horizontal1 = $horizontal->withTitleVisible(false);
170
171 $this->assertEquals(true, $horizontal->isTitleVisible());
172 $this->assertEquals(false, $horizontal1->isTitleVisible());
173 }
174
175 public function test_with_tooltips_invisible(): void
176 {
177 $f = $this->getFactory();
178
179 $dataset = $this->getSimpleDataset();
180
181 $horizontal = $f->horizontal(
182 "Horizontal Bar",
183 $dataset
184 );
185 $horizontal1 = $horizontal->withTooltipsVisible(false);
186
187 $this->assertEquals(true, $horizontal->isTooltipsVisible());
188 $this->assertEquals(false, $horizontal1->isTooltipsVisible());
189 }
190
191 public function test_with_legend_invisible(): void
192 {
193 $f = $this->getFactory();
194
195 $dataset = $this->getSimpleDataset();
196
197 $horizontal = $f->horizontal(
198 "Horizontal Bar",
199 $dataset
200 );
201 $horizontal1 = $horizontal->withLegendVisible(false);
202
203 $this->assertEquals(true, $horizontal->isLegendVisible());
204 $this->assertEquals(false, $horizontal1->isLegendVisible());
205 }
206
207 public function test_with_legend_position(): void
208 {
209 $f = $this->getFactory();
210
211 $dataset = $this->getSimpleDataset();
212
213 $horizontal = $f->horizontal(
214 "Horizontal Bar",
215 $dataset
216 );
217 $horizontal1 = $horizontal->withLegendPosition("left");
218
219 $this->assertEquals("top", $horizontal->getLegendPosition());
220 $this->assertEquals("left", $horizontal1->getLegendPosition());
221 }
222
223 public function test_with_invalid_legend_position(): void
224 {
225 $f = $this->getFactory();
226
227 $dataset = $this->getSimpleDataset();
228
229 $horizontal = $f->horizontal(
230 "Horizontal Bar",
231 $dataset
232 );
233
234 $this->expectException(InvalidArgumentException::class);
235 $this->expectExceptionMessage("Position must be 'bottom', 'top', 'left' or 'right'.");
236
237 $horizontal = $horizontal->withLegendPosition("middle");
238 }
239
240 public function test_with_dataset(): void
241 {
242 $f = $this->getFactory();
243
244 $s_dataset = $this->getSimpleDataset();
245 $e_dataset = $this->getExtendedDataset();
246
247 $horizontal = $f->horizontal(
248 "Horizontal Bar",
249 $s_dataset
250 );
251 $horizontal1 = $horizontal->withDataset($e_dataset);
252
253 $this->assertEquals($s_dataset, $horizontal->getDataset());
254 $this->assertEquals($e_dataset, $horizontal1->getDataset());
255 }
256
257 public function test_with_bar_configs(): void
258 {
259 $f = $this->getFactory();
260 $df = $this->getDataFactory();
261
262 $dataset = $this->getSimpleDataset();
263
264 $bc = new C\Chart\Bar\BarConfig();
265 $bc = $bc->withColor($df->color("#d38000"));
266
267 $bars = [
268 "Dataset" => $bc,
269 ];
270
271 $horizontal = $f->horizontal(
272 "Horizontal Bar",
273 $dataset
274 );
275 $horizontal1 = $horizontal->withBarConfigs($bars);
276
277 $this->assertEquals([], $horizontal->getBarConfigs());
278 $this->assertEquals($bars, $horizontal1->getBarConfigs());
279 }
280
281 public function test_index_axis(): void
282 {
283 $f = $this->getFactory();
284 $df = $this->getDataFactory();
285
286 $dataset = $this->getSimpleDataset();
287
288 $horizontal = $f->horizontal(
289 "Horizontal Bar",
290 $dataset
291 );
292
293 $this->assertEquals("y", $horizontal->getIndexAxis());
294
295 $vertical = $f->vertical(
296 "Vertical Bar",
297 $dataset
298 );
299
300 $this->assertEquals("x", $vertical->getIndexAxis());
301 }
302
303 public function test_render_horizontal(): void
304 {
305 $r = $this->getDefaultRenderer();
306 $f = $this->getFactory();
307
308 $dataset = $this->getSimpleDataset();
309
310 $horizontal = $f->horizontal(
311 "bar123",
312 $dataset
313 );
314
315 $html = $r->render($horizontal);
316
317 $expected_html = <<<EOT
318<div class="il-chart-bar-horizontal">
319 <canvas id="id_1" height="150px" aria-label="bar123" role="img"></canvas>
320</div>
321<div class="sr-only">
322 <dl>
323 <dt>Dataset</dt>
324 <dd>
325 <ul>
326 <li>Item: 0</li>
327 </ul>
328 </dd>
329 </dl>
330</div>
331EOT;
332
333 $this->assertHTMLEquals("<div>" . $expected_html . "</div>", "<div>" . $html . "</div>");
334 }
335
336 public function test_render_vertical(): void
337 {
338 $r = $this->getDefaultRenderer();
339 $f = $this->getFactory();
340
341 $dataset = $this->getExtendedDataset();
342
343 $vertical = $f->vertical(
344 "bar123",
345 $dataset
346 );
347
348 $html = $r->render($vertical);
349
350 $expected_html = <<<EOT
351<div class="il-chart-bar-vertical">
352 <canvas id="id_1" height="165px" aria-label="bar123" role="img"></canvas>
353</div>
354<div class="sr-only">
355 <dl>
356 <dt>Dataset 1</dt>
357 <dd>
358 <ul>
359 <li>Item 1: -1.25</li>
360 <li>Item 2: -</li>
361 </ul>
362 </dd>
363 <dt>Dataset 2</dt>
364 <dd>
365 <ul>
366 <li>Item 1: -2 - -1</li>
367 <li>Item 2: 0 - 0.5</li>
368 </ul>
369 </dd>
370 </dl>
371</div>
372EOT;
373
374 $this->assertHTMLEquals("<div>" . $expected_html . "</div>", "<div>" . $html . "</div>");
375 }
376}
Test on Bar Chart implementation.
test_with_tooltips_invisible()
test_with_legend_position()
test_implements_factory_interface()
test_with_title_invisible()
test_with_legend_invisible()
test_with_invalid_legend_position()
Builds data types.
Definition: Factory.php:21
Provides common functionality for UI tests.
Definition: Base.php:299
assertHTMLEquals(string $expected_html_as_string, string $html_as_string)
Definition: Base.php:427
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:355
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider \MainMenu\Provider.