ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
IsNumericConstraintTest.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 */
4require_once("libs/composer/vendor/autoload.php");
5
7use 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->c = $this->f->isNumeric();
18 }
19
20 public function testAccepts1()
21 {
22 $this->assertTrue($this->c->accepts(0));
23 }
24
25 public function testAccepts2()
26 {
27 $this->assertTrue($this->c->accepts("1"));
28 }
29
30 public function testAccepts3()
31 {
32 $this->assertTrue($this->c->accepts(1));
33 }
34
35 public function testAccepts4()
36 {
37 $this->assertTrue($this->c->accepts(0x102));
38 }
39
40 public function testAccepts5()
41 {
42 $this->assertTrue($this->c->accepts(0102));
43 }
44
45 public function testAccepts6()
46 {
47 $this->assertTrue($this->c->accepts(0b101));
48 }
49
50 public function testAccepts7()
51 {
52 $this->assertTrue($this->c->accepts(192e0));
53 }
54
55 public function testAccepts8()
56 {
57 $this->assertTrue($this->c->accepts(9.1));
58 }
59
60 public function testNotAccepts1()
61 {
62 $this->assertFalse($this->c->accepts(null));
63 }
64
65 public function testNotAccepts2()
66 {
67 $this->assertFalse($this->c->accepts("foo"));
68 }
69
70 public function testCheckSucceed()
71 {
72 $this->c->check(2);
73 $this->assertTrue(true); // does not throw
74 }
75
76 public function testCheckFails()
77 {
78 $this->expectException(\UnexpectedValueException::class);
79 $this->c->check("");
80 }
81
82 public function testNoProblemWith()
83 {
84 $this->assertNull($this->c->problemWith(2));
85 }
86
87 public function testProblemWith()
88 {
89 $this->lng
90 ->expects($this->once())
91 ->method("txt")
92 ->with("not_numeric")
93 ->willReturn("-%s-");
94
95 $this->assertEquals("-aa-", $this->c->problemWith("aa"));
96 }
97
98 public function testRestrictOk()
99 {
100 $ok = $this->df->ok(2);
101
102 $res = $this->c->restrict($ok);
103 $this->assertTrue($res->isOk());
104 }
105
106 public function testRestrictNotOk()
107 {
108 $not_ok = $this->df->ok("");
109
110 $res = $this->c->restrict($not_ok);
111 $this->assertFalse($res->isOk());
112 }
113
114 public function testRestrictError()
115 {
116 $error = $this->df->error("error");
117
118 $res = $this->c->restrict($error);
119 $this->assertSame($error, $res);
120 }
121
122 public function testWithProblemBuilder()
123 {
124 $new_c = $this->c->withProblemBuilder(function () {
125 return "This was a fault";
126 });
127 $this->assertEquals("This was a fault", $new_c->problemWith(""));
128 }
129}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:15
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
foreach($_POST as $key=> $value) $res