3 * @author JoakimLofgren
6 use PhpXmlRpc\Helper\Charset;
9 * Test conversion between encodings
11 * For Windows if you want to test the output use Consolas font
12 * and run the following in cmd:
16 class CharsetTest extends PHPUnit_Framework_TestCase
18 // Consolas font should render these properly
19 protected $runes = "ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ";
20 protected $greek = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ";
21 protected $russian = "Река неслася; бедный чёлн";
22 protected $chinese = "我能吞下玻璃而不伤身体。";
23 protected $latinString;
25 protected function setUp()
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);
32 for($i = 160; $i < 256; $i++) {
33 $this->latinString .= chr($i);
37 protected function utfToLatin($data)
39 return Charset::instance()->encodeEntities(
46 public function testUtf8ToLatin1All()
48 /*$this->assertEquals(
50 mb_detect_encoding($this->latinString, 'ISO-8859-1, UTF-8, WINDOWS-1251, ASCII', true),
51 'Setup latinString is not ISO-8859-1 encoded...'
53 $string = utf8_encode($this->latinString);
54 $encoded = $this->utfToLatin($string);
55 $this->assertEquals(str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $this->latinString), $encoded);
58 public function testUtf8ToLatin1EuroSymbol()
60 $string = 'a.b.c.å.ä.ö.€.';
61 $encoded = $this->utfToLatin($string);
62 $this->assertEquals(utf8_decode('a.b.c.å.ä.ö.€.'), $encoded);
65 public function testUtf8ToLatin1Runes()
67 $string = $this->runes;
68 $encoded = $this->utfToLatin($string);
69 $this->assertEquals('ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ', $encoded);
72 public function testUtf8ToLatin1Greek()
74 $string = $this->greek;
75 $encoded = $this->utfToLatin($string);
76 $this->assertEquals('Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ', $encoded);
79 public function testUtf8ToLatin1Russian()
81 $string = $this->russian;
82 $encoded = $this->utfToLatin($string);
83 $this->assertEquals('Река неслася; бедный чёлн', $encoded);
86 public function testUtf8ToLatin1Chinese()
88 $string = $this->chinese;
89 $encoded = $this->utfToLatin($string);
90 $this->assertEquals('我能吞下玻璃而不伤身体。', $encoded);