minor optimizations to charset encoder
[plcapi.git] / tests / 0CharsetTest.php
1 <?php
2 /**
3  * @author JoakimLofgren
4  */
5
6 include_once __DIR__ . '/PolyfillTestCase.php';
7
8 use PhpXmlRpc\Helper\Charset;
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  * @todo add tests for conversion: utf8 -> ascii (incl. chars 0-31 and 127)
19  * @todo add tests for conversion: latin1 -> utf8
20  * @todo add tests for conversion: latin1 -> ascii (incl. chars 0-31 and 127)
21  */
22 class CharsetTest extends PhpXmlRpc_PolyfillTestCase
23 {
24     // Consolas font should render these properly
25     protected $runes = "ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ";
26     protected $greek = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ";
27     protected $russian = "Река неслася; бедный чёлн";
28     protected $chinese = "我能吞下玻璃而不伤身体。";
29
30     protected $latinString;
31
32     /// @todo move to usage of a dataProvider and create the latinString there
33     protected function set_up()
34     {
35         // construct a latin string with all chars (except control ones)
36         $this->latinString = "\n\r\t";
37         for($i = 32; $i < 127; $i++) {
38             $this->latinString .= chr($i);
39         }
40         for($i = 160; $i < 256; $i++) {
41             $this->latinString .= chr($i);
42         }
43     }
44
45     protected function utf8ToLatin1($data)
46     {
47         return Charset::instance()->encodeEntities(
48             $data,
49             'UTF-8',
50             'ISO-8859-1'
51         );
52     }
53
54     protected function utf8ToAscii($data)
55     {
56         return Charset::instance()->encodeEntities(
57             $data,
58             'UTF-8',
59             'US-ASCII'
60         );
61     }
62
63     public function testUtf8ToLatin1All()
64     {
65         /*$this->assertEquals(
66             'ISO-8859-1',
67             mb_detect_encoding($this->latinString, 'ISO-8859-1, UTF-8, WINDOWS-1251, ASCII', true),
68             'Setup latinString is not ISO-8859-1 encoded...'
69         );*/
70         $string = utf8_encode($this->latinString);
71         $encoded = $this->utf8ToLatin1($string);
72         $this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $this->latinString), $encoded);
73     }
74
75     public function testUtf8ToLatin1EuroSymbol()
76     {
77         $string = 'a.b.c.å.ä.ö.€.';
78         $encoded = $this->utf8ToLatin1($string);
79         $this->assertEquals(utf8_decode('a.b.c.å.ä.ö.&#8364;.'), $encoded);
80     }
81
82     public function testUtf8ToLatin1Runes()
83     {
84         $string = $this->runes;
85         $encoded = $this->utf8ToLatin1($string);
86         $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);
87     }
88
89     public function testUtf8ToLatin1Greek()
90     {
91         $string = $this->greek;
92         $encoded = $this->utf8ToLatin1($string);
93         $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);
94     }
95
96     public function testUtf8ToLatin1Russian()
97     {
98         $string = $this->russian;
99         $encoded = $this->utf8ToLatin1($string);
100         $this->assertEquals('&#1056;&#1077;&#1082;&#1072; &#1085;&#1077;&#1089;&#1083;&#1072;&#1089;&#1103;; &#1073;&#1077;&#1076;&#1085;&#1099;&#1081; &#1095;&#1105;&#1083;&#1085;', $encoded);
101     }
102
103     public function testUtf8ToLatin1Chinese()
104     {
105         $string = $this->chinese;
106         $encoded = $this->utf8ToLatin1($string);
107         $this->assertEquals('&#25105;&#33021;&#21534;&#19979;&#29627;&#29827;&#32780;&#19981;&#20260;&#36523;&#20307;&#12290;', $encoded);
108     }
109 }