ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ComponentHelperTest Class Reference
+ Inheritance diagram for ComponentHelperTest:
+ Collaboration diagram for ComponentHelperTest:

Public Member Functions

 setUp ()
 
 testGetCanonicalName ()
 
 testCheckArgOk ()
 
 testCheckArgNotOk ()
 
 testCheckStringArgOk ()
 
 testCheckStringArgNotOk ()
 
 testCheckBoolArgOk ()
 
 testCheckBoolArgNotOk ()
 
 testCheckArgInstanceofOk ()
 
 testCheckArgInstanceofNotOk ()
 
 testCheckArgIsElementOk ()
 
 testCheckStringArgIsElementNotOk ()
 
 testToArrayWithArray ()
 
 testToArrayWithInt ()
 
 testCheckArgListElementsOk ()
 
 testCheckArgListElementsNoOk ()
 
 testCheckArgListElementsMultiClassOk ()
 
 testCheckArgListElementsMultiClassNotOk ()
 
 testCheckArgListElementsStringOrIntOk ()
 
 testCheckArgListElementsStringOrIntNotOk ()
 
 testCheckArgListOk ()
 
 testCheckArgListNotOk1 ()
 
 testCheckArgListNotOk2 ()
 
 testReduceWith ()
 
 testReduceWithDoesNotModify ()
 
 testReduceWithSubStructureIsTransient ()
 

Protected Attributes

ComponentMock $mock
 

Detailed Description

Member Function Documentation

◆ setUp()

ComponentHelperTest::setUp ( )

Definition at line 102 of file ComponentHelperTest.php.

102 : void
103 {
104 $this->mock = new ComponentMock();
105 }

◆ testCheckArgInstanceofNotOk()

ComponentHelperTest::testCheckArgInstanceofNotOk ( )

Definition at line 158 of file ComponentHelperTest.php.

158 : void
159 {
160 $this->expectException(InvalidArgumentException::class);
161 $this->expectExceptionMessage("Argument 'some_arg': expected ComponentMock, got ComponentHelperTest");
162 $this->mock->_checkArgInstanceOf("some_arg", $this, ComponentMock::class);
163 }

◆ testCheckArgInstanceofOk()

ComponentHelperTest::testCheckArgInstanceofOk ( )

Definition at line 153 of file ComponentHelperTest.php.

153 : void
154 {
155 $this->mock->_checkArgInstanceOf("some_arg", $this->mock, ComponentMock::class);
156 }

◆ testCheckArgIsElementOk()

ComponentHelperTest::testCheckArgIsElementOk ( )

Definition at line 167 of file ComponentHelperTest.php.

167 : void
168 {
169 $this->mock->_checkArgIsElement("some_arg", "bar", array("foo", "bar"), "foobar");
170 }

◆ testCheckArgListElementsMultiClassNotOk()

ComponentHelperTest::testCheckArgListElementsMultiClassNotOk ( )

Definition at line 216 of file ComponentHelperTest.php.

216 : void
217 {
218 $this->expectException(InvalidArgumentException::class);
219 $this->expectExceptionMessage("Argument 'some_arg': expected Class1, Class2, got Class3");
220 $l = array(new Class1(), new Class2(), new Class3(), new Class2());
221 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
222 }

◆ testCheckArgListElementsMultiClassOk()

ComponentHelperTest::testCheckArgListElementsMultiClassOk ( )

Definition at line 210 of file ComponentHelperTest.php.

210 : void
211 {
212 $l = array(new Class1(), new Class2(), new Class1());
213 $this->mock->_checkArgListElements("some_arg", $l, array("Class1", "Class2"));
214 }

◆ testCheckArgListElementsNoOk()

ComponentHelperTest::testCheckArgListElementsNoOk ( )

Definition at line 201 of file ComponentHelperTest.php.

201 : void
202 {
203 $this->expectException(InvalidArgumentException::class);
204 $this->expectExceptionMessage("Argument 'some_arg': expected Class1, got Class2");
205 $l = array(new Class1(), new Class1(), new Class2());
206 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
207 }

◆ testCheckArgListElementsOk()

ComponentHelperTest::testCheckArgListElementsOk ( )

Definition at line 195 of file ComponentHelperTest.php.

195 : void
196 {
197 $l = array(new Class1(), new Class1(), new Class1());
198 $this->mock->_checkArgListElements("some_arg", $l, array("Class1"));
199 }

◆ testCheckArgListElementsStringOrIntNotOk()

ComponentHelperTest::testCheckArgListElementsStringOrIntNotOk ( )

Definition at line 231 of file ComponentHelperTest.php.

