ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
HasMinLengthConstraintTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use ILIAS\Data\Factory as DataFactory;
24use PHPUnit\Framework\TestCase;
25
26class HasMinLengthConstraintTest extends TestCase
27{
28 private DataFactory $df;
30 private int $min_length;
31 private Constraint $c;
32
33 protected function setUp(): void
34 {
35 $this->df = new DataFactory();
36 $this->lng = $this->createMock(ILIAS\Language\Language::class);
37
38 $this->min_length = 10;
39
40 $this->c = new HasMinLength(
41 $this->min_length,
42 $this->df,
43 $this->lng
44 );
45 }
46
47 public function testAccepts1(): void
48 {
49 $this->assertTrue($this->c->accepts("1234567890"));
50 }
51
52 public function testAccepts2(): void
53 {
54 $this->assertTrue($this->c->accepts("12345678901"));
55 }
56
57 public function testNotAccepts(): void
58 {
59 $this->assertFalse($this->c->accepts("123456789"));
60 }
61
62 public function testCheckSucceed(): void
63 {
64 $this->c->check("1234567890");
65 $this->assertTrue(true); // does not throw
66 }
67
68 public function testCheckFails(): void
69 {
70 $this->expectException(UnexpectedValueException::class);
71 $this->c->check("");
72 }
73
74 public function testNoProblemWith(): void
75 {
76 $this->assertNull($this->c->problemWith("1234567890"));
77 }
78
79 public function testProblemWith(): void
80 {
81 $this->lng
82 ->expects($this->once())
83 ->method("txt")
84 ->with("not_min_length")
85 ->willReturn("-%s-%s-");
86
87 $this->assertEquals("-3-10-", $this->c->problemWith("123"));
88 }
89
90 public function testRestrictOk(): void
91 {
92 $ok = $this->df->ok("1234567890");
93
94 $res = $this->c->applyTo($ok);
95 $this->assertTrue($res->isOk());
96 }
97
98 public function testRestrictNotOk(): void
99 {
100 $not_ok = $this->df->ok("1234");
101
102 $res = $this->c->applyTo($not_ok);
103 $this->assertFalse($res->isOk());
104 }
105
106 public function testRestrictError(): void
107 {
108 $error = $this->df->error("error");
109
110 $res = $this->c->applyTo($error);
111 $this->assertSame($error, $res);
112 }
113
114 public function testWithProblemBuilder(): void
115 {
116 $new_c = $this->c->withProblemBuilder(static function (): string {
117 return "This was a fault";
118 });
119 $this->assertEquals("This was a fault", $new_c->problemWith(""));
120 }
121}
Builds data types.
Definition: Factory.php:36
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
$res
Definition: ltiservices.php:69
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.