3 * @author JoakimLofgren
6 include_once __DIR__ . '/PolyfillTestCase.php';
8 use PhpXmlRpc\Helper\Charset;
11 * Test conversion between encodings
13 * For Windows if you want to test the output use Consolas font
14 * and run the following in cmd:
18 * @todo add tests for conversion: utf8 -> ascii
19 * @todo add tests for conversion: latin1 -> utf8
21 class CharsetTest extends PhpXmlRpc_PolyfillTestCase
23 // Consolas font should render these properly
24 protected $runes = "ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ";
25 protected $greek = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ";
26 protected $russian = "Река неслася; бедный чёлн";
27 protected $chinese = "我能吞下玻璃而不伤身体。";
28 protected $latinString;
30 /// @todo move to usage of a dataProvider and create the latinString there
31 protected function set_up()
33 // construct a latin string with all chars (except control ones)
34 $this->latinString = "\n\r\t";
35 for($i = 32; $i < 127; $i++) {
36 $this->latinString .= chr($i);
38 for($i = 160; $i < 256; $i++) {
39 $this->latinString .= chr($i);
43 protected function utf8ToLatin1($data)
45 return Charset::instance()->encodeEntities(
52 public function testUtf8ToLatin1All()
54 /*$this->assertEquals(
56 mb_detect_encoding($this->latinString, 'ISO-8859-1, UTF-8, WINDOWS-1251, ASCII', true),
57 'Setup latinString is not ISO-8859-1 encoded...'
59 $string = utf8_encode($this->latinString);
60 $encoded = $this->utf8ToLatin1($string);
61 $this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $this->latinString), $encoded);
64 public function testUtf8ToLatin1EuroSymbol()
66 $string = 'a.b.c.å.ä.ö.€.';
67 $encoded = $this->utf8ToLatin1($string);
68 $this->assertEquals(utf8_decode('a.b.c.å.ä.ö.€.'), $encoded);
71 public function testUtf8ToLatin1Runes()
73 $string = $this->runes;
74 $encoded = $this->utf8ToLatin1($string);
75 $this->assertEquals('ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', $encoded);
78 public function testUtf8ToLatin1Greek()
80 $string = $this->greek;
81 $encoded = $this->utf8ToLatin1($string);
82 $this->assertEquals('Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ', $encoded);
85 public function testUtf8ToLatin1Russian()
87 $string = $this->russian;
88 $encoded = $this->utf8ToLatin1($string);
89 $this->assertEquals('Река неслася; бедный чёлн', $encoded);
92 public function testUtf8ToLatin1Chinese()
94 $string = $this->chinese;
95 $encoded = $this->utf8ToLatin1($string);
96 $this->assertEquals('我能吞下玻璃而不伤身体。', $encoded);