ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
EncodingTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 
25 class EncodingTest extends TestCase
26 {
27  private ?Group $group = null;
28 
29  public function setUp(): void
30  {
31  $this->group = new Group();
32  }
33 
34  public function latin1StringProvider(): array
35  {
36  // generate 500 random strings with ISO-8859-1 encoding. unfortunately, I was not able to find a list to copy
37  // here which keeps it's encoding, therefore we must generate them randomly
38  $strings = [];
39  for ($i = 0; $i < 500; $i++) {
40  $length = random_int(50, 500);
41  $string = '';
42  for ($j = 0; $j < $length; $j++) {
43  $string .= chr(random_int(0, 255));
44  }
45  $strings[] = [$string, @utf8_encode($string)]; // we must suppress the deprecation warning here
46  }
47  return $strings;
48  }
49 
53  public function testLatin1ToUTF8(
54  string $latin_1_string,
55  string $expected_utf8
56  ): void {
57  $this->assertTrue(mb_check_encoding($latin_1_string, 'ISO-8859-1'));
58  $result = $this->group->latin1ToUtf8()->transform($latin_1_string);
59  $this->assertTrue(mb_check_encoding($result, 'UTF-8'));
60  $this->assertEquals($expected_utf8, $result);
61  }
62 
63  public function asciiStringProvider(): array
64  {
65  // generate 500 random strings with US-ASCII encosing.
66  $strings = [];
67  for ($i = 0; $i < 500; $i++) {
68  $length = random_int(50, 500);
69  $string = '';
70  for ($j = 0; $j < $length; $j++) {
71  $string .= chr(random_int(0, 127));
72  }
73  $strings[] = [$string, $string];
74  }
75  return $strings;
76  }
77 
81  public function testAsciiToUTF8(
82  string $latin_1_string,
83  string $expected_utf8
84  ): void {
85  $this->assertTrue(mb_check_encoding($latin_1_string, 'US-ASCII'));
86  $result = $this->group->asciiToUtf8()->transform($latin_1_string);
87  $this->assertTrue(mb_check_encoding($result, 'UTF-8'));
88  $this->assertEquals($expected_utf8, $result);
89  }
90 
91 }
testAsciiToUTF8(string $latin_1_string, string $expected_utf8)
asciiStringProvider
testLatin1ToUTF8(string $latin_1_string, string $expected_utf8)
latin1StringProvider