-
Notifications
You must be signed in to change notification settings - Fork 821
/
MySQLiConnectorTest.php
154 lines (124 loc) · 5.42 KB
/
MySQLiConnectorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace SilverStripe\ORM\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\Tests\MySQLiConnectorTest\MySQLiConnector;
use SilverStripe\ORM\DB;
use SilverStripe\Tests\ORM\Utf8\Utf8TestHelper;
/**
* @requires extension mysqli
*/
class MySQLiConnectorTest extends SapphireTest implements TestOnly
{
/**
* @dataProvider charsetProvider
*/
public function testConnectionCharsetControl($charset, $defaultCollation)
{
$config = DB::getConfig();
$config['charset'] = $charset;
$config['database'] = 'information_schema';
if (strtolower(substr($config['type'], 0, 5)) !== 'mysql') {
return $this->markTestSkipped('The test only relevant for MySQL');
}
$connector = new MySQLiConnector();
$connector->connect($config);
$connection = $connector->getMysqliConnection();
$cset = $connection->get_charset();
// Note: we do not need to update the utf charset here because mysqli with newer
// version of mysql/mariadb still self-reports as 'utf8' rather than 'utf8mb3'
// This is unlike self::testConnectionCollationControl()
// And also unlike MySQLPDOConnectorTest::testConnectionCharsetControl()
$this->assertEquals($charset, $cset->charset);
$this->assertEquals($defaultCollation, $cset->collation);
unset($cset, $connection, $connector, $config);
}
/**
* @dataProvider charsetProvider
*/
public function testConnectionCollationControl($charset, $defaultCollation, $customCollation)
{
$config = DB::getConfig();
$config['charset'] = $charset;
$config['collation'] = $customCollation;
$config['database'] = 'information_schema';
if (strtolower(substr($config['type'], 0, 5)) !== 'mysql') {
return $this->markTestSkipped('The test only relevant for MySQL');
}
$connector = new MySQLiConnector();
$connector->connect($config);
$connection = $connector->getMysqliConnection();
$cset = $connection->get_charset();
$this->assertEquals($charset, $cset->charset);
/* Warning! This is a MySQLi limitation.
* If it changes in the future versions, this test may break.
* We are still testing for it as a limitation and a
* reminder that it exists.
*
* To make sure that we actually have correct collation see
* - testUtf8mb4GeneralCollation
* - testUtf8mb4UnicodeCollation
*/
$this->assertEquals(
$defaultCollation,
$cset->collation,
'This is an issue with mysqli. It always returns "default" collation, even if another is active'
);
$cset = $connection->query('show variables like "character_set_connection"')->fetch_array()[1];
$collation = $connection->query('show variables like "collation_connection"')->fetch_array()[1];
$helper = new Utf8TestHelper();
$this->assertEquals($helper->getUpdatedUtfCharsetForCurrentDB($charset), $cset);
$this->assertEquals($helper->getUpdatedUtfCollationForCurrentDB($customCollation), $collation);
$connection->close();
unset($cset, $connection, $connector, $config);
}
public function charsetProvider()
{
return [
['ascii', 'ascii_general_ci', 'ascii_bin'],
['utf8', 'utf8_general_ci', 'utf8_unicode_520_ci'],
['utf8mb4', 'utf8mb4_general_ci', 'utf8mb4_unicode_520_ci']
];
}
public function testUtf8mb4GeneralCollation()
{
$charset = 'utf8mb4';
$collation = 'utf8mb4_general_ci';
$config = DB::getConfig();
$config['charset'] = $charset;
$config['collation'] = $collation;
$config['database'] = 'information_schema';
if (strtolower(substr($config['type'], 0, 5)) !== 'mysql') {
return $this->markTestSkipped('The test only relevant for MySQL');
}
$connector = new MySQLiConnector();
$connector->connect($config, true);
$connection = $connector->getMysqliConnection();
$result = $connection->query(
"select `a`.`value` from (select 'rst' `value` union select 'rßt' `value`) `a` order by `value`"
)->fetch_all();
$this->assertCount(1, $result, '`utf8mb4_general_ci` handles both values as equal to "rst"');
$this->assertEquals('rst', $result[0][0]);
}
public function testUtf8mb4UnicodeCollation()
{
$charset = 'utf8mb4';
$collation = 'utf8mb4_unicode_ci';
$config = DB::getConfig();
$config['charset'] = $charset;
$config['collation'] = $collation;
$config['database'] = 'information_schema';
if (strtolower(substr($config['type'], 0, 5)) !== 'mysql') {
return $this->markTestSkipped('The test only relevant for MySQL');
}
$connector = new MySQLiConnector();
$connector->connect($config, true);
$connection = $connector->getMysqliConnection();
$result = $connection->query(
"select `a`.`value` from (select 'rst' `value` union select 'rßt' `value`) `a` order by `value`"
)->fetch_all();
$this->assertCount(2, $result, '`utf8mb4_unicode_ci` must recognise "rst" and "rßt" as different values');
$this->assertEquals('rßt', $result[0][0]);
$this->assertEquals('rst', $result[1][0]);
}
}