Completed
Push — master ( 87b7a4...6ce28d )
by Gaetano
11:11 queued 06:38
created

CharsetTest::testUtf8ToLatin1Russian()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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