ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
IsArrayOfConstraintTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2018 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 require_once("libs/composer/vendor/autoload.php");
5 
7 use ILIAS\Data;
8 
10 {
11  public function setUp()
12  {
13  $this->df = new Data\Factory();
14  $this->lng = $this->createMock(\ilLanguage::class);
15  $this->f = new Validation\Factory($this->df, $this->lng);
16 
17  $this->on_element = $this->createMock(Validation\Constraint::class);
18 
19  $this->c = $this->f->isArrayOf($this->on_element);
20  }
21 
22  public function testAccepts()
23  {
24  $this->on_element
25  ->expects($this->exactly(2))
26  ->method("accepts")
27  ->withConsecutive([1], [2])
28  ->will($this->onConsecutiveCalls(true, true));
29 
30  $this->assertTrue($this->c->accepts([1, 2]));
31  }
32 
33  public function testNotAccepts()
34  {
35  $this->on_element
36  ->expects($this->exactly(2))
37  ->method("accepts")
38  ->withConsecutive([1], [2])
39  ->will($this->onConsecutiveCalls(true, false));
40 
41  $this->assertFalse($this->c->accepts([1, 2, 3]));
42  }
43 
44  public function testNotAcceptsNoneArrays()
45  {
46  $this->assertFalse($this->c->accepts(1));
47  }
48 
49  public function testCheckSucceed()
50  {
51  $this->on_element
52  ->expects($this->exactly(2))
53  ->method("accepts")
54  ->withConsecutive([1], [2])
55  ->will($this->onConsecutiveCalls(true, true));
56 
57  $this->c->check([1, 2]);
58  $this->assertTrue(true); // does not throw
59  }
60 
61  public function testCheckFails()
62  {
63  $this->on_element
64  ->expects($this->exactly(2))
65  ->method("accepts")
66  ->withConsecutive([1], [2])
67  ->will($this->onConsecutiveCalls(true, false));
68 
69  $this->expectException(\UnexpectedValueException::class);
70  $this->c->check([1, 2]);
71  }
72 
73 
74  public function testCheckFailsOnNoneArray()
75  {
76  $this->expectException(\UnexpectedValueException::class);
77  $this->c->check(1);
78  }
79 
80  public function testNoProblemWith()
81  {
82  $this->on_element
83  ->expects($this->exactly(2))
84  ->method("accepts")
85  ->withConsecutive([1], [2])
86  ->will($this->onConsecutiveCalls(true, true));
87 
88  $this->assertNull($this->c->problemWith([1, 2]));
89  }
90 
91  public function testProblemWith()
92  {
93  $this->on_element
94  ->expects($this->exactly(3))
95  ->method("problemWith")
96  ->withConsecutive([1], [2], [3])
97  ->will($this->onConsecutiveCalls(null, "2", "3"));
98 
99  $this->lng
100  ->expects($this->exactly(1))
101  ->method("txt")
102  ->withConsecutive(["not_an_array_of"])
103  ->willReturn("-%s-");
104 
105  $this->assertEquals("-2 3-", $this->c->problemWith([1,2,3]));
106  }
107 
108  public function testProblemWithNoneArray()
109  {
110  $this->lng
111  ->expects($this->exactly(1))
112  ->method("txt")
113  ->withConsecutive(["not_an_array"])
114  ->willReturn("-%s-");
115 
116  $this->assertEquals("-integer-", $this->c->problemWith(1));
117  }
118 
119  public function testRestrictOk()
120  {
121  $this->on_element
122  ->expects($this->exactly(2))
123  ->method("accepts")
124  ->withConsecutive([1], [2])
125  ->will($this->onConsecutiveCalls(true, true));
126 
127  $ok = $this->df->ok([1, 2]);
128 
129  $res = $this->c->restrict($ok);
130  $this->assertTrue($res->isOk());
131  }
132 
133  public function testRestrictNotOk()
134  {
135  $this->on_element
136  ->expects($this->exactly(2))
137  ->method("accepts")
138  ->withConsecutive([1], [2])
139  ->will($this->onConsecutiveCalls(true, false));
140 
141  $not_ok = $this->df->ok([1,2]);
142 
143  $res = $this->c->restrict($not_ok);
144  $this->assertFalse($res->isOk());
145  }
146 
148  {
149  $not_ok = $this->df->ok(1);
150 
151  $res = $this->c->restrict($not_ok);
152  $this->assertFalse($res->isOk());
153  }
154 
155  public function testRestrictError()
156  {
157  $error = $this->df->error("error");
158 
159  $res = $this->c->restrict($error);
160  $this->assertSame($error, $res);
161  }
162 
163  public function testWithProblemBuilder()
164  {
165  $new_c = $this->c->withProblemBuilder(function () {
166  return "This was a fault";
167  });
168  $this->assertEquals("This was a fault", $new_c->problemWith(2));
169  }
170 }
foreach($_POST as $key=> $value) $res