Passed
Push — master ( 02b7f4...c1cbf5 )
by Gaetano
07:18
created

CharsetTest::set_up()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
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 via usage of the Charset class.
12
 * Note that quite a few other tests testing different classes also test character set conversion.
13
 *
14
 * For Windows if you want to test the output use Consolas font 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. Of course this file is encoded in UTF-8
25
    protected $runes = "ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ";
26
    protected $greek = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ";
27
    protected $russian = "Река неслася; бедный чёлн";
28
    protected $chinese = "我能吞下玻璃而不伤身体。";
29
30
    protected function utf8ToLatin1($data)
31
    {
32
        return Charset::instance()->encodeEntities(
33
            $data,
34
            'UTF-8',
35
            'ISO-8859-1'
36
        );
37
    }
38
39
    protected function utf8ToAscii($data)
40
    {
41
        return Charset::instance()->encodeEntities(
42
            $data,
43
            'UTF-8',
44
            'US-ASCII'
45
        );
46
    }
47
48
    public function testUtf8ToLatin1All()
49
    {
50
        $latinString = "\n\r\t";
51
        for($i = 32; $i < 127; $i++) {
52
            $latinString .= chr($i);
53
        }
54
        for($i = 160; $i < 256; $i++) {
55
            $latinString .= chr($i);
56
        }
57
58
        // the warning suppression is due to utf8_encode being deprecated in php 8.2
59
        $string = @utf8_encode($latinString);
60
        $encoded = $this->utf8ToLatin1($string);
61
        $this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $latinString), $encoded);
62
    }
63
64
    public function testUtf8ToLatin1EuroSymbol()
65
    {
66
        $string = 'a.b.c.å.ä.ö.€.';
67
        $encoded = $this->utf8ToLatin1($string);
68
        // the warning suppression is due to utf8_decode being deprecated in php 8.2
69
        $this->assertEquals(@utf8_decode('a.b.c.å.ä.ö.&#8364;.'), $encoded);
70
    }
71
72
    public function testUtf8ToLatin1Runes()
73
    {
74
        $string = $this->runes;
75
        $encoded = $this->utf8ToLatin1($string);
76
        $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);
77
    }
78
79
    public function testUtf8ToLatin1Greek()
80
    {
81
        $string = $this->greek;
82
        $encoded = $this->utf8ToLatin1($string);
83
        $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);
84
    }
85
86
    public function testUtf8ToLatin1Russian()
87
    {
88
        $string = $this->russian;
89
        $encoded = $this->utf8ToLatin1($string);
90
        $this->assertEquals('&#1056;&#1077;&#1082;&#1072; &#1085;&#1077;&#1089;&#1083;&#1072;&#1089;&#1103;; &#1073;&#1077;&#1076;&#1085;&#1099;&#1081; &#1095;&#1105;&#1083;&#1085;', $encoded);
91
    }
92
93
    public function testUtf8ToLatin1Chinese()
94
    {
95
        $string = $this->chinese;
96
        $encoded = $this->utf8ToLatin1($string);
97
        $this->assertEquals('&#25105;&#33021;&#21534;&#19979;&#29627;&#29827;&#32780;&#19981;&#20260;&#36523;&#20307;&#12290;', $encoded);
98
    }
99
100
    public function testLatin15()
101
    {
102
        if (!function_exists('mb_convert_encoding')) {
103
            $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
104
            return;
105
        }
106
107
        // euro symbol in ISO-8859-15
108
        $string = chr(164);
109
        $encoder = Charset::instance();
110
        $this->assertEquals('€', $encoder->encodeEntities($string, 'ISO-8859-15', 'UTF-8'));
111
        $this->assertEquals('&#8364;', $encoder->encodeEntities($string, 'ISO-8859-15', 'US-ASCII'));
112
        $this->assertEquals(chr(164), $encoder->encodeEntities($string, 'ISO-8859-15', 'ISO-8859-15'));
113
    }
114
}
115