ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
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;
29 private Constraint $c;
30
31 protected function setUp(): void
32 {
33 $this->df = new DataFactory();
34 $this->c = $this->sequentialConstraint($this->createStub(ILIAS\Language\Language::class));
35 }
36
37 public function testAccepts(): void
38 {
39 $this->assertTrue($this->c->accepts(4));
40 }
41
42 public function testNotAccepts(): void
43 {
44 $this->assertFalse($this->c->accepts(2));
45 }
46
47 public function testCheckSucceed(): void
48 {
49 $this->c->check(4);
50 $this->assertTrue(true); // does not throw
51 }
52
53 public function testCheckFails(): void
54 {
55 $this->expectException(UnexpectedValueException::class);
56 $this->c->check(2);
57 }
58
59 public function testNoProblemWith(): void
60 {
61 $this->assertNull($this->c->problemWith(4));
62 }
63
64 public function testProblemWith1(): void
65 {
66 $lng = $this->createMock(ILIAS\Language\Language::class);
67 $lng
68 ->expects($this->never())
69 ->method('txt');
70
71 $this->assertEquals('not_greater_than_3', $this->sequentialConstraint($lng)->problemWith(2));
72 }
73
74 public function testProblemWith2(): void
75 {
76 $lng = $this->createMock(ILIAS\Language\Language::class);
77 $lng
78 ->expects($this->never())
79 ->method('txt');
80
81 $this->assertEquals('not_less_than_5', $this->sequentialConstraint($lng)->problemWith(6));
82 }
83
84 public function testRestrictOk(): void
85 {
86 $ok = $this->df->ok(4);
87
88 $res = $this->c->applyTo($ok);
89 $this->assertTrue($res->isOk());
90 }
91
92 public function testRestrictNotOk(): void
93 {
94 $not_ok = $this->df->ok(7);
95
96 $res = $this->c->applyTo($not_ok);
97 $this->assertFalse($res->isOk());
98 }
99
100 public function testRestrictError(): void
101 {
102 $error = $this->df->error('error');
103
104 $res = $this->c->applyTo($error);
105 $this->assertSame($error, $res);
106 }
107
108 public function testWithProblemBuilder(): void
109 {
110 $new_c = $this->c->withProblemBuilder(static fn(): string => 'This was a fault');
111 $this->assertEquals('This was a fault', $new_c->problemWith(7));
112 }
113
114 private function sequentialConstraint(ILIAS\Language\Language $lng): Constraint
115 {
116 $refinery = new Refinery($this->df, $lng);
117 $group = $refinery->custom();
118
119 $greater_than_3 = $group->constraint(
120 fn($value) => $value > 3,
121 'not_greater_than_3'
122 );
123
124 $less_than_5 = $group->constraint(
125 fn($value) => $value < 5,
126 'not_less_than_5'
127 );
128
129 return $refinery->logical()->sequential([$greater_than_3, $less_than_5]);
130 }
131}
Builds data types.
Definition: Factory.php:36
sequentialConstraint(ILIAS\Language\Language $lng)
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:26
foreach($_POST as $key=> $value) $res