ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ComponentHelperTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
22use ILIAS\UI\Implementation\Component\ComponentHelper;
25
26require_once("vendor/composer/vendor/autoload.php");
27
28require_once(__DIR__ . "/../Renderer/TestComponent.php");
29
30class ComponentMock implements Component
31{
32 use ComponentHelper;
33
34 public function _checkArg(string $which, bool $check, string $message): void
35 {
36 $this->checkArg($which, $check, $message);
37 }
38
39 public function _checkStringArg(string $which, $value): void
40 {
41 $this->checkStringArg($which, $value);
42 }
43
44 public function _checkBoolArg(string $which, $value): void
45 {
46 $this->checkBoolArg($which, $value);
47 }
48
49 public function _checkArgInstanceOf(string $which, $value, string $class): void
50 {
51 $this->checkArgInstanceOf($which, $value, $class);
52 }
53
54 public function _checkArgIsElement(string $which, $value, array $array, string $name): void
55 {
56 $this->checkArgIsElement($which, $value, $array, $name);
57 }
58
59 public function _toArray($value): array
60 {
61 return $this->toArray($value);
62 }
63
64 public function _checkArgListElements(string $which, array &$value, $classes): void
65 {
66 $this->checkArgListElements($which, $value, $classes);
67 }
68
69 public function _checkArgList(string $which, array &$value, Closure $check, Closure $message): void
70 {
71 $this->checkArgList($which, $value, $check, $message);
72 }
73
74 public $sub_components = null;
76
77 public function getSubComponents(): ?array
78 {
80 }
81}
82
83class Class1
84{
85}
86
87class Class2
88{
89}
90
91class Class3
92{
93}
94
98class ComponentHelperTest extends TestCase
99{
101
102 public function setUp(): void
103 {
104 $this->mock = new ComponentMock();
105 }
106
107 public function testGetCanonicalName(): void
108 {
109 $c = new TestComponent("foo");
110 $this->assertEquals("Test Component Test", $c->getCanonicalName());
111 }
112
113 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
114 public function testCheckArgOk(): void
115 {
116 $this->mock->_checkArg("some_arg", true, "some message");
117 }
118
119 public function testCheckArgNotOk(): void
120 {
121 $this->expectException(InvalidArgumentException::class);
122 $this->expectExceptionMessage("Argument 'some_arg': some message");
123 $this->mock->_checkArg("some_arg", false, "some message");
124 }
125
126 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
127 public function testCheckStringArgOk(): void
128 {
129 $this->mock->_checkStringArg("some_arg", "bar");
130 }
131
132 public function testCheckStringArgNotOk(): void
133 {
134 $this->expectException(InvalidArgumentException::class);
135 $this->expectExceptionMessage("Argument 'some_arg': expected string, got integer '1'");
136 $this->mock->_checkStringArg("some_arg", 1);
137 }
138
139 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
140 public function testCheckBoolArgOk(): void
141 {
142 $this->mock->_checkBoolArg("some_arg", true);
143 }
144
145 public function testCheckBoolArgNotOk(): void
146 {
147 $this->expectException(InvalidArgumentException::class);
148 $this->expectExceptionMessage("Argument 'some_arg': expected bool, got integer '1'");
149 $this->mock->_checkBoolArg("some_arg", 1);
150 }
151
152 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
153 public function testCheckArgInstanceofOk(): void
154 {
155 $this->mock->_checkArgInstanceOf("some_arg", $this->mock, ComponentMock::class);
156 }
157
158 public function testCheckArgInstanceofNotOk(): void
159 {
160 $this->expectException(InvalidArgumentException::class);
161 $this->expectExceptionMessage("Argument 'some_arg': expected ComponentMock, got ComponentHelperTest");
162 $this->mock->_checkArgInstanceOf("some_arg", $this, ComponentMock::class);
163 }
164
165
166 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
167 public function testCheckArgIsElementOk(): void
168 {
169 $this->mock->_checkArgIsElement("some_arg", "bar", array("foo", "bar"), "foobar");
170 }
171
172 public function testCheckStringArgIsElementNotOk(): void
173 {
174 $this->expectException(InvalidArgumentException::class);
175 $this->expectExceptionMessage("Argument 'some_arg': expected foobar, got 'baz'");
176 $this->mock->_checkArgIsElement("some_arg", "baz", array("foo", "bar"), "foobar");
177 }
178
179 public function testToArrayWithArray(): void
180 {
181 $foo = array("foo", "bar");
182 $res = $this->mock->_toArray($foo);
183
184 $this->assertEquals($foo, $res);
185 }
186
187 public function testToArrayWithInt(): void
188 {
189 $foo = 1;
190 $res = $this->mock->_toArray($foo);
191 $this->assertEquals(array($foo), $res);
192 }
193
194 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
195 public function testCheckArgListElementsOk(): void
196 {
197 $l = array(new Class1(), new Class1(), new Class1());
198 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
199 }
200
201 public function testCheckArgListElementsNoOk(): void
202 {
203 $this->expectException(InvalidArgumentException::class);
204 $this->expectExceptionMessage("Argument 'some_arg': expected Class1, got Class2");
205 $l = array(new Class1(), new Class1(), new Class2());
206 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
207 }
208
209 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
211 {
212 $l = array(new Class1(), new Class2(), new Class1());
213 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
214 }
215
217 {
218 $this->expectException(InvalidArgumentException::class);
219 $this->expectExceptionMessage("Argument 'some_arg': expected Class1, Class2, got Class3");
220 $l = array(new Class1(), new Class2(), new Class3(), new Class2());
221 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
222 }
223
224 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
226 {
227 $l = array(1, "foo");
228 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
229 }
230
232 {
233 $this->expectException(InvalidArgumentException::class);
234 $this->expectExceptionMessage("Argument 'some_arg': expected string, int, got Class1");
235 $l = array(1, new Class1());
236 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
237 }
238
239 #[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
240 public function testCheckArgListOk(): void
241 {
242 $l = array("a" => 1, "b" => 2, "c" => 3);
243 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
244 return is_string($k) && is_int($v);
245 }, function ($k, $v) {
246 return "expected keys of type string and integer values, got ($k => $v)";
247 });
248 }
249
250 public function testCheckArgListNotOk1(): void
251 {
252 $m = "expected keys of type string and integer values, got (4 => 3)";
253 $this->expectException(InvalidArgumentException::class);
254 $this->expectExceptionMessage("Argument 'some_arg': $m");
255 $l = array("a" => 1, "b" => 2, 4 => 3);
256 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
257 return is_string($k) && is_int($v);
258 }, function ($k, $v) {
259 return "expected keys of type string and integer values, got ($k => $v)";
260 });
261 }
262
263 public function testCheckArgListNotOk2(): void
264 {
265 $m = "expected keys of type string and integer values, got (c => d)";
266 $this->expectException(InvalidArgumentException::class);
267 $this->expectExceptionMessage("Argument 'some_arg': $m");
268 $l = array("a" => 1, "b" => 2, "c" => "d");
269 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
270 return is_string($k) && is_int($v);
271 }, function ($k, $v) {
272 return "expected keys of type string and integer values, got ($k => $v)";
273 });
274 }
275
276 public function testReduceWith()
277 {
278 $a = new ComponentMock();
279 $a->random_data = "A";
280 $b = new ComponentMock();
281 $b->random_data = "B";
282 $c = new ComponentMock();
283 $c->random_data = "C";
284 $c->sub_components = [$a, $b];
285
286 $f = fn($c, $res) => [$c->random_data => $res];
287 $res = $c->reduceWith($f);
288
289 $this->assertEquals(
290 ["C" =>
291 [
292 ["A" => []],
293 ["B" => []],
294 ]
295 ],
296 $res
297 );
298 }
299
301 {
302 $a = new ComponentMock();
303 $a->random_data = "A";
304 $b = new ComponentMock();
305 $b->random_data = "B";
306 $c = new ComponentMock();
307 $c->random_data = "C";
308 $c->sub_components = [$a, $b];
309
310 $f = function ($c, $res) {
311 $clone = clone $c;
312 $clone->random_data = strtolower($c->random_data);
313 $clone->sub_components = $res;
314 return $clone;
315 };
316 $c2 = $c->reduceWith($f);
317
318 [$a2, $b2] = $c2->sub_components;
319
320 $this->assertNotEquals(spl_object_id($a), spl_object_id($a2));
321 $this->assertNotEquals(spl_object_id($b), spl_object_id($b2));
322 $this->assertNotEquals(spl_object_id($c), spl_object_id($c2));
323
324 $this->assertEquals("A", $a->random_data);
325 $this->assertEquals("B", $b->random_data);
326 $this->assertEquals("C", $c->random_data);
327 $this->assertEquals([$a, $b], $c->sub_components);
328
329 $this->assertEquals("a", $a2->random_data);
330 $this->assertEquals("b", $b2->random_data);
331 $this->assertEquals("c", $c2->random_data);
332 }
333
335 {
336 $a = new ComponentMock();
337 $a->random_data = "A";
338 $b = new ComponentMock();
339 $b->random_data = "B";
340 $c = new ComponentMock();
341 $c->random_data = "C";
342 $c->sub_components = [$a, $b];
343
344 $f = function ($c, $res) {
345 return [$c, $c->random_data];
346 };
347
348 [$res, $_] = $c->reduceWith($f);
349 $this->assertEquals([$a, $b], $res->sub_components);
350 }
351}
$check
Definition: buildRTE.php:81
_checkStringArg(string $which, $value)
_checkArgListElements(string $which, array &$value, $classes)
_checkArgInstanceOf(string $which, $value, string $class)
_checkArg(string $which, bool $check, string $message)
_checkBoolArg(string $which, $value)
_checkArgList(string $which, array &$value, Closure $check, Closure $message)
_checkArgIsElement(string $which, $value, array $array, string $name)
$c
Definition: deliver.php:25
A component is the most general form of an entity in the UI.
Definition: Component.php:28
$res
Definition: ltiservices.php:69
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$message
Definition: xapiexit.php:31