231 : void
232 {
233 $this->expectException(InvalidArgumentException::class);
234 $this->expectExceptionMessage("Argument 'some_arg': expected string, int, got Class1");
235 $l = array(1, new Class1());
236 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
237 }

◆ testCheckArgListElementsStringOrIntOk()

ComponentHelperTest::testCheckArgListElementsStringOrIntOk ( )

Definition at line 225 of file ComponentHelperTest.php.

225 : void
226 {
227 $l = array(1, "foo");
228 $this->mock->_checkArgListElements("some_arg", $l, array("string", "int"));
229 }

◆ testCheckArgListNotOk1()

ComponentHelperTest::testCheckArgListNotOk1 ( )

Definition at line 250 of file ComponentHelperTest.php.

250 : void
251 {
252 $m = "expected keys of type string and integer values, got (4 => 3)";
253 $this->expectException(InvalidArgumentException::class);
254 $this->expectExceptionMessage("Argument 'some_arg': $m");
255 $l = array("a" => 1, "b" => 2, 4 => 3);
256 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
257 return is_string($k) && is_int($v);
258 }, function ($k, $v) {
259 return "expected keys of type string and integer values, got ($k => $v)";
260 });
261 }

◆ testCheckArgListNotOk2()

ComponentHelperTest::testCheckArgListNotOk2 ( )

Definition at line 263 of file ComponentHelperTest.php.

263 : void
264 {
265 $m = "expected keys of type string and integer values, got (c => d)";
266 $this->expectException(InvalidArgumentException::class);
267 $this->expectExceptionMessage("Argument 'some_arg': $m");
268 $l = array("a" => 1, "b" => 2, "c" => "d");
269 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
270 return is_string($k) && is_int($v);
271 }, function ($k, $v) {
272 return "expected keys of type string and integer values, got ($k => $v)";
273 });
274 }

◆ testCheckArgListOk()

ComponentHelperTest::testCheckArgListOk ( )

Definition at line 240 of file ComponentHelperTest.php.

240 : void
241 {
242 $l = array("a" => 1, "b" => 2, "c" => 3);
243 $this->mock->_checkArgList("some_arg", $l, function ($k, $v) {
244 return is_string($k) && is_int($v);
245 }, function ($k, $v) {
246 return "expected keys of type string and integer values, got ($k => $v)";
247 });
248 }

◆ testCheckArgNotOk()

ComponentHelperTest::testCheckArgNotOk ( )

Definition at line 119 of file ComponentHelperTest.php.

119 : void
120 {
121 $this->expectException(InvalidArgumentException::class);
122 $this->expectExceptionMessage("Argument 'some_arg': some message");
123 $this->mock->_checkArg("some_arg", false, "some message");
124 }

◆ testCheckArgOk()

ComponentHelperTest::testCheckArgOk ( )

Definition at line 114 of file ComponentHelperTest.php.

114 : void
115 {
116 $this->mock->_checkArg("some_arg", true, "some message");
117 }

◆ testCheckBoolArgNotOk()

ComponentHelperTest::testCheckBoolArgNotOk ( )

Definition at line 145 of file ComponentHelperTest.php.

145 : void
146 {
147 $this->expectException(InvalidArgumentException::class);
148 $this->expectExceptionMessage("Argument 'some_arg': expected bool, got integer '1'");
149 $this->mock->_checkBoolArg("some_arg", 1);
150 }

◆ testCheckBoolArgOk()

ComponentHelperTest::testCheckBoolArgOk ( )

Definition at line 140 of file ComponentHelperTest.php.

140 : void
141 {
142 $this->mock->_checkBoolArg("some_arg", true);
143 }

◆ testCheckStringArgIsElementNotOk()

ComponentHelperTest::testCheckStringArgIsElementNotOk ( )

Definition at line 172 of file ComponentHelperTest.php.

172 : void
173 {
174 $this->expectException(InvalidArgumentException::class);
175 $this->expectExceptionMessage("Argument 'some_arg': expected foobar, got 'baz'");
176 $this->mock->_checkArgIsElement("some_arg", "baz", array("foo", "bar"), "foobar");
177 }

◆ testCheckStringArgNotOk()

ComponentHelperTest::testCheckStringArgNotOk ( )

Definition at line 132 of file ComponentHelperTest.php.

132 : void
133 {
134 $this->expectException(InvalidArgumentException::class);
135 $this->expectExceptionMessage("Argument 'some_arg': expected string, got integer '1'");
136 $this->mock->_checkStringArg("some_arg", 1);
137 }

◆ testCheckStringArgOk()

