ILIAS  release_7 Revision v7.30-3-g800a261c036
CaseOfLabelTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5use ILIAS\Data;
8use PHPUnit\Framework\TestCase;
9
15class CaseOfLabelTest extends TestCase
16{
17 const LANGUAGE_KEY = "en";
18 const SENSELESS_LANGUAGE_KEY = "this_language_key_will_never_exist";
19 const TEST_STRING_1 = "I am a test string for the title capitalization and I hope that works even if it is complicated :)";
20 const TEST_STRING_2 = "I switch the computer on and go online";
21 const TEST_STRING_3 = "Now it is working";
22 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 :)";
23 const EXPECTED_RESULT_TEST_STRING_2 = "I Switch the Computer on and Go Online";
24 const EXPECTED_RESULT_TEST_STRING_3 = "Now It Is Working";
32 protected $f;
33
34
38 protected function setUp() : void
39 {
40 $dataFactory = new Data\Factory();
41
42 $language = $this->createMock('\\' . ilLanguage::class);
43
44 $this->f = new Refinery\Factory($dataFactory, $language);
45 $this->case_of_label_if_possible = $this->f->string()->caseOfLabel(self::LANGUAGE_KEY);
46 }
47
48
52 protected function tearDown() : void
53 {
54 $this->f = null;
55 $this->case_of_label_if_possible = null;
56 }
57
58
62 public function testTransform1() : void
63 {
64 $str = $this->case_of_label_if_possible->transform(self::TEST_STRING_1);
65
66 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_1, $str);
67 }
68
69
73 public function testTransform2() : void
74 {
75 $str = $this->case_of_label_if_possible->transform(self::TEST_STRING_2);
76
77 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_2, $str);
78 }
79
80
84 public function testTransform3() : void
85 {
86 $str = $this->case_of_label_if_possible->transform(self::TEST_STRING_3);
87
88 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_3, $str);
89 }
90
91
95 public function testTransformFails() : void
96 {
97 $raised = false;
98 try {
99 $arr = [];
100 $next_str = $this->case_of_label_if_possible->transform($arr);
101 } catch (InvalidArgumentException $e) {
102 $raised = true;
103 }
104 $this->assertTrue($raised);
105
106 $raised = false;
107 try {
108 $int = 1001;
109 $next_str = $this->case_of_label_if_possible->transform($int);
110 } catch (InvalidArgumentException $e) {
111 $raised = true;
112 }
113 $this->assertTrue($raised);
114
115 $raised = false;
116 try {
117 $std_class = new stdClass();
118 $next_str = $this->case_of_label_if_possible->transform($std_class);
119 } catch (InvalidArgumentException $e) {
120 $raised = true;
121 }
122 $this->assertTrue($raised);
123 }
124
125
129 public function testInvoke() : void
130 {
131 $this->case_of_label_if_possible = $this->f->string()->caseOfLabel(self::LANGUAGE_KEY);
132
133 $str = ($this->case_of_label_if_possible)(self::TEST_STRING_1);
134
135 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_1, $str);
136 }
137
138
142 public function testInvokeFails() : void
143 {
144 $this->case_of_label_if_possible = $this->f->string()->caseOfLabel(self::LANGUAGE_KEY);
145
146 $raised = false;
147 try {
148 $arr = [];
149 $next_str = ($this->case_of_label_if_possible)($arr);
150 } catch (InvalidArgumentException $e) {
151 $raised = true;
152 }
153 $this->assertTrue($raised);
154
155 $raised = false;
156 try {
157 $int = 1001;
158 $next_str = ($this->case_of_label_if_possible)($int);
159 } catch (InvalidArgumentException $e) {
160 $raised = true;
161 }
162 $this->assertTrue($raised);
163
164 $raised = false;
165 try {
166 $std_class = new stdClass();
167 $next_str = ($this->case_of_label_if_possible)($std_class);
168 } catch (InvalidArgumentException $e) {
169 $raised = true;
170 }
171 $this->assertTrue($raised);
172 }
173
174
179 {
180 $factory = new Data\Factory();
181
182 $valueObject = $factory->ok(self::TEST_STRING_1);
183
184 $resultObject = $this->case_of_label_if_possible->applyTo($valueObject);
185
186 $this->assertEquals(self::EXPECTED_RESULT_TEST_STRING_1, $resultObject->value());
187 $this->assertFalse($resultObject->isError());
188 }
189
190
195 {
196 $factory = new Data\Factory();
197
198 $valueObject = $factory->ok(42);
199
200 $resultObject = $this->case_of_label_if_possible->applyTo($valueObject);
201
202 $this->assertTrue($resultObject->isError());
203 }
204
205
209 public function testUnknownLanguageKey() : void
210 {
211 $this->case_of_label_if_possible = $this->f->string()->caseOfLabel(self::SENSELESS_LANGUAGE_KEY);
212
213 $raised = false;
214 try {
215 $str = $this->case_of_label_if_possible->transform(self::TEST_STRING_1);
216 } catch (LogicException $e) {
217 $raised = true;
218 }
219 $this->assertTrue($raised);
220 }
221}
An exception for terminatinating execution or to throw for unit testing.
Class CaseOfLabelTest.
const EXPECTED_RESULT_TEST_STRING_1
const EXPECTED_RESULT_TEST_STRING_2
const SENSELESS_LANGUAGE_KEY
testApplyToWithInvalidValueWillLeadToErrorResult()
const EXPECTED_RESULT_TEST_STRING_3
testApplyToWithValidValueReturnsAnOkResult()
Builds data types.
Definition: Factory.php:20
$factory
Definition: metadata.php:58