ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
8 use \ILIAS\UI\Implementation\Component\ComponentHelper;
9
10 public function _checkArg($which, $check, $message) {
11 $this->checkArg($which, $check, $message);
12 }
13 public function _checkIntArg($which, $value) {
14 $this->checkIntArg($which, $value);
15 }
16 public function _checkStringArg($which, $value) {
17 $this->checkStringArg($which, $value);
18 }
19 public function _checkArgInstanceOf($which, $value, $class) {
20 $this->checkArgInstanceOf($which, $value, $class);
21 }
22 public function _checkArgIsElement($which, $value, $array, $name) {
23 $this->checkArgIsElement($which, $value, $array, $name);
24 }
25 public function _toArray($value) {
26 return $this->toArray($value);
27 }
28 public function _checkArgListElements($which, &$value, $classes) {
29 $this->checkArgListElements($which, $value, $classes);
30 }
31 public function _checkArgList($which, &$value, $check, $message) {
32 $this->checkArgList($which, $value, $check, $message);
33 }
34}
35
36class Class1 {
37}
38class Class2 {
39}
40class Class3 {
41}
42
47 public function setUp() {
48 $this->mock = new ComponentMock();
49 }
50
51 public function test_check_arg_ok() {
52 try {
53 $this->mock->_checkArg("some_arg", true, "some message");
54 }
55 catch (\InvalidArgumentException $e) {
56 $this->assertFalse("This should not happen.");
57 }
58 }
59
60 public function test_check_arg_not_ok() {
61 try {
62 $this->mock->_checkArg("some_arg", false, "some message");
63 $this->assertFalse("This should not happen.");
64 }
65 catch (\InvalidArgumentException $e) {
66 $this->assertEquals("Argument 'some_arg': some message", $e->getMessage());
67 }
68 }
69
70 public function test_check_int_arg_ok() {
71 try {
72 $this->mock->_checkIntArg("some_arg", 1);
73 }
74 catch (\InvalidArgumentException $e) {
75 $this->assertFalse("This should not happen.");
76 }
77 }
78
79 public function test_check_int_arg_not_ok() {
80 try {
81 $this->mock->_checkIntArg("some_arg", "foo");
82 $this->assertFalse("This should not happen.");
83 }
84 catch (\InvalidArgumentException $e) {
85 $this->assertEquals("Argument 'some_arg': expected integer, got string 'foo'", $e->getMessage());
86 }
87 }
88
89 public function test_check_string_arg_ok() {
90 try {
91 $this->mock->_checkStringArg("some_arg", "bar");
92 }
93 catch (\InvalidArgumentException $e) {
94 $this->assertFalse("This should not happen.");
95 }
96 }
97
98 public function test_check_string_arg_not_ok() {
99 try {
100 $this->mock->_checkStringArg("some_arg", 1);
101 $this->assertFalse("This should not happen.");
102 }
103 catch (\InvalidArgumentException $e) {
104 $this->assertEquals("Argument 'some_arg': expected string, got integer '1'", $e->getMessage());
105 }
106 }
107
109 try {
110 $this->mock->_checkArgInstanceOf("some_arg", $this->mock, ComponentMock::class);
111 }
112 catch (\InvalidArgumentException $e) {
113 $this->assertFalse("This should not happen.");
114 }
115 }
116
118 try {
119 $this->mock->_checkArgInstanceOf("some_arg", $this, ComponentMock::class);
120 $this->assertFalse("This should not happen.");
121 }
122 catch (\InvalidArgumentException $e) {
123 $this->assertEquals("Argument 'some_arg': expected ComponentMock, got ComponentHelperTest", $e->getMessage());
124 }
125 }
126
127
128
130 try {
131 $this->mock->_checkArgIsElement("some_arg", "bar", array("foo", "bar"), "foobar");
132 }
133 catch (\InvalidArgumentException $e) {
134 $this->assertFalse("This should not happen.");
135 }
136 }
137
139 try {
140 $this->mock->_checkArgIsElement("some_arg", "baz", array("foo", "bar"), "foobar");
141 $this->assertFalse("This should not happen.");
142 }
143 catch (\InvalidArgumentException $e) {
144 $this->assertEquals("Argument 'some_arg': expected foobar, got 'baz'", $e->getMessage());
145 }
146 }
147
148 public function test_to_array_with_array() {
149 $foo = array("foo", "bar");
150 $res = $this->mock->_toArray($foo);
151
152 $this->assertEquals($foo, $res);
153 }
154
155 public function test_to_array_with_int() {
156 $foo = 1;
157 $res = $this->mock->_toArray($foo);
158 $this->assertEquals(array($foo), $res);
159 }
160
162 $l = array(new Class1(), new Class1(), new Class1());
163 try {
164 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
165 }
166 catch (\InvalidArgumentException $e) {
167 $this->assertFalse("This should not happen.");
168 }
169 }
170
172 $l = array(new Class1(), new Class1(), new Class2());
173 try {
174 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
175 $this->assertFalse("This should not happen.");
176 }
177 catch (\InvalidArgumentException $e) {
178 $this->assertEquals("Argument 'some_arg': expected Class1, got Class2", $e->getMessage());
179 }
180 }
181
183 $l = array(new Class1(), new Class2(), new Class1());
184 try {
185 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
186 }
187 catch (\InvalidArgumentException $e) {
188 $this->assertFalse("This should not happen.");
189 }
190 }
191
193 $l = array(new Class1(), new Class2(), new Class3(), new Class2());
194 try {
195 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
196 $this->assertFalse("This should not happen.");
197 }
198 catch (\InvalidArgumentException $e) {
199 $this->assertEquals("Argument 'some_arg': expected Class1, Class2, got Class3", $e->getMessage());
200 }
201 }
202
204 $l = array(1, "foo");
205 try {
206 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
207 }
208 catch (\InvalidArgumentException $e) {
209 $this->assertFalse("This should not happen.");
210 }
211 }
212
214 $l = array(1, new Class1());
215 try {
216 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
217 $this->assertFalse("This should not happen.");
218 }
219 catch (\InvalidArgumentException $e) {
220 $this->assertEquals("Argument 'some_arg': expected string, int, got Class1", $e->getMessage());
221 }
222 }
223
224 public function test_check_arg_list_ok() {
225 $l = array("a" => 1, "b" => 2, "c" => 3);
226 try {
227 $this->mock->_checkArgList
228 ( "some_arg"
229 , $l
230 , function($k,$v) {
231 return is_string($k) && is_int($v);
232 }
233 , function($k, $v) {
234 return "expected keys of type string and integer values, got ($k => $v)";
235 });
236 }
237 catch (\InvalidArgumentException $e) {
238 $this->assertFalse("This should not happen.");
239 }
240 }
241
243 $l = array("a" => 1, "b" => 2, 4 => 3);
244 try {
245 $this->mock->_checkArgList
246 ( "some_arg"
247 , $l
248 , function($k,$v) {
249 return is_string($k) && is_int($v);
250 }
251 , function($k, $v) {
252 return "expected keys of type string and integer values, got ($k => $v)";
253 });
254 $this->assertFalse("This should not happen.");
255 }
256 catch (\InvalidArgumentException $e) {
257 $m = "expected keys of type string and integer values, got (4 => 3)";
258 $this->assertEquals("Argument 'some_arg': $m", $e->getMessage());
259 }
260 }
261
263 $l = array("a" => 1, "b" => 2, "c" => "d");
264 try {
265 $this->mock->_checkArgList
266 ( "some_arg"
267 , $l
268 , function($k,$v) {
269 return is_string($k) && is_int($v);
270 }
271 , function($k, $v) {
272 return "expected keys of type string and integer values, got ($k => $v)";
273 });
274
275 $this->assertFalse("This should not happen.");
276 }
277 catch (\InvalidArgumentException $e) {
278 $m = "expected keys of type string and integer values, got (c => d)";
279 $this->assertEquals("Argument 'some_arg': $m", $e->getMessage());
280 }
281 }
282}
global $l
Definition: afr.php:30
An exception for terminatinating execution or to throw for unit testing.
_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)