ComponentHelperTest::testCheckStringArgOk ( )

Definition at line 127 of file ComponentHelperTest.php.

127 : void
128 {
129 $this->mock->_checkStringArg("some_arg", "bar");
130 }

◆ testGetCanonicalName()

ComponentHelperTest::testGetCanonicalName ( )

Definition at line 107 of file ComponentHelperTest.php.

107 : void
108 {
109 $c = new TestComponent("foo");
110 $this->assertEquals("Test Component Test", $c->getCanonicalName());
111 }
$c
Definition: deliver.php:25

References $c.

◆ testReduceWith()

ComponentHelperTest::testReduceWith ( )

Definition at line 276 of file ComponentHelperTest.php.

277 {
278 $a = new ComponentMock();
279 $a->random_data = "A";
280 $b = new ComponentMock();
281 $b->random_data = "B";
282 $c = new ComponentMock();
283 $c->random_data = "C";
284 $c->sub_components = [$a, $b];
285
286 $f = fn($c, $res) => [$c->random_data => $res];
287 $res = $c->reduceWith($f);
288
289 $this->assertEquals(
290 ["C" =>
291 [
292 ["A" => []],
293 ["B" => []],
294 ]
295 ],
296 $res
297 );
298 }
$res
Definition: ltiservices.php:69
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples

References Vendor\Package\$a, Vendor\Package\$b, $c, Vendor\Package\$f, and $res.

◆ testReduceWithDoesNotModify()

ComponentHelperTest::testReduceWithDoesNotModify ( )

Definition at line 300 of file ComponentHelperTest.php.

301 {
302 $a = new ComponentMock();
303 $a->random_data = "A";
304 $b = new ComponentMock();
305 $b->random_data = "B";
306 $c = new ComponentMock();
307 $c->random_data = "C";
308 $c->sub_components = [$a, $b];
309
310 $f = function ($c, $res) {
311 $clone = clone $c;
312 $clone->random_data = strtolower($c->random_data);
313 $clone->sub_components = $res;
314 return $clone;
315 };
316 $c2 = $c->reduceWith($f);
317
318 [$a2, $b2] = $c2->sub_components;
319
320 $this->assertNotEquals(spl_object_id($a), spl_object_id($a2));
321 $this->assertNotEquals(spl_object_id($b), spl_object_id($b2));
322 $this->assertNotEquals(spl_object_id($c), spl_object_id($c2));
323
324 $this->assertEquals("A", $a->random_data);
325 $this->assertEquals("B", $b->random_data);
326 $this->assertEquals("C", $c->random_data);
327 $this->assertEquals([$a, $b], $c->sub_components);
328
329 $this->assertEquals("a", $a2->random_data);
330 $this->assertEquals("b", $b2->random_data);
331 $this->assertEquals("c", $c2->random_data);
332 }

References Vendor\Package\$a, Vendor\Package\$b, $c, Vendor\Package\$f, and $res.

◆ testReduceWithSubStructureIsTransient()

ComponentHelperTest::testReduceWithSubStructureIsTransient ( )

Definition at line 334 of file ComponentHelperTest.php.

335 {
336 $a = new ComponentMock();
337 $a->random_data = "A";
338 $b = new ComponentMock();
339 $b->random_data = "B";
340 $c = new ComponentMock();
341 $c->random_data = "C";
342 $c->sub_components = [$a, $b];
343
344 $f = function ($c, $res) {
345 return [$c, $c->random_data];
346 };
347
348 [$res, $_] = $c->reduceWith($f);
349 $this->assertEquals([$a, $b], $res->sub_components);
350 }

References Vendor\Package\$a, Vendor\Package\$b, $c, Vendor\Package\$f, and $res.

◆ testToArrayWithArray()

ComponentHelperTest::testToArrayWithArray ( )

Definition at line 179 of file ComponentHelperTest.php.

179 : void
180 {
181 $foo = array("foo", "bar");
182 $res = $this->mock->_toArray($foo);
183
184 $this->assertEquals($foo, $res);
185 }

References Vendor\Package\$foo, and $res.

◆ testToArrayWithInt()

ComponentHelperTest::testToArrayWithInt ( )

Definition at line 187 of file ComponentHelperTest.php.

187 : void
188 {
189 $foo = 1;
190 $res = $this->mock->_toArray($foo);
191 $this->assertEquals(array($foo), $res);
192 }

References Vendor\Package\$foo, and $res.

Field Documentation

◆ $mock

ComponentMock ComponentHelperTest::$mock
protected

Definition at line 100 of file ComponentHelperTest.php.


The documentation for this class was generated from the following file: