ILIAS  release_8 Revision v8.24
ComponentHelperTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21use PHPUnit\Framework\TestCase;
22use ILIAS\UI\Implementation\Component\ComponentHelper;
24
25require_once("libs/composer/vendor/autoload.php");
26
27require_once(__DIR__ . "/../Renderer/TestComponent.php");
28
30{
31 use ComponentHelper;
32
33 public function _checkArg(string $which, bool $check, string $message): void
34 {
35 $this->checkArg($which, $check, $message);
36 }
37
38 public function _checkStringArg(string $which, $value): void
39 {
40 $this->checkStringArg($which, $value);
41 }
42
43 public function _checkBoolArg(string $which, $value): void
44 {
45 $this->checkBoolArg($which, $value);
46 }
47
48 public function _checkArgInstanceOf(string $which, $value, string $class): void
49 {
50 $this->checkArgInstanceOf($which, $value, $class);
51 }
52
53 public function _checkArgIsElement(string $which, $value, array $array, string $name): void
54 {
55 $this->checkArgIsElement($which, $value, $array, $name);
56 }
57
58 public function _toArray($value): array
59 {
60 return $this->toArray($value);
61 }
62
63 public function _checkArgListElements(string $which, array &$value, $classes): void
64 {
65 $this->checkArgListElements($which, $value, $classes);
66 }
67
68 public function _checkArgList(string $which, array &$value, Closure $check, Closure $message): void
69 {
70 $this->checkArgList($which, $value, $check, $message);
71 }
72}
73
74class Class1
75{
76}
77
78class Class2
79{
80}
81
82class Class3
83{
84}
85
89class ComponentHelperTest extends TestCase
90{
92
93 public function setUp(): void
94 {
95 $this->mock = new ComponentMock();
96 }
97
98 public function test_getCanonicalName(): void
99 {
100 $c = new TestComponent("foo");
101 $this->assertEquals("Test Component Test", $c->getCanonicalName());
102 }
103
107 public function test_check_arg_ok(): void
108 {
109 $this->mock->_checkArg("some_arg", true, "some message");
110 }
111
112 public function test_check_arg_not_ok(): void
113 {
114 $this->expectException(InvalidArgumentException::class);
115 $this->expectExceptionMessage("Argument 'some_arg': some message");
116 $this->mock->_checkArg("some_arg", false, "some message");
117 }
118
122 public function test_check_string_arg_ok(): void
123 {
124 $this->mock->_checkStringArg("some_arg", "bar");
125 }
126
127 public function test_check_string_arg_not_ok(): void
128 {
129 $this->expectException(InvalidArgumentException::class);
130 $this->expectExceptionMessage("Argument 'some_arg': expected string, got integer '1'");
131 $this->mock->_checkStringArg("some_arg", 1);
132 }
133
137 public function test_check_bool_arg_ok(): void
138 {
139 $this->mock->_checkBoolArg("some_arg", true);
140 }
141
142 public function test_check_bool_arg_not_ok(): void
143 {
144 $this->expectException(InvalidArgumentException::class);
145 $this->expectExceptionMessage("Argument 'some_arg': expected bool, got integer '1'");
146 $this->mock->_checkBoolArg("some_arg", 1);
147 }
148
152 public function test_check_arg_instanceof_ok(): void
153 {
154 $this->mock->_checkArgInstanceOf("some_arg", $this->mock, ComponentMock::class);
155 }
156
157 public function test_check_arg_instanceof_not_ok(): void
158 {
159 $this->expectException(InvalidArgumentException::class);
160 $this->expectExceptionMessage("Argument 'some_arg': expected ComponentMock, got ComponentHelperTest");
161 $this->mock->_checkArgInstanceOf("some_arg", $this, ComponentMock::class);
162 }
163
164
168 public function test_check_arg_is_element_ok(): void
169 {
170 $this->mock->_checkArgIsElement("some_arg", "bar", array("foo", "bar"), "foobar");
171 }
172
174 {
175 $this->expectException(InvalidArgumentException::class);
176 $this->expectExceptionMessage("Argument 'some_arg': expected foobar, got 'baz'");
177 $this->mock->_checkArgIsElement("some_arg", "baz", array("foo", "bar"), "foobar");
178 }
179
180 public function test_to_array_with_array(): void
181 {
182 $foo = array("foo", "bar");
183 $res = $this->mock->_toArray($foo);
184
185 $this->assertEquals($foo, $res);
186 }
187
188 public function test_to_array_with_int(): void
189 {
190 $foo = 1;
191 $res = $this->mock->_toArray($foo);
192 $this->assertEquals(array($foo), $res);
193 }
194
198 public function test_check_arg_list_elements_ok(): void
199 {
200 $l = array(new Class1(), new Class1(), new Class1());
201 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
202 }
203
205 {
206 $this->expectException(InvalidArgumentException::class);
207 $this->expectExceptionMessage("Argument 'some_arg': expected Class1, got Class2");
208 $l = array(new Class1(), new Class1(), new Class2());
209 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
210 }
211
216 {
217 $l = array(new Class1(), new Class2(), new Class1());
218 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
219 }
220
222 {
223 $this->expectException(InvalidArgumentException::class);
224 $this->expectExceptionMessage("Argument 'some_arg': expected Class1, Class2, got Class3");
225 $l = array(new Class1(), new Class2(), new Class3(), new Class2());
226 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
227 }
228
233 {
234 $l = array(1, "foo");
235 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
236 }
237
239 {
240 $this->expectException(InvalidArgumentException::class);
241 $this->expectExceptionMessage("Argument 'some_arg': expected string, int, got Class1");
242 $l = array(1, new Class1());
243 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
244 }
245
249 public function test_check_arg_list_ok(): void
250 {
251 $l = array("a" => 1, "b" => 2, "c" => 3);
252 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
253 return is_string($k) && is_int($v);
254 }, function ($k, $v) {
255 return "expected keys of type string and integer values, got ($k => $v)";
256 });
257 }
258
259 public function test_check_arg_list_not_ok_1(): void
260 {
261 $m = "expected keys of type string and integer values, got (4 => 3)";
262 $this->expectException(InvalidArgumentException::class);
263 $this->expectExceptionMessage("Argument 'some_arg': $m");
264 $l = array("a" => 1, "b" => 2, 4 => 3);
265 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
266 return is_string($k) && is_int($v);
267 }, function ($k, $v) {
268 return "expected keys of type string and integer values, got ($k => $v)";
269 });
270 }
271
272 public function test_check_arg_list_not_ok_2(): void
273 {
274 $m = "expected keys of type string and integer values, got (c => d)";
275 $this->expectException(InvalidArgumentException::class);
276 $this->expectExceptionMessage("Argument 'some_arg': $m");
277 $l = array("a" => 1, "b" => 2, "c" => "d");
278 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
279 return is_string($k) && is_int($v);
280 }, function ($k, $v) {
281 return "expected keys of type string and integer values, got ($k => $v)";
282 });
283 }
284}
$check
Definition: buildRTE.php:81
test_check_arg_list_elements_multi_class_ok()
@doesNotPerformAssertions
test_check_arg_list_elements_string_or_int_ok()
@doesNotPerformAssertions
test_check_arg_is_element_ok()
@doesNotPerformAssertions
test_check_arg_list_ok()
@doesNotPerformAssertions
test_check_string_arg_ok()
@doesNotPerformAssertions
test_check_bool_arg_ok()
@doesNotPerformAssertions
test_check_arg_instanceof_ok()
@doesNotPerformAssertions
test_check_arg_ok()
@doesNotPerformAssertions
test_check_arg_list_elements_ok()
@doesNotPerformAssertions
_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: cli.php:38
$res
Definition: ltiservices.php:69
if($format !==null) $name
Definition: metadata.php:247
$message
Definition: xapiexit.php:32