ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CaseOfLabelTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Data\Factory as DataFactory;
22use ILIAS\Refinery\Factory as Refinery ;
24use PHPUnit\Framework\TestCase;
25
26class CaseOfLabelTest extends TestCase
27{
28 private const LANGUAGE_KEY = "en";
29 private const SENSELESS_LANGUAGE_KEY = "this_language_key_will_never_exist";
30 private const TEST_STRING_1 = "I am a test string for the title capitalization and I hope that works even if it is complicated :)";
31 private const TEST_STRING_2 = "I switch the computer on and go online";
32 private const TEST_STRING_3 = "Now it is working";
33 private const EXPECTED_RESULT_TEST_STRING_1 = "I Am a Test String for the Title Capitalization and I Hope that Works even if It Is Complicated :)";
34 private const EXPECTED_RESULT_TEST_STRING_2 = "I Switch the Computer on and Go Online";
35 private const EXPECTED_RESULT_TEST_STRING_3 = "Now It Is Working";
36
38 private ?Refinery $f;
39
40 protected function setUp(): void
41 {
42 $dataFactory = new DataFactory();
43
44 $language = $this->createMock(ILIAS\Language\Language::class);
45
46 $this->f = new Refinery($dataFactory, $language);
47 $this->case_of_label_if_possible = $this->f->string()->caseOfLabel(self::LANGUAGE_KEY);
48 }
49
50 protected function tearDown(): void
51 {
52 $this->f = null;
53 $this->case_of_label_if_possible = null;
54 }
55
56 public function testTransform1(): void
57 {
58 $str = $this->case_of_label_if_possible->transform(self::TEST_STRING_1);
59
60 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_1, $str);
61 }
62
63 public function testTransform2(): void
64 {
65 $str = $this->case_of_label_if_possible->transform(self::TEST_STRING_2);
66
67 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_2, $str);
68 }
69
70
71 public function testTransform3(): void
72 {
73 $str = $this->case_of_label_if_possible->transform(self::TEST_STRING_3);
74
75 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_3, $str);
76 }
77
78 public function testTransformFails(): void
79 {
80 $raised = false;
81 try {
82 $arr = [];
83 $next_str = $this->case_of_label_if_possible->transform($arr);
84 } catch (InvalidArgumentException $e) {
85 $raised = true;
86 }
87 $this->assertTrue($raised);
88
89 $raised = false;
90 try {
91 $int = 1001;
92 $next_str = $this->case_of_label_if_possible->transform($int);
93 } catch (InvalidArgumentException $e) {
94 $raised = true;
95 }
96 $this->assertTrue($raised);
97
98 $raised = false;
99 try {
100 $std_class = new stdClass();
101 $next_str = $this->case_of_label_if_possible->transform($std_class);
102 } catch (InvalidArgumentException $e) {
103 $raised = true;
104 }
105 $this->assertTrue($raised);
106 }
107
108 public function testInvoke(): void
109 {
110 $this->case_of_label_if_possible = $this->f->string()->caseOfLabel(self::LANGUAGE_KEY);
111
112 $str = ($this->case_of_label_if_possible)(self::TEST_STRING_1);
113
114 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_1, $str);
115 }
116
117 public function testInvokeFails(): void
118 {
119 $this->case_of_label_if_possible = $this->f->string()->caseOfLabel(self::LANGUAGE_KEY);
120
121 $raised = false;
122 try {
123 $arr = [];
124 $next_str = ($this->case_of_label_if_possible)($arr);
125 } catch (InvalidArgumentException $e) {
126 $raised = true;
127 }
128 $this->assertTrue($raised);
129
130 $raised = false;
131 try {
132 $int = 1001;
133 $next_str = ($this->case_of_label_if_possible)($int);
134 } catch (InvalidArgumentException $e) {
135 $raised = true;
136 }
137 $this->assertTrue($raised);
138
139 $raised = false;
140 try {
141 $std_class = new stdClass();
142 $next_str = ($this->case_of_label_if_possible)($std_class);
143 } catch (InvalidArgumentException $e) {
144 $raised = true;
145 }
146 $this->assertTrue($raised);
147 }
148
150 {
151 $factory = new DataFactory();
152
153 $valueObject = $factory->ok(self::TEST_STRING_1);
154
155 $resultObject = $this->case_of_label_if_possible->applyTo($valueObject);
156
157 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_1, $resultObject->value());
158 $this->assertFalse($resultObject->isError());
159 }
160
162 {
163 $factory = new DataFactory();
164
165 $valueObject = $factory->ok(42);
166
167 $resultObject = $this->case_of_label_if_possible->applyTo($valueObject);
168
169 $this->assertTrue($resultObject->isError());
170 }
171
172 public function testUnknownLanguageKey(): void
173 {
174 $this->case_of_label_if_possible = $this->f->string()->caseOfLabel(self::SENSELESS_LANGUAGE_KEY);
175
176 $raised = false;
177 try {
178 $str = $this->case_of_label_if_possible->transform(self::TEST_STRING_1);
179 } catch (LogicException $e) {
180 $raised = true;
181 }
182 $this->assertTrue($raised);
183 }
184}
const EXPECTED_RESULT_TEST_STRING_1
const EXPECTED_RESULT_TEST_STRING_2
const SENSELESS_LANGUAGE_KEY
testApplyToWithInvalidValueWillLeadToErrorResult()
const EXPECTED_RESULT_TEST_STRING_3
Transformation $case_of_label_if_possible
testApplyToWithValidValueReturnsAnOkResult()
Builds data types.
Definition: Factory.php:36
A transformation is a function from one datatype to another.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.