ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
5require_once("libs/composer/vendor/autoload.php");
6
7require_once(__DIR__ . "/../Renderer/TestComponent.php");
8
10{
12
13 public function _checkArg($which, $check, $message)
14 {
15 $this->checkArg($which, $check, $message);
16 }
17 public function _checkIntArg($which, $value)
18 {
19 $this->checkIntArg($which, $value);
20 }
21 public function _checkStringArg($which, $value)
22 {
23 $this->checkStringArg($which, $value);
24 }
25 public function _checkFloatArg($which, $value)
26 {
27 $this->checkFloatArg($which, $value);
28 }
29 public function _checkArgInstanceOf($which, $value, $class)
30 {
31 $this->checkArgInstanceOf($which, $value, $class);
32 }
33 public function _checkArgIsElement($which, $value, $array, $name)
34 {
35 $this->checkArgIsElement($which, $value, $array, $name);
36 }
37 public function _toArray($value)
38 {
39 return $this->toArray($value);
40 }
41 public function _checkArgListElements($which, &$value, $classes)
42 {
43 $this->checkArgListElements($which, $value, $classes);
44 }
45 public function _checkArgList($which, &$value, $check, $message)
46 {
47 $this->checkArgList($which, $value, $check, $message);
48 }
49
50 public $called_gcnbfqn = 0;
52 {
53 $this->called_gcnbfqn++;
54 return "Foo";
55 }
56}
57
58class Class1
59{
60}
61class Class2
62{
63}
64class Class3
65{
66}
67
72{
73 public function setUp()
74 {
75 $this->mock = new ComponentMock();
76 }
77
78 public function test_getCanonicalName()
79 {
80 $c = new \ILIAS\UI\Component\Test\TestComponent("foo");
81 $this->assertEquals("Test Component Test", $c->getCanonicalName());
82 }
83
84 public function test_cachesCanonicalName()
85 {
86 $name1 = $this->mock->getCanonicalName();
87 $name2 = $this->mock->getCanonicalName();
88 $this->assertEquals($name1, $name2);
89 $this->assertEquals(1, $this->mock->called_gcnbfqn);
90 }
91
92 public function test_check_arg_ok()
93 {
94 try {
95 $this->mock->_checkArg("some_arg", true, "some message");
96 } catch (\InvalidArgumentException $e) {
97 $this->assertFalse("This should not happen.");
98 }
99 }
100
101 public function test_check_arg_not_ok()
102 {
103 try {
104 $this->mock->_checkArg("some_arg", false, "some message");
105 $this->assertFalse("This should not happen.");
106 } catch (\InvalidArgumentException $e) {
107 $this->assertEquals("Argument 'some_arg': some message", $e->getMessage());
108 }
109 }
110
111 public function test_check_int_arg_ok()
112 {
113 try {
114 $this->mock->_checkIntArg("some_arg", 1);
115 } catch (\InvalidArgumentException $e) {
116 $this->assertFalse("This should not happen.");
117 }
118 }
119
121 {
122 try {
123 $this->mock->_checkIntArg("some_arg", "foo");
124 $this->assertFalse("This should not happen.");
125 } catch (\InvalidArgumentException $e) {
126 $this->assertEquals("Argument 'some_arg': expected integer, got string 'foo'", $e->getMessage());
127 }
128 }
129
130 public function test_check_string_arg_ok()
131 {
132 try {
133 $this->mock->_checkStringArg("some_arg", "bar");
134 } catch (\InvalidArgumentException $e) {
135 $this->assertFalse("This should not happen.");
136 }
137 }
138
140 {
141 try {
142 $this->mock->_checkStringArg("some_arg", 1);
143 $this->assertFalse("This should not happen.");
144 } catch (\InvalidArgumentException $e) {
145 $this->assertEquals("Argument 'some_arg': expected string, got integer '1'", $e->getMessage());
146 }
147 }
148
150 {
151 try {
152 $this->mock->_checkArgInstanceOf("some_arg", $this->mock, ComponentMock::class);
153 } catch (\InvalidArgumentException $e) {
154 $this->assertFalse("This should not happen.");
155 }
156 }
157
159 {
160 try {
161 $this->mock->_checkArgInstanceOf("some_arg", $this, ComponentMock::class);
162 $this->assertFalse("This should not happen.");
163 } catch (\InvalidArgumentException $e) {
164 $this->assertEquals("Argument 'some_arg': expected ComponentMock, got ComponentHelperTest", $e->getMessage());
165 }
166 }
167
168
169
171 {
172 try {
173 $this->mock->_checkArgIsElement("some_arg", "bar", array("foo", "bar"), "foobar");
174 } catch (\InvalidArgumentException $e) {
175 $this->assertFalse("This should not happen.");
176 }
177 }
178
180 {
181 try {
182 $this->mock->_checkArgIsElement("some_arg", "baz", array("foo", "bar"), "foobar");
183 $this->assertFalse("This should not happen.");
184 } catch (\InvalidArgumentException $e) {
185 $this->assertEquals("Argument 'some_arg': expected foobar, got 'baz'", $e->getMessage());
186 }
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
205 {
206 $l = array(new Class1(), new Class1(), new Class1());
207 try {
208 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
209 } catch (\InvalidArgumentException $e) {
210 $this->assertFalse("This should not happen.");
211 }
212 }
213
215 {
216 $l = array(new Class1(), new Class1(), new Class2());
217 try {
218 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
219 $this->assertFalse("This should not happen.");
220 } catch (\InvalidArgumentException $e) {
221 $this->assertEquals("Argument 'some_arg': expected Class1, got Class2", $e->getMessage());
222 }
223 }
224
226 {
227 $l = array(new Class1(), new Class2(), new Class1());
228 try {
229 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
230 } catch (\InvalidArgumentException $e) {
231 $this->assertFalse("This should not happen.");
232 }
233 }
234
236 {
237 $l = array(new Class1(), new Class2(), new Class3(), new Class2());
238 try {
239 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
240 $this->assertFalse("This should not happen.");
241 } catch (\InvalidArgumentException $e) {
242 $this->assertEquals("Argument 'some_arg': expected Class1, Class2, got Class3", $e->getMessage());
243 }
244 }
245
247 {
248 $l = array(1, "foo");
249 try {
250 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
251 } catch (\InvalidArgumentException $e) {
252 $this->assertFalse("This should not happen.");
253 }
254 }
255
257 {
258 $l = array(1, new Class1());
259 try {
260 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
261 $this->assertFalse("This should not happen.");
262 } catch (\InvalidArgumentException $e) {
263 $this->assertEquals("Argument 'some_arg': expected string, int, got Class1", $e->getMessage());
264 }
265 }
266
267 public function test_check_arg_list_ok()
268 {
269 $l = array("a" => 1, "b" => 2, "c" => 3);
270 try {
271 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
272 return is_string($k) && is_int($v);
273 }, function ($k, $v) {
274 return "expected keys of type string and integer values, got ($k => $v)";
275 });
276 } catch (\InvalidArgumentException $e) {
277 $this->assertFalse("This should not happen.");
278 }
279 }
280
282 {
283 $l = array("a" => 1, "b" => 2, 4 => 3);
284 try {
285 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
286 return is_string($k) && is_int($v);
287 }, function ($k, $v) {
288 return "expected keys of type string and integer values, got ($k => $v)";
289 });
290 $this->assertFalse("This should not happen.");
291 } catch (\InvalidArgumentException $e) {
292 $m = "expected keys of type string and integer values, got (4 => 3)";
293 $this->assertEquals("Argument 'some_arg': $m", $e->getMessage());
294 }
295 }
296
298 {
299 $l = array("a" => 1, "b" => 2, "c" => "d");
300 try {
301 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
302 return is_string($k) && is_int($v);
303 }, function ($k, $v) {
304 return "expected keys of type string and integer values, got ($k => $v)";
305 });
306
307 $this->assertFalse("This should not happen.");
308 } catch (\InvalidArgumentException $e) {
309 $m = "expected keys of type string and integer values, got (c => d)";
310 $this->assertEquals("Argument 'some_arg': $m", $e->getMessage());
311 }
312 }
313
314 public function test_check_float_arg_ok()
315 {
316 try {
317 $this->mock->_checkFloatArg("some_arg", 1.73);
318 } catch (\InvalidArgumentException $e) {
319 $this->assertFalse("This should not happen.");
320 }
321 }
322
324 {
325 try {
326 $this->mock->_checkFloatArg("some_arg", "foo");
327 $this->assertFalse("This should not happen.");
328 } catch (\InvalidArgumentException $e) {
329 $this->assertEquals("Argument 'some_arg': expected float, got string 'foo'", $e->getMessage());
330 }
331 }
332}
global $l
Definition: afr.php:30
An exception for terminatinating execution or to throw for unit testing.
_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)
_checkArg($which, $check, $message)
if($format !==null) $name
Definition: metadata.php:146
catch(Exception $e) $message
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.
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