ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
IsNumericConstraintTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
24 
25 class IsNumericConstraintTest extends TestCase
26 {
27  private DataFactory $df;
28  private ilLanguage $lng;
29  private Refinery $f;
30  private \ILIAS\Refinery\Constraint $c;
31 
32  protected function setUp(): void
33  {
34  $this->df = new DataFactory();
35  $this->lng = $this->createMock(ilLanguage::class);
36 
37  $this->f = new Refinery($this->df, $this->lng);
38 
39  $this->c = $this->f->numeric()->isNumeric();
40  }
41 
42  public function testAccepts1(): void
43  {
44  $this->assertTrue($this->c->accepts(0));
45  }
46 
47  public function testAccepts2(): void
48  {
49  $this->assertTrue($this->c->accepts("1"));
50  }
51 
52  public function testAccepts3(): void
53  {
54  $this->assertTrue($this->c->accepts(1));
55  }
56 
57  public function testAccepts4(): void
58  {
59  $this->assertTrue($this->c->accepts(0x102));
60  }
61 
62  public function testAccepts5(): void
63  {
64  $this->assertTrue($this->c->accepts(0102));
65  }
66 
67  public function testAccepts6(): void
68  {
69  $this->assertTrue($this->c->accepts(0b101));
70  }
71 
72  public function testAccepts7(): void
73  {
74  $this->assertTrue($this->c->accepts(192e0));
75  }
76 
77  public function testAccepts8(): void
78  {
79  $this->assertTrue($this->c->accepts(9.1));
80  }
81 
82  public function testNotAccepts1(): void
83  {
84  $this->assertFalse($this->c->accepts(null));
85  }
86 
87  public function testNotAccepts2(): void
88  {
89  $this->assertFalse($this->c->accepts("foo"));
90  }
91 
92  public function testCheckSucceed(): void
93  {
94  $this->c->check(2);
95  $this->assertTrue(true); // does not throw
96  }
97 
98  public function testCheckFails(): void
99  {
100  $this->lng
101  ->method('txt')
102  ->willReturnCallback(static function (string $value): string {
103  return $value;
104  })
105  ;
106  $this->expectException(\UnexpectedValueException::class);
107  $this->c->check("");
108  }
109 
110  public function testNoProblemWith(): void
111  {
112  $this->assertNull($this->c->problemWith(2));
113  }
114 
115  public function testProblemWith(): void
116  {
117  $this->lng
118  ->expects($this->once())
119  ->method("txt")
120  ->with("not_numeric")
121  ->willReturn("-%s-");
122 
123  $this->assertEquals("-aa-", $this->c->problemWith("aa"));
124  }
125 
126  public function testRestrictOk(): void
127  {
128  $ok = $this->df->ok(2);
129 
130  $res = $this->c->applyTo($ok);
131  $this->assertTrue($res->isOk());
132  }
133 
134  public function testRestrictNotOk(): void
135  {
136  $not_ok = $this->df->ok("");
137 
138  $this->lng
139  ->expects($this->once())
140  ->method("txt")
141  ->with("not_numeric_empty_string")
142  ->willReturn("-%s-");
143 
144  $res = $this->c->applyTo($not_ok);
145  $this->assertFalse($res->isOk());
146  }
147 
148  public function testRestrictError(): void
149  {
150  $error = $this->df->error("error");
151 
152  $res = $this->c->applyTo($error);
153  $this->assertSame($error, $res);
154  }
155 
156  public function testWithProblemBuilder(): void
157  {
158  $new_c = $this->c->withProblemBuilder(static function (): string {
159  return "This was a fault";
160  });
161  $this->assertEquals("This was a fault", $new_c->problemWith(""));
162  }
163 }
$res
Definition: ltiservices.php:69
ILIAS Refinery Constraint $c