1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpMyAdmin\SqlParser\Tests\Misc; |
4
|
|
|
|
5
|
|
|
use PhpMyAdmin\SqlParser\Tests\TestCase; |
6
|
|
|
use PhpMyAdmin\SqlParser\UtfString; |
7
|
|
|
|
8
|
|
|
class UtfStringTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Sample phrase in French. |
12
|
|
|
* |
13
|
|
|
* @var UtfString |
14
|
|
|
*/ |
15
|
|
|
const TEST_PHRASE = 'Les naïfs ægithales hâtifs pondant à Noël où il gèle sont sûrs d\'être déçus en voyant leurs drôles d\'œufs abîmés.'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The length of the sample phrase. |
19
|
|
|
* |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
const TEST_PHRASE_LEN = 113; |
23
|
|
|
|
24
|
|
|
public function testArrayAccess() |
25
|
|
|
{ |
26
|
|
|
$str = new UtfString(static::TEST_PHRASE); |
27
|
|
|
|
28
|
|
|
// offsetExists |
29
|
|
|
$this->assertArrayHasKey(static::TEST_PHRASE_LEN - 1, $str); |
30
|
|
|
$this->assertArrayNotHasKey(-1, $str); |
31
|
|
|
$this->assertArrayNotHasKey(static::TEST_PHRASE_LEN, $str); |
32
|
|
|
|
33
|
|
|
// offsetGet |
34
|
|
|
$this->assertEquals('.', $str[static::TEST_PHRASE_LEN - 1]); |
35
|
|
|
$this->assertNull($str[-1]); |
36
|
|
|
$this->assertNull($str[static::TEST_PHRASE_LEN]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @expectedException \Exception |
41
|
|
|
* @expectedExceptionMessage Not implemented. |
42
|
|
|
*/ |
43
|
|
|
public function testSet() |
44
|
|
|
{ |
45
|
|
|
$str = new UtfString(''); |
46
|
|
|
$str[0] = 'a'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @expectedException \Exception |
51
|
|
|
* @expectedExceptionMessage Not implemented. |
52
|
|
|
*/ |
53
|
|
|
public function testUnset() |
54
|
|
|
{ |
55
|
|
|
$str = new UtfString(''); |
56
|
|
|
unset($str[0]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetCharLength() |
60
|
|
|
{ |
61
|
|
|
$this->assertEquals(1, UtfString::getCharLength(chr(0x00))); // 00000000 |
62
|
|
|
$this->assertEquals(1, UtfString::getCharLength(chr(0x7F))); // 01111111 |
63
|
|
|
|
64
|
|
|
$this->assertEquals(2, UtfString::getCharLength(chr(0xC0))); // 11000000 |
65
|
|
|
$this->assertEquals(2, UtfString::getCharLength(chr(0xDF))); // 11011111 |
66
|
|
|
|
67
|
|
|
$this->assertEquals(3, UtfString::getCharLength(chr(0xE0))); // 11100000 |
68
|
|
|
$this->assertEquals(3, UtfString::getCharLength(chr(0xEF))); // 11101111 |
69
|
|
|
|
70
|
|
|
$this->assertEquals(4, UtfString::getCharLength(chr(0xF0))); // 11110000 |
71
|
|
|
$this->assertEquals(4, UtfString::getCharLength(chr(0xF7))); // 11110111 |
72
|
|
|
|
73
|
|
|
$this->assertEquals(5, UtfString::getCharLength(chr(0xF8))); // 11111000 |
74
|
|
|
$this->assertEquals(5, UtfString::getCharLength(chr(0xFB))); // 11111011 |
75
|
|
|
|
76
|
|
|
$this->assertEquals(6, UtfString::getCharLength(chr(0xFC))); // 11111100 |
77
|
|
|
$this->assertEquals(6, UtfString::getCharLength(chr(0xFD))); // 11111101 |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testToString() |
81
|
|
|
{ |
82
|
|
|
$str = new UtfString(static::TEST_PHRASE); |
83
|
|
|
$this->assertEquals(static::TEST_PHRASE, (string) $str); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Test access to string. |
88
|
|
|
* |
89
|
|
|
* @dataProvider utf8Strings |
90
|
|
|
* |
91
|
|
|
* @param mixed $text |
92
|
|
|
* @param mixed $pos10 |
93
|
|
|
* @param mixed $pos20 |
94
|
|
|
*/ |
95
|
|
|
public function testAccess($text, $pos10, $pos20) |
96
|
|
|
{ |
97
|
|
|
$str = new UtfString($text); |
98
|
|
|
$this->assertEquals($pos10, $str->offsetGet(10)); |
99
|
|
|
$this->assertEquals($pos20, $str->offsetGet(20)); |
100
|
|
|
$this->assertEquals($pos10, $str->offsetGet(10)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function utf8Strings() |
104
|
|
|
{ |
105
|
|
|
return array( |
106
|
|
|
'ascii' => array( |
107
|
|
|
'abcdefghijklmnopqrstuvwxyz', |
108
|
|
|
'k', |
109
|
|
|
'u', |
110
|
|
|
), |
111
|
|
|
'unicode' => array( |
112
|
|
|
'áéíóúýěřťǔǐǒǎšďȟǰǩľžčǚň', |
113
|
|
|
'ǐ', |
114
|
|
|
'č', |
115
|
|
|
), |
116
|
|
|
'emoji' => array( |
117
|
|
|
'😂😄😃😀😊😉😍😘😚😗😂👿😮😨😱😠😡😤😖😆😋👯', |
118
|
|
|
'😂', |
119
|
|
|
'😋', |
120
|
|
|
), |
121
|
|
|
'iso' => array( |
122
|
|
|
"P\xf8\xed\xb9ern\xec \xbelu\xbbou\xe8k\xfd k\xf3d \xfap\xecl \xef\xe1belsk\xe9 k\xf3dy", |
123
|
|
|
null, |
124
|
|
|
null, |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|