ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
CustomTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2017 Stefan Hecken <stefan.hecken@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 require_once("libs/composer/vendor/autoload.php");
5 
7 use ILIAS\Data;
8 
9 class MyValidationConstraintsCustom extends Validation\Constraints\Custom
10 {
11  public function _getLngClosure()
12  {
13  return $this->getLngClosure();
14  }
15 }
16 
18 {
19  protected $str_repr;
20 
21  public function __toString()
22  {
23  return $this->str_repr;
24  }
25 }
26 
33 {
37  protected $f = null;
38 
39  public function setUp()
40  {
41  $is_ok = function ($value) {
42  return false;
43  };
44  $this->txt_id = "TXT_ID";
45  $error = function (callable $txt, $value) use ($txt_id) {
46  return $txt($txt_id, $value);
47  };
48  $this->lng = $this->createMock(\ilLanguage::class);
49  $this->constraint = new MyValidationConstraintsCustom($is_ok, $error, new Data\Factory(), $this->lng);
50  }
51 
52  public function testWithProblemBuilder()
53  {
54  $new_constraint = $this->constraint->withProblemBuilder(function () {
55  return "This was a fault";
56  });
57  $this->assertEquals("This was a fault", $new_constraint->problemWith(""));
58  }
59 
61  {
62  $c = $this->constraint->withProblemBuilder(function ($txt) {
63  $this->cls = $txt;
64  return "";
65  });
66  $c->problemWith("");
67  $this->assertTrue(is_callable($this->cls));
68  }
69 
70  public function test_use_txt()
71  {
72  $txt_out = "'%s'";
73  $this->lng
74  ->expects($this->once())
75  ->method("txt")
76  ->with($txt_id)
77  ->willReturn($txt_out);
78 
79  $value = "VALUE";
80  $problem = $this->constraint->problemWith($value);
81 
82  $this->assertEquals(sprintf($txt_out, $value), $problem);
83  }
84 
86  {
87  $lng_closure = $this->constraint->_getLngClosure();
88 
89  $this->expectException(\InvalidArgumentException::class);
90 
91  $lng_closure();
92  }
93 
95  {
96  $lng_closure = $this->constraint->_getLngClosure();
97 
98  $txt_out = "txt";
99  $this->lng
100  ->expects($this->once())
101  ->method("txt")
102  ->with($this->txt_id)
103  ->willReturn($txt_out);
104 
105  $res = $lng_closure($this->txt_id);
106 
107  $this->assertEquals($txt_out, $res);
108  }
109 
111  {
112  $lng_closure = $this->constraint->_getLngClosure();
113 
114  $this->lng
115  ->expects($this->once())
116  ->method("txt")
117  ->with("id")
118  ->willReturn("%s-%s-%s-%s-");
119 
120  $to_string = new MyToStringClass("foo");
121 
122  $res = $lng_closure("id", [], new \stdClass(), "foo", null);
123 
124  $this->assertEquals("array-" . \stdClass::class . "-foo-null-", $res);
125  }
126 }
TestCase for the custom constraints.
Definition: CustomTest.php:32
foreach($_POST as $key=> $value) $res
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
Builds data types.
Definition: Factory.php:14
$txt
Definition: error.php:11