ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
5 require_once("libs/composer/vendor/autoload.php");
6 
7 require_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 _checkBoolArg($which, $value)
30  {
31  $this->checkBoolArg($which, $value);
32  }
33  public function _checkArgInstanceOf($which, $value, $class)
34  {
35  $this->checkArgInstanceOf($which, $value, $class);
36  }
37  public function _checkArgIsElement($which, $value, $array, $name)
38  {
39  $this->checkArgIsElement($which, $value, $array, $name);
40  }
41  public function _toArray($value)
42  {
43  return $this->toArray($value);
44  }
45  public function _checkArgListElements($which, &$value, $classes)
46  {
47  $this->checkArgListElements($which, $value, $classes);
48  }
49  public function _checkArgList($which, &$value, $check, $message)
50  {
51  $this->checkArgList($which, $value, $check, $message);
52  }
53 
54  public $called_gcnbfqn = 0;
56  {
57  $this->called_gcnbfqn++;
58  return "Foo";
59  }
60 }
61 
62 class Class1
63 {
64 }
65 class Class2
66 {
67 }
68 class Class3
69 {
70 }
71 
76 {
77  public function setUp()
78  {
79  $this->mock = new ComponentMock();
80  }
81 
82  public function test_getCanonicalName()
83  {
84  $c = new \ILIAS\UI\Component\Test\TestComponent("foo");
85  $this->assertEquals("Test Component Test", $c->getCanonicalName());
86  }
87 
88  public function test_cachesCanonicalName()
89  {
90  $name1 = $this->mock->getCanonicalName();
91  $name2 = $this->mock->getCanonicalName();
92  $this->assertEquals($name1, $name2);
93  $this->assertEquals(1, $this->mock->called_gcnbfqn);
94  }
95 
96  public function test_check_arg_ok()
97  {
98  try {
99  $this->mock->_checkArg("some_arg", true, "some message");
100  } catch (\InvalidArgumentException $e) {
101  $this->assertFalse("This should not happen.");
102  }
103  }
104 
105  public function test_check_arg_not_ok()
106  {
107  try {
108  $this->mock->_checkArg("some_arg", false, "some message");
109  $this->assertFalse("This should not happen.");
110  } catch (\InvalidArgumentException $e) {
111  $this->assertEquals("Argument 'some_arg': some message", $e->getMessage());
112  }
113  }
114 
115  public function test_check_int_arg_ok()
116  {
117  try {
118  $this->mock->_checkIntArg("some_arg", 1);
119  } catch (\InvalidArgumentException $e) {
120  $this->assertFalse("This should not happen.");
121  }
122  }
123 
124  public function test_check_int_arg_not_ok()
125  {
126  try {
127  $this->mock->_checkIntArg("some_arg", "foo");
128  $this->assertFalse("This should not happen.");
129  } catch (\InvalidArgumentException $e) {
130  $this->assertEquals("Argument 'some_arg': expected integer, got string 'foo'", $e->getMessage());
131  }
132  }
133 
134  public function test_check_string_arg_ok()
135  {
136  try {
137  $this->mock->_checkStringArg("some_arg", "bar");
138  } catch (\InvalidArgumentException $e) {
139  $this->assertFalse("This should not happen.");
140  }
141  }
142 
144  {
145  try {
146  $this->mock->_checkStringArg("some_arg", 1);
147  $this->assertFalse("This should not happen.");
148  } catch (\InvalidArgumentException $e) {
149  $this->assertEquals("Argument 'some_arg': expected string, got integer '1'", $e->getMessage());
150  }
151  }
152 
153  public function test_check_bool_arg_ok()
154  {
155  try {
156  $this->mock->_checkBoolArg("some_arg", true);
157  } catch (\InvalidArgumentException $e) {
158  $this->assertFalse("This should not happen.");
159  }
160  }
161 
162  public function test_check_bool_arg_not_ok()
163  {
164  try {
165  $this->mock->_checkBoolArg("some_arg", 1);
166  $this->assertFalse("This should not happen.");
167  } catch (\InvalidArgumentException $e) {
168  $this->assertEquals("Argument 'some_arg': expected bool, got integer '1'", $e->getMessage());
169  }
170  }
171 
173  {
174  try {
175  $this->mock->_checkArgInstanceOf("some_arg", $this->mock, ComponentMock::class);
176  } catch (\InvalidArgumentException $e) {
177  $this->assertFalse("This should not happen.");
178  }
179  }
180 
182  {
183  try {
184  $this->mock->_checkArgInstanceOf("some_arg", $this, ComponentMock::class);
185  $this->assertFalse("This should not happen.");
186  } catch (\InvalidArgumentException $e) {
187  $this->assertEquals("Argument 'some_arg': expected ComponentMock, got ComponentHelperTest", $e->getMessage());
188  }
189  }
190 
191 
192 
194  {
195  try {
196  $this->mock->_checkArgIsElement("some_arg", "bar", array("foo", "bar"), "foobar");
197  } catch (\InvalidArgumentException $e) {
198  $this->assertFalse("This should not happen.");
199  }
200  }
201 
203  {
204  try {
205  $this->mock->_checkArgIsElement("some_arg", "baz", array("foo", "bar"), "foobar");
206  $this->assertFalse("This should not happen.");
207  } catch (\InvalidArgumentException $e) {
208  $this->assertEquals("Argument 'some_arg': expected foobar, got 'baz'", $e->getMessage());
209  }
210  }
211 
212  public function test_to_array_with_array()
213  {
214  $foo = array("foo", "bar");
215  $res = $this->mock->_toArray($foo);
216 
217  $this->assertEquals($foo, $res);
218  }
219 
220  public function test_to_array_with_int()
221  {
222  $foo = 1;
223  $res = $this->mock->_toArray($foo);
224  $this->assertEquals(array($foo), $res);
225  }
226 
228  {
229  $l = array(new Class1(), new Class1(), new Class1());
230  try {
231  $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
232  } catch (\InvalidArgumentException $e) {
233  $this->assertFalse("This should not happen.");
234  }
235  }
236 
238  {
239  $l = array(new Class1(), new Class1(), new Class2());
240  try {
241  $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
242  $this->assertFalse("This should not happen.");
243  } catch (\InvalidArgumentException $e) {
244  $this->assertEquals("Argument 'some_arg': expected Class1, got Class2", $e->getMessage());
245  }
246  }
247 
249  {
250  $l = array(new Class1(), new Class2(), new Class1());
251  try {
252  $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
253  } catch (\InvalidArgumentException $e) {
254  $this->assertFalse("This should not happen.");
255  }
256  }
257 
259  {
260  $l = array(new Class1(), new Class2(), new Class3(), new Class2());
261  try {
262  $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
263  $this->assertFalse("This should not happen.");
264  } catch (\InvalidArgumentException $e) {
265  $this->assertEquals("Argument 'some_arg': expected Class1, Class2, got Class3", $e->getMessage());
266  }
267  }
268 
270  {
271  $l = array(1, "foo");
272  try {
273  $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
274  } catch (\InvalidArgumentException $e) {
275  $this->assertFalse("This should not happen.");
276  }
277  }
278 
280  {
281  $l = array(1, new Class1());
282  try {
283  $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
284  $this->assertFalse("This should not happen.");
285  } catch (\InvalidArgumentException $e) {
286  $this->assertEquals("Argument 'some_arg': expected string, int, got Class1", $e->getMessage());
287  }
288  }
289 
290  public function test_check_arg_list_ok()
291  {
292  $l = array("a" => 1, "b" => 2, "c" => 3);
293  try {
294  $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
295  return is_string($k) && is_int($v);
296  }, function ($k, $v) {
297  return "expected keys of type string and integer values, got ($k => $v)";
298  });
299  } catch (\InvalidArgumentException $e) {
300  $this->assertFalse("This should not happen.");
301  }
302  }
303 
305  {
306  $l = array("a" => 1, "b" => 2, 4 => 3);
307  try {
308  $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
309  return is_string($k) && is_int($v);
310  }, function ($k, $v) {
311  return "expected keys of type string and integer values, got ($k => $v)";
312  });
313  $this->assertFalse("This should not happen.");
314  } catch (\InvalidArgumentException $e) {
315  $m = "expected keys of type string and integer values, got (4 => 3)";
316  $this->assertEquals("Argument 'some_arg': $m", $e->getMessage());
317  }
318  }
319 
321  {
322  $l = array("a" => 1, "b" => 2, "c" => "d");
323  try {
324  $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
325  return is_string($k) && is_int($v);
326  }, function ($k, $v) {
327  return "expected keys of type string and integer values, got ($k => $v)";
328  });
329 
330  $this->assertFalse("This should not happen.");
331  } catch (\InvalidArgumentException $e) {
332  $m = "expected keys of type string and integer values, got (c => d)";
333  $this->assertEquals("Argument 'some_arg': $m", $e->getMessage());
334  }
335  }
336 
337  public function test_check_float_arg_ok()
338  {
339  try {
340  $this->mock->_checkFloatArg("some_arg", 1.73);
341  } catch (\InvalidArgumentException $e) {
342  $this->assertFalse("This should not happen.");
343  }
344  }
345 
346  public function test_check_float_arg_not_ok()
347  {
348  try {
349  $this->mock->_checkFloatArg("some_arg", "foo");
350  $this->assertFalse("This should not happen.");
351  } catch (\InvalidArgumentException $e) {
352  $this->assertEquals("Argument 'some_arg': expected float, got string 'foo'", $e->getMessage());
353  }
354  }
355 }
_checkBoolArg($which, $value)
_checkArgList($which, &$value, $check, $message)
checkArgIsElement($which, $value, $array, $name)
Throw an InvalidArgumentException if $value is not an element of array.
checkArg($which, $check, $message)
/** Throw an InvalidArgumentException containing the message if $check is false.
_checkArgListElements($which, &$value, $classes)
checkArgInstanceOf($which, $value, $class)
Throw an InvalidArgumentException if $value is not an instance of $class.
trait ComponentHelper
Provides common functionality for component implementations.
checkStringArg($which, $value)
Throw an InvalidArgumentException if $value is no string.
checkArgList($which, array &$values, \Closure $check, \Closure $message)
Check every key and value of the list with a supplied closure.
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.
catch(Exception $e) $message
_checkArg($which, $check, $message)
foreach($_POST as $key=> $value) $res
_checkArgIsElement($which, $value, $array, $name)
checkFloatArg($which, $value)
Throw an InvalidArgumentException if $value is not a float.
_checkStringArg($which, $value)
_checkArgInstanceOf($which, $value, $class)
global $l
Definition: afr.php:30
toArray($value)
Wrap the given value in an array if it is no array.
_checkIntArg($which, $value)
_checkFloatArg($which, $value)
checkIntArg($which, $value)
Throw an InvalidArgumentException if $value is no int.