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('&', '"', ''', '<', '>'), $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.å.ä.ö.€.'), $encoded); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testUtf8ToLatin1Runes() |
66
|
|
|
{ |
67
|
|
|
$string = $this->runes; |
68
|
|
|
$encoded = $this->utfToLatin($string); |
69
|
|
|
$this->assertEquals('ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', $encoded); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testUtf8ToLatin1Greek() |
73
|
|
|
{ |
74
|
|
|
$string = $this->greek; |
75
|
|
|
$encoded = $this->utfToLatin($string); |
76
|
|
|
$this->assertEquals('Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ', $encoded); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testUtf8ToLatin1Russian() |
80
|
|
|
{ |
81
|
|
|
$string = $this->russian; |
82
|
|
|
$encoded = $this->utfToLatin($string); |
83
|
|
|
$this->assertEquals('Река неслася; бедный чёлн', $encoded); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testUtf8ToLatin1Chinese() |
87
|
|
|
{ |
88
|
|
|
$string = $this->chinese; |
89
|
|
|
$encoded = $this->utfToLatin($string); |
90
|
|
|
$this->assertEquals('我能吞下玻璃而不伤身体。', $encoded); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|