Completed
Push — master ( 640b8c...d8e180 )
by Gaetano
07:50
created

CharsetTest::utfToLatin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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(
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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