tests on php8
[plcapi.git] / tests / 0CharsetTest.php
1 <?php
2 /**
3  * @author JoakimLofgren
4  */
5
6 use PhpXmlRpc\Helper\Charset;
7
8 include_once __DIR__ . '/PolyfillTestCase.php';
9
10 /**
11  * Test conversion between encodings
12  *
13  * For Windows if you want to test the output use Consolas font
14  * and run the following in cmd:
15  *     chcp 28591 (latin1)
16  *     chcp 65001 (utf8)
17  */
18 class CharsetTest extends PhpXmlRpc_PolyfillTestCase
19 {
20     // Consolas font should render these properly
21     protected $runes = "ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ";
22     protected $greek = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ";
23     protected $russian = "Река неслася; бедный чёлн";
24     protected $chinese = "我能吞下玻璃而不伤身体。";
25     protected $latinString;
26
27     protected function set_up()
28     {
29         // construct a latin string with all chars (except control ones)
30         $this->latinString = "\n\r\t";
31         for($i = 32; $i < 127; $i++) {
32             $this->latinString .= chr($i);
33         }
34         for($i = 160; $i < 256; $i++) {
35             $this->latinString .= chr($i);
36         }
37     }
38
39     protected function utfToLatin($data)
40     {
41         return Charset::instance()->encodeEntities(
42             $data,
43             'UTF-8',
44             'ISO-8859-1'
45         );
46     }
47
48     public function testUtf8ToLatin1All()
49     {
50         /*$this->assertEquals(
51             'ISO-8859-1',
52             mb_detect_encoding($this->latinString, 'ISO-8859-1, UTF-8, WINDOWS-1251, ASCII', true),
53             'Setup latinString is not ISO-8859-1 encoded...'
54         );*/
55         $string = utf8_encode($this->latinString);
56         $encoded = $this->utfToLatin($string);
57         $this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $this->latinString), $encoded);
58     }
59
60     public function testUtf8ToLatin1EuroSymbol()
61     {
62         $string = 'a.b.c.å.ä.ö.€.';
63         $encoded = $this->utfToLatin($string);
64         $this->assertEquals(utf8_decode('a.b.c.å.ä.ö.&#8364;.'), $encoded);
65     }
66
67     public function testUtf8ToLatin1Runes()
68     {
69         $string = $this->runes;
70         $encoded = $this->utfToLatin($string);
71         $this->assertEquals('&#5792;&#5831;&#5819;&#5867;&#5842;&#5862;&#5798;&#5867;&#5792;&#5809;&#5801;&#5792;&#5794;&#5809;&#5867;&#5792;&#5825;&#5809;&#5802;&#5867;&#5815;&#5846;&#5819;&#5817;&#5862;&#5850;&#5811;&#5794;&#5847;', $encoded);
72     }
73
74     public function testUtf8ToLatin1Greek()
75     {
76         $string = $this->greek;
77         $encoded = $this->utfToLatin($string);
78         $this->assertEquals('&#932;&#8052; &#947;&#955;&#8182;&#963;&#963;&#945; &#956;&#959;&#8166; &#7956;&#948;&#969;&#963;&#945;&#957; &#7953;&#955;&#955;&#951;&#957;&#953;&#954;&#8052;', $encoded);
79     }
80
81     public function testUtf8ToLatin1Russian()
82     {
83         $string = $this->russian;
84         $encoded = $this->utfToLatin($string);
85         $this->assertEquals('&#1056;&#1077;&#1082;&#1072; &#1085;&#1077;&#1089;&#1083;&#1072;&#1089;&#1103;; &#1073;&#1077;&#1076;&#1085;&#1099;&#1081; &#1095;&#1105;&#1083;&#1085;', $encoded);
86     }
87
88     public function testUtf8ToLatin1Chinese()
89     {
90         $string = $this->chinese;
91         $encoded = $this->utfToLatin($string);
92         $this->assertEquals('&#25105;&#33021;&#21534;&#19979;&#29627;&#29827;&#32780;&#19981;&#20260;&#36523;&#20307;&#12290;', $encoded);
93     }
94 }