PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / apimatic / core / tests / UtilsTest.php
ameliabooking / vendor / apimatic / core / tests Last commit date
Mocking 1 year ago ApiCallTest.php 1 year ago AuthenticationTest.php 1 year ago ClientTest.php 1 year ago CoreTestCaseTest.php 1 year ago EndToEndTest.php 1 year ago LoggerTest.php 1 year ago TypesTest.php 1 year ago UtilsTest.php 1 year ago bootstrap.php 1 year ago
UtilsTest.php
468 lines
1 <?php
2
3 namespace Core\Tests;
4
5 use Core\Tests\Mocking\Other\MockClass;
6 use Core\Utils\CoreHelper;
7 use Core\Utils\DateHelper;
8 use Core\Utils\XmlDeserializer;
9 use Core\Utils\XmlSerializer;
10 use DateTime;
11 use Exception;
12 use InvalidArgumentException;
13 use PHPUnit\Framework\TestCase;
14
15 class UtilsTest extends TestCase
16 {
17 public function testXmlSerialization()
18 {
19 $xmlSerializer = new XMLSerializer(['formatOutput' => true]);
20 $res = $xmlSerializer->serialize('mockClass', new MockClass([34, 'asad']));
21 $this->assertEquals("<?xml version=\"1.0\"?>\n" .
22 "<mockClass attr=\"this is attribute\">\n" .
23 " <body>34</body>\n" .
24 " <body>asad</body>\n" .
25 " <new1>this is new</new1>\n" .
26 " <new2>\n" .
27 " <entry key=\"key1\">val1</entry>\n" .
28 " <entry key=\"key2\">val2</entry>\n" .
29 " </new2>\n" .
30 "</mockClass>\n", $res);
31
32 $xmlSerializer = new XMLSerializer([]);
33 $res = $xmlSerializer->serialize('root', true);
34 $this->assertEquals("<?xml version=\"1.0\"?>\n<root>true</root>\n", $res);
35 }
36
37 public function testXmlDeserialization()
38 {
39 $xmlDeSerializer = new XmlDeserializer();
40 $input = "<?xml version=\"1.0\"?>\n<root>23</root>";
41 $res = $xmlDeSerializer->deserialize($input, 'root', 'int');
42 $this->assertEquals(23, $res);
43 $res = $xmlDeSerializer->deserialize($input, 'root', '?int');
44 $this->assertEquals(23, $res);
45 $input = "<?xml version=\"1.0\"?>\n<root>true</root>";
46 $res = $xmlDeSerializer->deserialize($input, 'root', 'bool');
47 $this->assertEquals(true, $res);
48 $input = "<?xml version=\"1.0\"?>\n<root>false</root>";
49 $res = $xmlDeSerializer->deserialize($input, 'root', 'bool');
50 $this->assertEquals(false, $res);
51 $input = "<?xml version=\"1.0\"?>\n<root>2.3</root>";
52 $res = $xmlDeSerializer->deserialize($input, 'root', 'float');
53 $this->assertEquals(2.3, $res);
54
55 $input = "<?xml version=\"1.0\"?>\n<root></root>";
56 $res = $xmlDeSerializer->deserialize($input, 'abc', '?int');
57 $this->assertNull($res);
58 $res = $xmlDeSerializer->deserializeToArray($input, 'abc', 'item', '?float');
59 $this->assertNull($res);
60 $res = $xmlDeSerializer->deserializeToMap($input, 'abc', '?float');
61 $this->assertNull($res);
62 }
63
64 public function testXmlDeserializationFailure1()
65 {
66 $this->expectException(Exception::class);
67 $this->expectExceptionMessage('Required value not found at XML path "/root[1]" during deserialization.');
68
69 $xmlDeSerializer = new XmlDeserializer();
70 $input = "<?xml version=\"1.0\"?>\n<abc>23</abc>";
71 $xmlDeSerializer->deserialize($input, 'root', 'int');
72 }
73
74 public function testXmlDeserializationFailureTypeBool()
75 {
76 $this->expectException(Exception::class);
77 $this->expectExceptionMessage('Expected value of type "bool" but got value "2.3" at XML path ' .
78 '"/root" during deserialization.');
79
80 $xmlDeSerializer = new XmlDeserializer();
81 $input = "<?xml version=\"1.0\"?>\n<root>2.3</root>";
82 $xmlDeSerializer->deserialize($input, 'root', 'bool');
83 }
84
85 public function testXmlDeserializationFailureTypeInt()
86 {
87 $this->expectException(Exception::class);
88 $this->expectExceptionMessage('Expected value of type "int" but got value ""asad"" at XML path ' .
89 '"/root" during deserialization.');
90
91 $xmlDeSerializer = new XmlDeserializer();
92 $input = "<?xml version=\"1.0\"?>\n<root>\"asad\"</root>";
93 $xmlDeSerializer->deserialize($input, 'root', 'int');
94 }
95
96 public function testXmlDeserializationFailureTypeFloat()
97 {
98 $this->expectException(Exception::class);
99 $this->expectExceptionMessage('Expected value of type "float" but got value ""asad"" at XML path ' .
100 '"/root" during deserialization.');
101
102 $xmlDeSerializer = new XmlDeserializer();
103 $input = "<?xml version=\"1.0\"?>\n<root>\"asad\"</root>";
104 $xmlDeSerializer->deserialize($input, 'root', 'float');
105 }
106
107 public function testCoreHelperDeserialize()
108 {
109 $input = '{"key": "my value"}';
110 $res = CoreHelper::deserialize($input);
111 $this->assertIsArray($res);
112 $this->assertEquals("my value", $res['key']);
113 }
114
115 public function testCoreHelperSerializeNull()
116 {
117 $this->assertEquals(null, CoreHelper::serialize(null));
118 }
119
120 public function testCoreHelperValidateUrl()
121 {
122 $this->expectException(InvalidArgumentException::class);
123 $this->expectExceptionMessage('Invalid Url format.');
124 CoreHelper::validateUrl('some/invalid/url/format');
125 }
126
127 public function testCoreHelperValidateUrlForwardSlashesFix()
128 {
129 $validated = CoreHelper::validateUrl('https://google.com');
130 $this->assertEquals('https://google.com', $validated);
131
132 $validated = CoreHelper::validateUrl('https://google.com/');
133 $this->assertEquals('https://google.com', $validated);
134
135 $validated = CoreHelper::validateUrl('https://google.com///apimatic///');
136 $this->assertEquals('https://google.com/apimatic', $validated);
137 }
138
139 public function testCoreHelperCheckValueOrValuesInList()
140 {
141 $list = ['string', 'int', 'float', '1'];
142 $this->assertTrue(CoreHelper::checkValueOrValuesInList(null, $list));
143
144 $this->assertFalse(CoreHelper::checkValueOrValuesInList(1, $list));
145 $this->assertTrue(CoreHelper::checkValueOrValuesInList('float', $list));
146
147 $this->assertFalse(CoreHelper::checkValueOrValuesInList(['int', 'unknown'], $list));
148 $this->assertTrue(CoreHelper::checkValueOrValuesInList(['float', 'int'], $list));
149
150 $this->assertFalse(CoreHelper::checkValueOrValuesInList(['int', ['float', 'unknown']], $list));
151 $this->assertTrue(CoreHelper::checkValueOrValuesInList(['float', ['int', 'string']], $list));
152 }
153
154 public function testCoreHelperClone()
155 {
156 $mockClass = new MockClass([]);
157 $list = ['some string', 1, [false, $mockClass]];
158
159 $newList = $list;
160 $this->assertEquals($list, $newList);
161 $newList[2][1]->addAdditionalProperty('real', 214);
162 $this->assertEquals($list, $newList);
163
164 $clonedList = CoreHelper::clone($list);
165 $this->assertEquals($list, $clonedList);
166 $clonedList[2][1]->addAdditionalProperty('newValue', 12);
167 $list[2][1]->addAdditionalProperty('real2', 14);
168 $this->assertNotEquals($list, $clonedList);
169 $this->assertEquals(
170 '["some string",1,[false,{"body":[],"real":214,"real2":14}]]',
171 CoreHelper::serialize($list)
172 );
173 $this->assertEquals(
174 '["some string",1,[false,{"body":[],"real":214,"newValue":12}]]',
175 CoreHelper::serialize($clonedList)
176 );
177 }
178
179 public function testCoreHelperConvertToNullableString()
180 {
181 $this->assertEquals(null, CoreHelper::convertToNullableString(false));
182 $this->assertEquals("false", CoreHelper::convertToNullableString("false"));
183 }
184
185 public function testIsNullOrEmpty()
186 {
187 $this->assertTrue(CoreHelper::isNullOrEmpty(0));
188 $this->assertTrue(CoreHelper::isNullOrEmpty([]));
189 $this->assertTrue(CoreHelper::isNullOrEmpty(''));
190 $this->assertTrue(CoreHelper::isNullOrEmpty(null));
191 $this->assertTrue(CoreHelper::isNullOrEmpty(false));
192 $this->assertFalse(CoreHelper::isNullOrEmpty('0'));
193 $this->assertFalse(CoreHelper::isNullOrEmpty('some value'));
194 }
195
196 public function testOsInfo()
197 {
198 $expected = PHP_OS_FAMILY . '-' . php_uname('r');
199 $this->assertEquals($expected, CoreHelper::getOsInfo());
200 }
201
202 public function testEmptyOsInfo()
203 {
204 $this->assertEquals('', CoreHelper::getOsInfo(''));
205 $this->assertEquals('', CoreHelper::getOsInfo('Unknown'));
206 }
207
208 public function testDisabledOsVersion()
209 {
210 $this->assertEquals(PHP_OS_FAMILY, CoreHelper::getOsInfo(PHP_OS_FAMILY, 'unknown_func'));
211 }
212
213 public function testBasicAuthEncodedString()
214 {
215 $expected = 'Basic dXNlcm5hbWU6X1BhNTV3MHJk';
216 $this->assertEquals($expected, CoreHelper::getBasicAuthEncodedString('username', '_Pa55w0rd'));
217 }
218
219 public function testEmptyBasicAuthEncodedString()
220 {
221 $this->assertEmpty(CoreHelper::getBasicAuthEncodedString('', '_Pa55w0rd'));
222 $this->assertEmpty(CoreHelper::getBasicAuthEncodedString('username', ''));
223 $this->assertEmpty(CoreHelper::getBasicAuthEncodedString('', ''));
224 }
225
226 public function testBearerAuthString()
227 {
228 $expected = 'Bearer my-token';
229 $this->assertEquals($expected, CoreHelper::getBearerAuthString('my-token'));
230 }
231
232 public function testEmptyBearerAuthString()
233 {
234 $this->assertEmpty(CoreHelper::getBearerAuthString(''));
235 }
236
237 public function testFromSimpleDateFailure()
238 {
239 $this->expectException(InvalidArgumentException::class);
240 $this->expectExceptionMessage('Incorrect format.');
241 DateHelper::fromSimpleDate('---');
242 }
243
244 public function testFromSimpleDateRequiredFailure()
245 {
246 $this->expectException(InvalidArgumentException::class);
247 $this->expectExceptionMessage('Date is null, empty or not in required format.');
248 DateHelper::fromSimpleDateRequired(null);
249 }
250
251 public function testFromSimpleDateRequired()
252 {
253 $result = DateHelper::fromSimpleDateRequired('2021-10-01');
254 $this->assertEquals('2021-10-01', DateHelper::toSimpleDate($result));
255 }
256
257 public function testFromRFC1123DateFailure()
258 {
259 $this->expectException(InvalidArgumentException::class);
260 $this->expectExceptionMessage('Incorrect format.');
261 DateHelper::fromRfc1123DateTime('---');
262 }
263
264 public function testFromRFC1123DateRequiredFailure()
265 {
266 $this->expectException(InvalidArgumentException::class);
267 $this->expectExceptionMessage('DateTime is null, empty or not in required format.');
268 DateHelper::fromRfc1123DateTimeRequired(null);
269 }
270
271 public function testFromRFC1123DateRequired()
272 {
273 $result = DateHelper::fromRfc1123DateTimeRequired('Thu, 30 Sep 2021 00:00:00 GMT');
274 $this->assertEquals('Thu, 30 Sep 2021 00:00:00 GMT', DateHelper::toRfc1123DateTime($result));
275 }
276
277 public function testFromRFC3339DateFailure()
278 {
279 $this->expectException(InvalidArgumentException::class);
280 $this->expectExceptionMessage('Incorrect format.');
281 DateHelper::fromRfc3339DateTime('---');
282 }
283
284 public function testFromRFC3339DateRequiredFailure()
285 {
286 $this->expectException(InvalidArgumentException::class);
287 $this->expectExceptionMessage('DateTime is null, empty or not in required format.');
288 DateHelper::fromRfc3339DateTimeRequired(null);
289 }
290
291 public function testFromRFC3339DateRequired()
292 {
293 $result = DateHelper::fromRfc3339DateTimeRequired('2021-10-01T00:00:00+00:00');
294 $this->assertEquals('2021-10-01T00:00:00+00:00', DateHelper::toRfc3339DateTime($result));
295 }
296
297 public function testFromUnixDateFailure()
298 {
299 $this->expectException(InvalidArgumentException::class);
300 $this->expectExceptionMessage('Incorrect format.');
301 DateHelper::fromUnixTimestamp('-0-');
302 }
303
304 public function testFromUnixDateRequiredFailure()
305 {
306 $this->expectException(InvalidArgumentException::class);
307 $this->expectExceptionMessage('DateTime is null, empty or not in required format.');
308 DateHelper::fromUnixTimestampRequired(null);
309 }
310
311 public function testFromUnixDateRequired()
312 {
313 $result = DateHelper::fromUnixTimestampRequired('1633046400');
314 $this->assertEquals(1633046400, DateHelper::toUnixTimestamp($result));
315 }
316
317 public function testFromSimpleDateString()
318 {
319 $this->assertNull(DateHelper::fromSimpleDateMapOfArray(null));
320 $this->assertNull(DateHelper::fromSimpleDateArrayOfMap(null));
321 $res = DateHelper::fromSimpleDateMapOfArray((object)[
322 'A' => ['2021-10-01', '2021-09-30'],
323 'B' => [null, '2021-09-29'],
324 'C' => null]);
325 $this->assertEquals([
326 'A' => ['2021-10-01', '2021-09-30'],
327 'B' => [null, '2021-09-29'],
328 'C' => null], DateHelper::toSimpleDate2DArray($res));
329 $res = DateHelper::fromSimpleDateArrayOfMap([
330 (object)['key1' => '2021-10-01', 'key2' => '2021-09-30'],
331 (object)['keyA' => null, 'keyB' => '2021-09-29'],
332 null]);
333 $this->assertEquals([
334 ['key1' => '2021-10-01', 'key2' => '2021-09-30'],
335 ['keyA' => null, 'keyB' => '2021-09-29'],
336 null], DateHelper::toSimpleDate2DArray($res));
337 }
338
339 public function testFromSimpleDateStringTimeInfo()
340 {
341 $date = DateHelper::fromSimpleDate('2024-01-16');
342 $this->assertEquals(strtotime('2024-01-16'), $date->getTimestamp());
343 }
344
345 public function testFromRFC1123DateString()
346 {
347 $this->assertNull(DateHelper::fromRfc1123DateTimeMapOfArray(null));
348 $this->assertNull(DateHelper::fromRfc1123DateTimeArrayOfMap(null));
349 $res = DateHelper::fromRfc1123DateTimeMapOfArray((object)[
350 'A' => ['Fri, 01 Oct 2021 00:00:00 GMT', 'Thu, 30 Sep 2021 00:00:00 GMT'],
351 'B' => [null, 'Wed, 29 Sep 2021 00:00:00 GMT'],
352 'C' => null]);
353 $this->assertEquals([
354 'A' => [new DateTime("2021-09-31"), new DateTime("2021-09-30")],
355 'B' => [null, new DateTime("2021-09-29")],
356 'C' => null], $res);
357 $res = DateHelper::fromRfc1123DateTimeArrayOfMap([
358 (object)['key1' => 'Fri, 01 Oct 2021 00:00:00 GMT', 'key2' => 'Thu, 30 Sep 2021 00:00:00 GMT'],
359 (object)['keyA' => null, 'keyB' => 'Wed, 29 Sep 2021 00:00:00 GMT'],
360 null]);
361 $this->assertEquals([
362 ['key1' => new DateTime("2021-09-31"), 'key2' => new DateTime("2021-09-30")],
363 ['keyA' => null, 'keyB' => new DateTime("2021-09-29")],
364 null], $res);
365 }
366
367 public function testAddingTimezoneInRFC3339DateString()
368 {
369 $date = DateHelper::fromRfc3339DateTime("2021-10-01T00:00:00");
370 $this->assertEquals("2021-10-01T00:00:00+00:00", DateHelper::toRfc3339DateTime($date));
371
372 $date = DateHelper::fromRfc3339DateTime("2021-10-01T00:00:00Z");
373 $this->assertEquals("2021-10-01T00:00:00+00:00", DateHelper::toRfc3339DateTime($date));
374
375 $date = DateHelper::fromRfc3339DateTime("2021-10-01T00:00:00+01:00");
376 $this->assertEquals("2021-09-30T23:00:00+00:00", DateHelper::toRfc3339DateTime($date));
377
378 $date = DateHelper::fromRfc3339DateTime("2021-10-01T00:00:00-01:00");
379 $this->assertEquals("2021-10-01T01:00:00+00:00", DateHelper::toRfc3339DateTime($date));
380 }
381
382 public function testFromRFC3339DateString()
383 {
384 $this->assertNull(DateHelper::fromRfc3339DateTimeMapOfArray(null));
385 $this->assertNull(DateHelper::fromRfc3339DateTimeArrayOfMap(null));
386 $res = DateHelper::fromRfc3339DateTimeMapOfArray((object)[
387 'A' => ['2021-10-01T00:00:00+00:00', '2021-09-30T00:00:00+00:00'],
388 'B' => [null, '2021-09-29T00:00:00+00:00'],
389 'C' => null]);
390 $this->assertEquals([
391 'A' => [new DateTime("2021-09-31"), new DateTime("2021-09-30")],
392 'B' => [null, new DateTime("2021-09-29")],
393 'C' => null], $res);
394 $res = DateHelper::fromRfc3339DateTimeArrayOfMap([
395 (object)['key1' => '2021-10-01T00:00:00+00:00', 'key2' => '2021-09-30T00:00:00+00:00'],
396 (object)['keyA' => null, 'keyB' => '2021-09-29T00:00:00+00:00'],
397 null]);
398 $this->assertEquals([
399 ['key1' => new DateTime("2021-09-31"), 'key2' => new DateTime("2021-09-30")],
400 ['keyA' => null, 'keyB' => new DateTime("2021-09-29")],
401 null], $res);
402 $this->assertEquals(new DateTime("2021-09-31"), DateHelper::fromRfc3339DateTime('2021-10-01T00:00:00'));
403 $this->assertEquals(
404 new DateTime("2021-09-31"),
405 DateHelper::fromRfc3339DateTime('2021-10-01T00:00:00.000000')
406 );
407 $this->assertEquals(
408 new DateTime("2021-09-31"),
409 DateHelper::fromRfc3339DateTime('2021-10-01T00:00:00.000000000000')
410 );
411 }
412
413 public function testFromUnixDateString()
414 {
415 $this->assertNull(DateHelper::fromUnixTimestampMapOfArray(null));
416 $this->assertNull(DateHelper::fromUnixTimestampArrayOfMap(null));
417 $res = DateHelper::fromUnixTimestampMapOfArray((object)[
418 'A' => [1633046400, 1632960000],
419 'B' => [null, 1632873600],
420 'C' => null]);
421 $this->assertEquals([
422 'A' => [new DateTime("2021-09-31"), new DateTime("2021-09-30")],
423 'B' => [null, new DateTime("2021-09-29")],
424 'C' => null], $res);
425 $res = DateHelper::fromUnixTimestampArrayOfMap([
426 (object)['key1' => 1633046400, 'key2' => 1632960000],
427 (object)['keyA' => null, 'keyB' => 1632873600],
428 null]);
429 $this->assertEquals([
430 ['key1' => new DateTime("2021-09-31"), 'key2' => new DateTime("2021-09-30")],
431 ['keyA' => null, 'keyB' => new DateTime("2021-09-29")],
432 null], $res);
433 }
434
435 public function testToDateStringConversions()
436 {
437 $input = [
438 'A' => ['key1' => new DateTime("2021-09-31"), 'key2' => new DateTime("2021-09-30")],
439 'B' => ['keyA' => null, 'keyB' => new DateTime("2021-09-29")],
440 'C' => null
441 ];
442 $this->assertNull(DateHelper::toSimpleDate2DArray(null));
443 $res = DateHelper::toSimpleDate2DArray($input);
444 $this->assertEquals([
445 'A' => ['key1' => '2021-10-01', 'key2' => '2021-09-30'],
446 'B' => ['keyA' => null, 'keyB' => '2021-09-29'],
447 'C' => null], $res);
448 $this->assertNull(DateHelper::toRfc1123DateTime2DArray(null));
449 $res = DateHelper::toRfc1123DateTime2DArray($input);
450 $this->assertEquals([
451 'A' => ['key1' => 'Fri, 01 Oct 2021 00:00:00 GMT', 'key2' => 'Thu, 30 Sep 2021 00:00:00 GMT'],
452 'B' => ['keyA' => null, 'keyB' => 'Wed, 29 Sep 2021 00:00:00 GMT'],
453 'C' => null], $res);
454 $this->assertNull(DateHelper::toRfc3339DateTime2DArray(null));
455 $res = DateHelper::toRfc3339DateTime2DArray($input);
456 $this->assertEquals([
457 'A' => ['key1' => '2021-10-01T00:00:00+00:00', 'key2' => '2021-09-30T00:00:00+00:00'],
458 'B' => ['keyA' => null, 'keyB' => '2021-09-29T00:00:00+00:00'],
459 'C' => null], $res);
460 $this->assertNull(DateHelper::toUnixTimestamp2DArray(null));
461 $res = DateHelper::toUnixTimestamp2DArray($input);
462 $this->assertEquals([
463 'A' => ['key1' => 1633046400, 'key2' => 1632960000],
464 'B' => ['keyA' => null, 'keyB' => 1632873600],
465 'C' => null], $res);
466 }
467 }
468