ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SequentialTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
6 use ILIAS\Data;
7 
14 {
15  protected function setUp()
16  {
17  $this->f = new Validation\Factory(new Data\Factory());
18  }
19 
20  protected function tearDown()
21  {
22  $this->f = null;
23  }
24 
25  public function testAccept()
26  {
27  $constraint = $this->f->sequential(array($this->f->isInt(), $this->f->greaterThan(3), $this->f->lessThan(5)));
28 
29  $this->assertTrue($constraint->accepts(4));
30  $this->assertFalse($constraint->accepts(2));
31  }
32 
33  public function testCheck()
34  {
35  $constraint = $this->f->sequential(array($this->f->isInt(), $this->f->greaterThan(3), $this->f->lessThan(5)));
36  $raised = false;
37 
38  try {
39  $constraint->check(4);
40  } catch (UnexpectedValueException $e) {
41  $raised = true;
42  }
43 
44  $this->assertFalse($raised);
45 
46  try {
47  $constraint->check(2);
48  $raised = false;
49  } catch (UnexpectedValueException $e) {
50  $this->assertEquals("'2' is not greater than '3'.", $e->getMessage());
51  $raised = true;
52  }
53 
54  $this->assertTrue($raised);
55  }
56 
57  public function testProblemWith()
58  {
59  $constraint = $this->f->sequential(array($this->f->isInt(), $this->f->greaterThan(3), $this->f->lessThan(5)));
60 
61  $this->assertNull($constraint->problemWith(4));
62  $this->assertInternalType("string", $constraint->problemWith(2));
63  }
64 
65  public function testRestrict()
66  {
67  $constraint = $this->f->sequential(array($this->f->isInt(), $this->f->greaterThan(3), $this->f->lessThan(5)));
68 
69  $rf = new Data\Factory();
70  $ok = $rf->ok(4);
71  $ok2 = $rf->ok(2);
72  $error = $rf->error("text");
73 
74  $result = $constraint->restrict($ok);
75  $this->assertTrue($result->isOk());
76 
77  $result = $constraint->restrict($ok2);
78  $this->assertTrue($result->isError());
79 
80  $result = $constraint->restrict($error);
81  $this->assertSame($error, $result);
82  }
83 
84  public function testWithProblemBuilder()
85  {
86  $constraint = $this->f->sequential(array($this->f->isInt(), $this->f->greaterThan(3), $this->f->lessThan(5)));
87 
88  $new_constraint = $constraint->withProblemBuilder(function () {
89  return "This was a vault";
90  });
91  $this->assertEquals("This was a vault", $new_constraint->problemWith(2));
92  }
93 
95  {
96  $constraint = $this->f->sequential(array($this->f->isInt(), $this->f->greaterThan(3), $this->f->lessThan(2)));
97  $constraint->accepts("A");
98  $constraint->accepts(2);
99  $constraint->accepts(4);
100 
101  $this->assertEquals("'string' is not an integer.", $constraint->problemWith("A"));
102  $this->assertEquals("'2' is not greater than '3'.", $constraint->problemWith(2));
103  $this->assertEquals("'4' is greater than '2'.", $constraint->problemWith(4));
104  }
105 }
$result
TestCase for the factory of constraints.
$error
Definition: Error.php:17
Builds data types.
Definition: Factory.php:14
Create styles array
The data for the language used.
testCorrectErrorMessagesAfterMultiAccept()