ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
NotConstraintTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\Refinery\Factory as Refinery;
23use ILIAS\Data\Factory as DataFactory;
24use PHPUnit\Framework\TestCase;
25
26class NotConstraintTest extends TestCase
27{
28 private DataFactory $df;
30 private Refinery $refinery;
33
34 protected function setUp(): void
35 {
36 $this->df = new DataFactory();
37 $this->lng = $this->createMock(ILIAS\Language\Language::class);
38 $this->refinery = new Refinery($this->df, $this->lng);
39
40 $group = $this->refinery->custom();
41
42 $this->not_true = $this->refinery->logical()->not($group->constraint(
43 static function ($v): bool {
44 return true;
45 },
46 "not_true"
47 ));
48
49 $this->not_false = $this->refinery->logical()->not($group->constraint(
50 static function ($v): bool {
51 return false;
52 },
53 "not_false"
54 ));
55 }
56
57 public function testAccepts(): void
58 {
59 $this->assertTrue($this->not_false->accepts(null));
60 }
61
62 public function testNotAccepts(): void
63 {
64 $this->assertFalse($this->not_true->accepts(null));
65 }
66
67 public function testCheckSucceed(): void
68 {
69 $this->not_false->check(null);
70 $this->assertTrue(true); // does not throw
71 }
72
73 public function testCheckFails(): void
74 {
75 $this->expectException(UnexpectedValueException::class);
76 $this->not_true->check(null);
77 }
78
79 public function testNoProblemWith(): void
80 {
81 $this->assertNull($this->not_false->problemWith(null));
82 }
83
84 public function testProblemWith(): void
85 {
86 $this->lng
87 ->expects($this->once())
88 ->method("txt")
89 ->with("not_generic")
90 ->willReturn("-%s-");
91
92 $this->assertEquals("-not_true-", $this->not_true->problemWith(null));
93 }
94
95 public function testRestrictOk(): void
96 {
97 $ok = $this->df->ok(null);
98
99 $res = $this->not_false->applyTo($ok);
100 $this->assertTrue($res->isOk());
101 }
102
103 public function testRestrictNotOk(): void
104 {
105 $not_ok = $this->df->ok(null);
106
107 $res = $this->not_true->applyTo($not_ok);
108 $this->assertFalse($res->isOk());
109 }
110
111 public function testRestrictError(): void
112 {
113 $error = $this->df->error("error");
114
115 $res = $this->not_false->applyTo($error);
116 $this->assertSame($error, $res);
117 }
118
119 public function testWithProblemBuilder(): void
120 {
121 $new_c = $this->not_true->withProblemBuilder(static function (): string {
122 return "This was a fault";
123 });
124 $this->assertEquals("This was a fault", $new_c->problemWith(null));
125 }
126}
Builds data types.
Definition: Factory.php:36
ILIAS Language Language $lng
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.