ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ComponentHelperTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5use PHPUnit\Framework\TestCase;
6
7require_once("libs/composer/vendor/autoload.php");
8
9require_once(__DIR__ . "/../Renderer/TestComponent.php");
10
12{
14
15 public function _checkArg($which, $check, $message)
16 {
17 $this->checkArg($which, $check, $message);
18 }
19 public function _checkIntArg($which, $value)
20 {
21 $this->checkIntArg($which, $value);
22 }
23 public function _checkStringArg($which, $value)
24 {
25 $this->checkStringArg($which, $value);
26 }
27 public function _checkFloatArg($which, $value)
28 {
29 $this->checkFloatArg($which, $value);
30 }
31 public function _checkBoolArg($which, $value)
32 {
33 $this->checkBoolArg($which, $value);
34 }
35 public function _checkArgInstanceOf($which, $value, $class)
36 {
37 $this->checkArgInstanceOf($which, $value, $class);
38 }
39 public function _checkArgIsElement($which, $value, $array, $name)
40 {
41 $this->checkArgIsElement($which, $value, $array, $name);
42 }
43 public function _toArray($value)
44 {
45 return $this->toArray($value);
46 }
47 public function _checkArgListElements($which, &$value, $classes)
48 {
49 $this->checkArgListElements($which, $value, $classes);
50 }
51 public function _checkArgList($which, &$value, $check, $message)
52 {
53 $this->checkArgList($which, $value, $check, $message);
54 }
55
56 public $called_gcnbfqn = 0;
58 {
59 $this->called_gcnbfqn++;
60 return "Foo";
61 }
62}
63
64class Class1
65{
66}
67class Class2
68{
69}
70class Class3
71{
72}
73
77class ComponentHelperTest extends TestCase
78{
79 public function setUp() : void
80 {
81 $this->mock = new ComponentMock();
82 }
83
84 public function test_getCanonicalName()
85 {
86 $c = new \ILIAS\UI\Component\Test\TestComponent("foo");
87 $this->assertEquals("Test Component Test", $c->getCanonicalName());
88 }
89
90 public function test_cachesCanonicalName()
91 {
92 $name1 = $this->mock->getCanonicalName();
93 $name2 = $this->mock->getCanonicalName();
94 $this->assertEquals($name1, $name2);
95 $this->assertEquals(1, $this->mock->called_gcnbfqn);
96 }
97
101 public function test_check_arg_ok()
102 {
103 $this->mock->_checkArg("some_arg", true, "some message");
104 }
105
106 public function test_check_arg_not_ok()
107 {
108 $this->expectException(\InvalidArgumentException::class);
109 $this->expectExceptionMessage("Argument 'some_arg': some message");
110 $this->mock->_checkArg("some_arg", false, "some message");
111 }
112
116 public function test_check_int_arg_ok()
117 {
118 $this->mock->_checkIntArg("some_arg", 1);
119 }
120
122 {
123 $this->expectException(\InvalidArgumentException::class);
124 $this->expectExceptionMessage("Argument 'some_arg': expected integer, got string 'foo'");
125 $this->mock->_checkIntArg("some_arg", "foo");
126 }
127
131 public function test_check_string_arg_ok()
132 {
133 $this->mock->_checkStringArg("some_arg", "bar");
134 }
135
137 {
138 $this->expectException(\InvalidArgumentException::class);
139 $this->expectExceptionMessage("Argument 'some_arg': expected string, got integer '1'");
140 $this->mock->_checkStringArg("some_arg", 1);
141 }
142
146 public function test_check_bool_arg_ok()
147 {
148 $this->mock->_checkBoolArg("some_arg", true);
149 }
150
152 {
153 $this->expectException(\InvalidArgumentException::class);
154 $this->expectExceptionMessage("Argument 'some_arg': expected bool, got integer '1'");
155 $this->mock->_checkBoolArg("some_arg", 1);
156 }
157
162 {
163 $this->mock->_checkArgInstanceOf("some_arg", $this->mock, ComponentMock::class);
164 }
165
167 {
168 $this->expectException(\InvalidArgumentException::class);
169 $this->expectExceptionMessage("Argument 'some_arg': expected ComponentMock, got ComponentHelperTest");
170 $this->mock->_checkArgInstanceOf("some_arg", $this, ComponentMock::class);
171 }
172
173
178 {
179 $this->mock->_checkArgIsElement("some_arg", "bar", array("foo", "bar"), "foobar");
180 }
181
183 {
184 $this->expectException(\InvalidArgumentException::class);
185 $this->expectExceptionMessage("Argument 'some_arg': expected foobar, got 'baz'");
186 $this->mock->_checkArgIsElement("some_arg", "baz", array("foo", "bar"), "foobar");
187 }
188
189 public function test_to_array_with_array()
190 {
191 $foo = array("foo", "bar");
192 $res = $this->mock->_toArray($foo);
193
194 $this->assertEquals($foo, $res);
195 }
196
197 public function test_to_array_with_int()
198 {
199 $foo = 1;
200 $res = $this->mock->_toArray($foo);
201 $this->assertEquals(array($foo), $res);
202 }
203
208 {
209 $l = array(new Class1(), new Class1(), new Class1());
210 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
211 }
212
214 {
215 $this->expectException(\InvalidArgumentException::class);
216 $this->expectExceptionMessage("Argument 'some_arg': expected Class1, got Class2");
217 $l = array(new Class1(), new Class1(), new Class2());
218 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
219 }
220
225 {
226 $l = array(new Class1(), new Class2(), new Class1());
227 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
228 }
229
231 {
232 $this->expectException(\InvalidArgumentException::class);
233 $this->expectExceptionMessage("Argument 'some_arg': expected Class1, Class2, got Class3");
234 $l = array(new Class1(), new Class2(), new Class3(), new Class2());
235 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
236 }
237
242 {
243 $l = array(1, "foo");
244 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
245 }
246
248 {
249 $this->expectException(\InvalidArgumentException::class);
250 $this->expectExceptionMessage("Argument 'some_arg': expected string, int, got Class1");
251 $l = array(1, new Class1());
252 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
253 }
254
258 public function test_check_arg_list_ok()
259 {
260 $l = array("a" => 1, "b" => 2, "c" => 3);
261 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
262 return is_string($k) && is_int($v);
263 }, function ($k, $v) {
264 return "expected keys of type string and integer values, got ($k => $v)";
265 });
266 }
267
269 {
270 $m = "expected keys of type string and integer values, got (4 => 3)";
271 $this->expectException(\InvalidArgumentException::class);
272 $this->expectExceptionMessage("Argument 'some_arg': $m");
273 $l = array("a" => 1, "b" => 2, 4 => 3);
274 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
275 return is_string($k) && is_int($v);
276 }, function ($k, $v) {
277 return "expected keys of type string and integer values, got ($k => $v)";
278 });
279 }
280
282 {
283 $m = "expected keys of type string and integer values, got (c => d)";
284 $this->expectException(\InvalidArgumentException::class);
285 $this->expectExceptionMessage("Argument 'some_arg': $m");
286 $l = array("a" => 1, "b" => 2, "c" => "d");
287 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
288 return is_string($k) && is_int($v);
289 }, function ($k, $v) {
290 return "expected keys of type string and integer values, got ($k => $v)";
291 });
292 }
293
297 public function test_check_float_arg_ok()
298 {
299 $this->mock->_checkFloatArg("some_arg", 1.73);
300 }
301
303 {
304 $this->expectException(\InvalidArgumentException::class);
305 $this->expectExceptionMessage("Argument 'some_arg': expected float, got string 'foo'");
306 $this->mock->_checkFloatArg("some_arg", "foo");
307 }
308}
An exception for terminatinating execution or to throw for unit testing.
test_check_float_arg_ok()
@doesNotPerformAssertions
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_int_arg_ok()
@doesNotPerformAssertions
test_check_arg_instanceof_ok()
@doesNotPerformAssertions
test_check_arg_ok()
@doesNotPerformAssertions
test_check_arg_list_elements_ok()
@doesNotPerformAssertions
_checkFloatArg($which, $value)
_checkIntArg($which, $value)
_checkArgList($which, &$value, $check, $message)
_checkArgListElements($which, &$value, $classes)
_checkArgInstanceOf($which, $value, $class)
_checkArgIsElement($which, $value, $array, $name)
_checkStringArg($which, $value)
_checkBoolArg($which, $value)
_checkArg($which, $check, $message)
if($format !==null) $name
Definition: metadata.php:230
checkArgList($which, array &$values, \Closure $check, \Closure $message)
Check every key and value of the list with a supplied closure.
checkStringArg($which, $value)
Throw an InvalidArgumentException if $value is no string.
checkArg($which, $check, $message)
/** Throw an InvalidArgumentException containing the message if $check is false.
checkIntArg($which, $value)
Throw an InvalidArgumentException if $value is no int.
toArray($value)
Wrap the given value in an array if it is no array.
checkArgListElements($which, array &$values, $classes)
Check every element of the list if it is an instance of one of the given classes.
checkBoolArg($which, $value)
Throw an InvalidArgumentException if $value is not a bool.
trait ComponentHelper
Provides common functionality for component implementations.
checkArgIsElement($which, $value, $array, $name)
Throw an InvalidArgumentException if $value is not an element of array.
checkFloatArg($which, $value)
Throw an InvalidArgumentException if $value is not a float.
checkArgInstanceOf($which, $value, $class)
Throw an InvalidArgumentException if $value is not an instance of $class.
foreach($_POST as $key=> $value) $res
$message
Definition: xapiexit.php:14