ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
SequentialConstraintTest.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 SequentialConstraintTest extends TestCase
27{
28 private DataFactory $df;
30 private Refinery $refinery;
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 $this->refinery = new Refinery($this->df, $this->lng);
38
39 $group = $this->refinery->custom();
40
41 $greater_than_3 = $group->constraint(
42 function ($value) {
43 return $value > 3;
44 },
45 "not_greater_than_3"
46 );
47
48 $less_than_5 = $group->constraint(
49 function ($value) {
50 return $value < 5;
51 },
52 "not_less_than_5"
53 );
54
55 $this->c = $this->refinery
56 ->logical()
57 ->sequential([$greater_than_3, $less_than_5]);
58 }
59
60 public function testAccepts(): void
61 {
62 $this->assertTrue($this->c->accepts(4));
63 }
64
65 public function testNotAccepts(): void
66 {
67 $this->assertFalse($this->c->accepts(2));
68 }
69
70 public function testCheckSucceed(): void
71 {
72 $this->c->check(4);
73 $this->assertTrue(true); // does not throw
74 }
75
76 public function testCheckFails(): void
77 {
78 $this->expectException(UnexpectedValueException::class);
79 $this->c->check(2);
80 }
81
82 public function testNoProblemWith(): void
83 {
84 $this->assertNull($this->c->problemWith(4));
85 }
86
87 public function testProblemWith1(): void
88 {
89 $this->lng
90 ->expects($this->never())
91 ->method("txt");
92
93 $this->assertEquals("not_greater_than_3", $this->c->problemWith(2));
94 }
95
96 public function testProblemWith2(): void
97 {
98 $this->lng
99 ->expects($this->never())
100 ->method("txt");
101
102 $this->assertEquals("not_less_than_5", $this->c->problemWith(6));
103 }
104
105 public function testRestrictOk(): void
106 {
107 $ok = $this->df->ok(4);
108
109 $res = $this->c->applyTo($ok);
110 $this->assertTrue($res->isOk());
111 }
112
113 public function testRestrictNotOk(): void
114 {
115 $not_ok = $this->df->ok(7);
116
117 $res = $this->c->applyTo($not_ok);
118 $this->assertFalse($res->isOk());
119 }
120
121 public function testRestrictError(): void
122 {
123 $error = $this->df->error("error");
124
125 $res = $this->c->applyTo($error);
126 $this->assertSame($error, $res);
127 }
128
129 public function testWithProblemBuilder(): void
130 {
131 $new_c = $this->c->withProblemBuilder(static function (): string {
132 return "This was a fault";
133 });
134 $this->assertEquals("This was a fault", $new_c->problemWith(7));
135 }
136}
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.