ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
GroupInputTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2018 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
5 require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6 require_once(__DIR__ . "/../../../Base.php");
7 
12 use \ILIAS\Data;
13 
14 abstract class Input1 extends Input
15 {
16 };
17 abstract class Input2 extends Input
18 {
19 };
20 
22 {
26  private $refinery;
27 
28  public function setUp() : void
29  {
30  $this->child1 = $this->createMock(Input1::class);
31  $this->child2 = $this->createMock(Input2::class);
32  $this->data_factory = new Data\Factory;
33  $this->language = $this->createMock(\ilLanguage::class);
34  $this->refinery = new \ILIAS\Refinery\Factory($this->data_factory, $this->language);
35 
36  $this->group = new Group(
37  $this->data_factory,
38  $this->refinery,
39  $this->language,
40  [$this->child1, $this->child2],
41  "LABEL",
42  "BYLINE"
43  );
44  }
45 
47  {
48  $this->assertNotSame($this->child1, $this->child2);
49 
50  $this->child1
51  ->expects($this->once())
52  ->method("withDisabled")
53  ->with(true)
54  ->willReturn($this->child2);
55  $this->child2
56  ->expects($this->once())
57  ->method("withDisabled")
58  ->with(true)
59  ->willReturn($this->child1);
60 
61  $new_group = $this->group->withDisabled(true);
62 
63  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
64  $this->assertInstanceOf(Group::class, $new_group);
65  $this->assertNotSame($this->group, $new_group);
66  }
67 
69  {
70  $this->assertNotSame($this->child1, $this->child2);
71 
72  $this->child1
73  ->expects($this->once())
74  ->method("withRequired")
75  ->with(true)
76  ->willReturn($this->child2);
77  $this->child2
78  ->expects($this->once())
79  ->method("withRequired")
80  ->with(true)
81  ->willReturn($this->child1);
82 
83  $new_group = $this->group->withRequired(true);
84 
85  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
86  $this->assertInstanceOf(Group::class, $new_group);
87  $this->assertNotSame($this->group, $new_group);
88  }
89 
91  {
92  $this->expectException(\InvalidArgumentException::class);
93 
94  $this->group = new Group(
95  $this->data_factory,
96  $this->refinery,
97  $this->language,
98  ["foo", "bar"],
99  "LABEL",
100  "BYLINE"
101  );
102  }
103 
105  {
106  $this->assertNotSame($this->child1, $this->child2);
107 
108  $this->child1
109  ->expects($this->once())
110  ->method("withValue")
111  ->with(1)
112  ->willReturn($this->child2);
113  $this->child1
114  ->expects($this->once())
115  ->method("isClientSideValueOk")
116  ->with(1)
117  ->willReturn(true);
118  $this->child2
119  ->expects($this->once())
120  ->method("withValue")
121  ->with(2)
122  ->willReturn($this->child1);
123  $this->child2
124  ->expects($this->once())
125  ->method("isClientSideValueOk")
126  ->with(2)
127  ->willReturn(true);
128 
129  $new_group = $this->group->withValue([1,2]);
130 
131  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
132  $this->assertInstanceOf(Group::class, $new_group);
133  $this->assertNotSame($this->group, $new_group);
134  }
135 
136  public function testWithValuePreservesKeys()
137  {
138  $this->assertNotSame($this->child1, $this->child2);
139 
140  $this->group = new Group(
141  $this->data_factory,
142  $this->refinery,
143  $this->language,
144  ["child1" => $this->child1, "child2" => $this->child2],
145  "LABEL",
146  "BYLINE"
147  );
148 
149  $this->child1
150  ->method("withValue")
151  ->willReturn($this->child2);
152  $this->child1
153  ->method("isClientSideValueOk")
154  ->willReturn(true);
155  $this->child2
156  ->method("withValue")
157  ->willReturn($this->child1);
158  $this->child2
159  ->method("isClientSideValueOk")
160  ->willReturn(true);
161 
162  $new_group = $this->group->withValue(["child1" => 1,"child2" => 2]);
163 
164  $this->assertEquals(["child1" => $this->child2, "child2" => $this->child1], $new_group->getInputs());
165  }
166 
167 
169  {
170  $this->expectException(\InvalidArgumentException::class);
171 
172  $new_group = $this->group->withValue(1);
173  }
174 
176  {
177  $this->expectException(\InvalidArgumentException::class);
178 
179  $new_group = $this->group->withValue([1]);
180  }
181 
183  {
184  $this->assertNotSame($this->child1, $this->child2);
185 
186  $this->child1
187  ->expects($this->once())
188  ->method("getValue")
189  ->with()
190  ->willReturn("one");
191  $this->child2
192  ->expects($this->once())
193  ->method("getValue")
194  ->with()
195  ->willReturn("two");
196 
197  $vals = $this->group->getValue();
198 
199  $this->assertEquals(["one", "two"], $vals);
200  }
201 
203  {
204  $this->assertNotSame($this->child1, $this->child2);
205 
206  $input_data = $this->createMock(InputData::class);
207 
208  $this->child1
209  ->expects($this->once())
210  ->method("withInput")
211  ->with($input_data)
212  ->willReturn($this->child2);
213  $this->child1
214  ->expects($this->once())
215  ->method("getContent")
216  ->willReturn($this->data_factory->ok("one"));
217  $this->child2
218  ->expects($this->once())
219  ->method("withInput")
220  ->with($input_data)
221  ->willReturn($this->child1);
222  $this->child2
223  ->expects($this->once())
224  ->method("getContent")
225  ->willReturn($this->data_factory->ok("two"));
226 
227  $called = false;
228  $new_group = $this->group
229  ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called) {
230  $called = true;
231  $this->assertEquals(["two", "one"], $v);
232  return "result";
233  }))
234  ->withInput($input_data);
235 
236  $this->assertTrue($called);
237  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
238  $this->assertInstanceOf(Group::class, $new_group);
239  $this->assertNotSame($this->group, $new_group);
240  $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
241  }
242 
244  {
245  $this->assertNotSame($this->child1, $this->child2);
246 
247  $input_data = $this->createMock(InputData::class);
248 
249  $this->child1
250  ->expects($this->once())
251  ->method("withInput")
252  ->with($input_data)
253  ->willReturn($this->child2);
254  $this->child1
255  ->expects($this->once())
256  ->method("getContent")
257  ->willReturn($this->data_factory->error(""));
258  $this->child2
259  ->expects($this->once())
260  ->method("withInput")
261  ->with($input_data)
262  ->willReturn($this->child1);
263  $this->child2
264  ->expects($this->once())
265  ->method("getContent")
266  ->willReturn($this->data_factory->ok("two"));
267 
268  $i18n = "THERE IS SOME ERROR IN THIS GROUP";
269  $this->language
270  ->expects($this->once())
271  ->method("txt")
272  ->with("ui_error_in_group")
273  ->willReturn($i18n);
274 
275  $new_group = $this->group
276  ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) {
277  $this->assertFalse(true, "This should not happen.");
278  }))
279  ->withInput($input_data);
280 
281  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
282  $this->assertInstanceOf(Group::class, $new_group);
283  $this->assertNotSame($this->group, $new_group);
284  $this->assertTrue($new_group->getContent()->isError());
285  }
286 
287  public function testErrorIsI18NOnError()
288  {
289  $this->assertNotSame($this->child1, $this->child2);
290 
291  $input_data = $this->createMock(InputData::class);
292 
293  $this->child1
294  ->method("withInput")
295  ->willReturn($this->child2);
296  $this->child1
297  ->method("getContent")
298  ->willReturn($this->data_factory->error(""));
299  $this->child2
300  ->method("withInput")
301  ->willReturn($this->child1);
302  $this->child2
303  ->method("getContent")
304  ->willReturn($this->data_factory->ok("two"));
305 
306  $i18n = "THERE IS SOME ERROR IN THIS GROUP";
307  $this->language
308  ->expects($this->once())
309  ->method("txt")
310  ->with("ui_error_in_group")
311  ->willReturn($i18n);
312 
313  $new_group = $this->group
314  ->withInput($input_data);
315 
316  $this->assertTrue($new_group->getContent()->isError());
317  $this->assertEquals($i18n, $new_group->getContent()->error());
318  }
319  public function testWithoutChildren()
320  {
321  $group = new Group(
322  $this->data_factory,
323  $this->refinery,
324  $this->language,
325  [],
326  "LABEL",
327  "BYLINE"
328  );
329  $content = $group->getContent();
330  $this->assertInstanceOf(\ILIAS\Data\Result\Ok::class, $content);
331  $this->assertCount(0, $content->value());
332  }
333 }
testWithInputDoesNotApplyOperationsOnError()
This implements the group input.
Definition: Group.php:19
Class ChatMainBarProvider .
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:11
testWithInputCallsChildrenAndAppliesOperations()
testGroupForwardsValuesOnGetValue()
testWithDisabledDisablesChildren()
testWithRequiredRequiresChildren()
testGroupOnlyDoesNoAcceptNonArrayValue()
Provides common functionality for UI tests.
Definition: Base.php:224
testGroupOnlyDoesNoAcceptArrayValuesWithWrongLength()
testGroupMayOnlyHaveInputChildren()
testGroupForwardsValuesOnWithValue()
language()
Definition: language.php:2