ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 */
4require_once("libs/composer/vendor/autoload.php");
5
7use ILIAS\Data;
8use PHPUnit\Framework\TestCase;
9
11{
12 public function _getLngClosure()
13 {
14 return $this->getLngClosure();
15 }
16}
17
19{
20 protected $str_repr;
21
22 public function __toString()
23 {
24 return $this->str_repr;
25 }
26}
27
33class ValidationConstraintsCustomTest extends TestCase
34{
38 protected $f = null;
39
40 protected $txt_id = '';
41
42 public function setUp() : void
43 {
44 $is_ok = function ($value) {
45 return false;
46 };
47 $this->txt_id = "TXT_ID";
48 $error = function (callable $txt, $value) {
49 return $txt($this->txt_id, $value);
50 };
51 $this->lng = $this->createMock(\ilLanguage::class);
52 $this->constraint = new MyValidationConstraintsConstraint($is_ok, $error, new Data\Factory(), $this->lng);
53 }
54
55 public function testWithProblemBuilder()
56 {
57 $new_constraint = $this->constraint->withProblemBuilder(function () {
58 return "This was a fault";
59 });
60 $this->assertEquals("This was a fault", $new_constraint->problemWith(""));
61 }
62
64 {
65 $c = $this->constraint->withProblemBuilder(function ($txt) {
66 $this->cls = $txt;
67 return "";
68 });
69 $c->problemWith("");
70 $this->assertTrue(is_callable($this->cls));
71 }
72
73 public function test_use_txt()
74 {
75 $txt_out = "'%s'";
76 $this->lng
77 ->expects($this->once())
78 ->method("txt")
79 ->with($this->txt_id)
80 ->willReturn($txt_out);
81
82 $value = "VALUE";
83 $problem = $this->constraint->problemWith($value);
84
85 $this->assertEquals(sprintf($txt_out, $value), $problem);
86 }
87
89 {
90 $lng_closure = $this->constraint->_getLngClosure();
91
92 $this->expectException(\InvalidArgumentException::class);
93
94 $lng_closure();
95 }
96
98 {
99 $lng_closure = $this->constraint->_getLngClosure();
100
101 $txt_out = "txt";
102 $this->lng
103 ->expects($this->once())
104 ->method("txt")
105 ->with($this->txt_id)
106 ->willReturn($txt_out);
107
108 $res = $lng_closure($this->txt_id);
109
110 $this->assertEquals($txt_out, $res);
111 }
112
114 {
115 $lng_closure = $this->constraint->_getLngClosure();
116
117 $this->lng
118 ->expects($this->once())
119 ->method("txt")
120 ->with("id")
121 ->willReturn("%s-%s-%s-%s-");
122
123 $to_string = new MyToStringClass("foo");
124
125 $res = $lng_closure("id", [], new \stdClass(), "foo", null);
126
127 $this->assertEquals("array-" . \stdClass::class . "-foo-null-", $res);
128 }
129}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
TestCase for the custom constraints.
Definition: CustomTest.php:34
$txt
Definition: error.php:13
foreach($_POST as $key=> $value) $res