ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
CustomTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
24 
26 {
27  public function _getLngClosure(): Closure
28  {
29  return $this->getLngClosure();
30  }
31 }
32 
34 {
35  private string $str_repr = '';
36 
37  public function __toString(): string
38  {
39  return $this->str_repr;
40  }
41 }
42 
43 class ValidationConstraintsCustomTest extends TestCase
44 {
45  private string $txt_id = '';
46  private ilLanguage $lng;
48 
49  protected function setUp(): void
50  {
51  $is_ok = static function ($value): bool {
52  return false;
53  };
54  $this->txt_id = "TXT_ID";
55  $error = function (callable $txt, $value): string {
56  return $txt($this->txt_id, $value);
57  };
58  $this->lng = $this->createMock(ilLanguage::class);
59  $this->constraint = new MyValidationConstraintsConstraint($is_ok, $error, new DataFactory(), $this->lng);
60  }
61 
62  public function testWithProblemBuilder(): void
63  {
64  $new_constraint = $this->constraint->withProblemBuilder(static function (): string {
65  return "This was a fault";
66  });
67  $this->assertEquals("This was a fault", $new_constraint->problemWith(""));
68  }
69 
70  public function testProblemBuilderRetrievesLngClosure(): void
71  {
72  $cls = null;
73  $c = $this->constraint->withProblemBuilder(function ($txt) use (&$cls): string {
74  $cls = $txt;
75  return "";
76  });
77  $c->problemWith("");
78  $this->assertIsCallable($cls);
79  }
80 
81  public function test_use_txt(): void
82  {
83  $txt_out = "'%s'";
84  $this->lng
85  ->expects($this->once())
86  ->method("txt")
87  ->with($this->txt_id)
88  ->willReturn($txt_out);
89 
90  $value = "VALUE";
91  $problem = $this->constraint->problemWith($value);
92 
93  $this->assertEquals(sprintf($txt_out, $value), $problem);
94  }
95 
96  public function test_exception_on_no_parameter(): void
97  {
98  $lng_closure = $this->constraint->_getLngClosure();
99 
100  $this->expectException(InvalidArgumentException::class);
101 
102  $lng_closure();
103  }
104 
105  public function test_no_sprintf_on_one_parameter(): void
106  {
107  $lng_closure = $this->constraint->_getLngClosure();
108 
109  $txt_out = "txt";
110  $this->lng
111  ->expects($this->once())
112  ->method("txt")
113  ->with($this->txt_id)
114  ->willReturn($txt_out);
115 
116  $res = $lng_closure($this->txt_id);
117 
118  $this->assertEquals($txt_out, $res);
119  }
120 
122  {
123  $lng_closure = $this->constraint->_getLngClosure();
124 
125  $this->lng
126  ->expects($this->once())
127  ->method("txt")
128  ->with("id")
129  ->willReturn("%s-%s-%s-%s-");
130 
131  $to_string = new MyToStringClass();
132 
133  $res = $lng_closure("id", [], new stdClass(), "foo", null);
134 
135  $this->assertEquals("array-" . stdClass::class . "-foo-null-", $res);
136  }
137 }
$res
Definition: ltiservices.php:69
$c
Definition: cli.php:38
MyValidationConstraintsConstraint $constraint
Definition: CustomTest.php:47
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.
$txt
Definition: error.php:13