ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
CustomTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
25 class CustomTest extends TestCase
26 {
27  private string $txt_id = '';
30 
31  protected function setUp(): void
32  {
33  $is_ok = static function ($value): bool {
34  return false;
35  };
36  $this->txt_id = "TXT_ID";
37  $error = function (callable $txt, $value): string {
38  return $txt($this->txt_id, $value);
39  };
40  $this->lng = $this->createMock(ILIAS\Language\Language::class);
41  $this->constraint = new class ($is_ok, $error, new DataFactory(), $this->lng) extends CustomConstraint {
42  public function _getLngClosure(): Closure
43  {
44  return $this->getLngClosure();
45  }
46  };
47  }
48 
49  public function testWithProblemBuilder(): void
50  {
51  $new_constraint = $this->constraint->withProblemBuilder(static function (): string {
52  return "This was a fault";
53  });
54  $this->assertEquals("This was a fault", $new_constraint->problemWith(""));
55  }
56 
57  public function testProblemBuilderRetrievesLngClosure(): void
58  {
59  $cls = null;
60  $c = $this->constraint->withProblemBuilder(function ($txt) use (&$cls): string {
61  $cls = $txt;
62  return "";
63  });
64  $c->problemWith("");
65  $this->assertIsCallable($cls);
66  }
67 
68  public function test_use_txt(): void
69  {
70  $txt_out = "'%s'";
71  $this->lng
72  ->expects($this->once())
73  ->method("txt")
74  ->with($this->txt_id)
75  ->willReturn($txt_out);
76 
77  $value = "VALUE";
78  $problem = $this->constraint->problemWith($value);
79 
80  $this->assertEquals(sprintf($txt_out, $value), $problem);
81  }
82 
83  public function test_exception_on_no_parameter(): void
84  {
85  $lng_closure = $this->constraint->_getLngClosure();
86 
87  $this->expectException(ArgumentCountError::class);
88 
89  $lng_closure();
90  }
91 
92  public function test_no_sprintf_on_one_parameter(): void
93  {
94  $lng_closure = $this->constraint->_getLngClosure();
95 
96  $txt_out = "txt";
97  $this->lng
98  ->expects($this->once())
99  ->method("txt")
100  ->with($this->txt_id)
101  ->willReturn($txt_out);
102 
103  $res = $lng_closure($this->txt_id);
104 
105  $this->assertEquals($txt_out, $res);
106  }
107 
109  {
110  $lng_closure = $this->constraint->_getLngClosure();
111 
112  $this->lng
113  ->expects($this->once())
114  ->method("txt")
115  ->with("id")
116  ->willReturn("%s-%s-%s-%s-");
117 
118  $res = $lng_closure("id", [], new stdClass(), "foo", null);
119 
120  $this->assertEquals("array-" . stdClass::class . "-foo-null-", $res);
121  }
122 }
testWithProblemBuilder()
Definition: CustomTest.php:49
$res
Definition: ltiservices.php:66
Interface Observer Contains several chained tasks and infos about them.
test_gracefully_handle_arrays_and_objects()
Definition: CustomTest.php:108
$c
Definition: deliver.php:25
testProblemBuilderRetrievesLngClosure()
Definition: CustomTest.php:57
string $txt_id
Definition: CustomTest.php:27
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
ILIAS Language Language $lng
Definition: CustomTest.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getLngClosure()
Get the closure to be passed to the error-function that does i18n and sprintf.
$txt
Definition: error.php:31
test_no_sprintf_on_one_parameter()
Definition: CustomTest.php:92
test_exception_on_no_parameter()
Definition: CustomTest.php:83
CustomConstraint $constraint
Definition: CustomTest.php:29