@@ -76,132 +76,59 @@ function my_mysqli_connect(
76
76
int $ port ,
77
77
?string $ socket = null ,
78
78
bool $ enable_env_flags = true
79
- ): \mysqli |false {
80
-
81
- $ flags = $ enable_env_flags ? get_environment_connection_flags ():0 ;
79
+ ): \mysqli {
80
+ // Because the tests are meant to test both error modes, they can set the report_mode to a different value,
81
+ // which we do not want to override. However, we want to make sure that if a connection cannot be made,
82
+ // the constuctor will throw an exception. We store current report_mode in variable and restore it later.
83
+ $ driver = new mysqli_driver ;
84
+ $ report_mode = $ driver ->report_mode ;
85
+ $ driver ->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT ;
86
+ $ flags = $ enable_env_flags ? get_environment_connection_flags () : 0 ;
82
87
if ($ flags !== 0 ) {
83
88
$ link = mysqli_init ();
84
- if (!mysqli_real_connect ($ link , $ host , $ user , $ password , $ db , $ port , $ socket , $ flags )) {
85
- $ link = false ;
86
- }
89
+ mysqli_real_connect ($ link , $ host , $ user , $ password , $ db , $ port , $ socket , $ flags );
87
90
} else {
88
91
$ link = mysqli_connect ($ host , $ user , $ password , $ db , $ port , $ socket );
89
92
}
90
-
93
+ // Restore error mode
94
+ $ driver ->report_mode = $ report_mode ;
91
95
return $ link ;
92
96
}
93
-
94
- /**
95
- * Whenever possible, please use this wrapper to make testing of MYSQLI_CLIENT_COMPRESS (and potentially SSL) possible
96
- *
97
- * @param bool $enable_env_flags Enable setting of connection flags through env(MYSQL_TEST_CONNECT_FLAGS)
98
- */
99
- function my_mysqli_real_connect (
100
- mysqli $ link ,
101
- string $ host ,
102
- string $ user ,
103
- string $ password ,
104
- string $ db ,
105
- int $ port ,
106
- ?string $ socket = null ,
107
- int $ flags = 0 ,
108
- bool $ enable_env_flags = true
109
- ): \mysqli |false {
110
- if ($ enable_env_flags ) {
111
- $ flags = $ flags | get_environment_connection_flags ();
112
- }
113
-
114
- return mysqli_real_connect ($ link , $ host , $ user , $ password , $ db , $ port , $ socket , $ flags );
115
- }
116
-
117
- function default_mysqli_connect_ex (): \mysqli |false {
118
- return my_mysqli_connect (
119
- get_default_host (),
120
- get_default_user (),
121
- get_default_password (),
122
- get_default_database (),
123
- get_default_port (),
124
- null ,
125
- );
126
- }
127
97
function default_mysqli_connect (): \mysqli {
128
- $ link = default_mysqli_connect_ex ();
129
- if (!$ link ) {
130
- throw new Error ("Cannot connect to default connection " );
131
- }
132
- return $ link ;
133
- }
134
- function default_mysqli_real_connect (mysqli $ link , int $ flags = 0 ): bool {
135
- return my_mysqli_real_connect (
136
- $ link ,
98
+ return my_mysqli_connect (
137
99
get_default_host (),
138
100
get_default_user (),
139
101
get_default_password (),
140
102
get_default_database (),
141
103
get_default_port (),
142
104
null ,
143
- $ flags ,
144
105
);
145
106
}
146
-
147
107
function mysqli_check_skip_test (): void {
148
- /* Disable exceptions */
149
- mysqli_report (MYSQLI_REPORT_OFF );
150
- $ link = default_mysqli_connect_ex ();
151
- // Re-enable exceptions
152
- mysqli_report (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
153
- if (!is_object ($ link )) {
108
+ try {
109
+ $ link = default_mysqli_connect ();
110
+ } catch (\mysqli_sql_exception ) {
154
111
die (sprintf ("skip Can't connect to MySQL Server - [%d] %s " , mysqli_connect_errno (), mysqli_connect_error ()));
155
112
}
156
- mysqli_close ($ link );
157
113
}
158
-
159
114
function have_innodb (mysqli $ link ): bool {
160
- if (($ res = $ link ->query ("SHOW VARIABLES LIKE 'have_innodb' " ))
161
- && ($ row = $ res ->fetch_row ())
162
- && !empty ($ row )
163
- ) {
164
- return !($ row [1 ] == 'DISABLED ' || $ row [1 ] == 'NO ' );
165
- }
166
- // MySQL 5.6.1+
167
- if ($ res = $ link ->query ('SHOW ENGINES ' )) {
168
- while ($ row = $ res ->fetch_assoc ()) {
169
- if (!isset ($ row ['Engine ' ]) || !isset ($ row ['Support ' ])) {
170
- return false ;
171
- }
172
-
173
- if (($ row ['Engine ' ] == 'InnoDB ' )
174
- && (($ row ['Support ' ] == 'YES ' ) || ($ row ['Support ' ] == 'DEFAULT ' ))
175
- ) {
176
- return true ;
177
- }
178
- }
179
- }
180
- return false ;
115
+ $ res = $ link ->query ("SELECT SUPPORT FROM INFORMATION_SCHEMA.ENGINES WHERE ENGINE = 'InnoDB' " );
116
+ $ supported = $ res ->fetch_column ();
117
+ return $ supported === 'YES ' || $ supported === 'DEFAULT ' ;
181
118
}
182
119
function mysqli_check_innodb_support_skip_test (): void {
183
- /* Disable exceptions */
184
- mysqli_report (MYSQLI_REPORT_OFF );
185
- $ link = default_mysqli_connect_ex ();
186
- if (!is_object ($ link )) {
187
- // Re-enable exceptions
188
- mysqli_report (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
120
+ try {
121
+ $ link = default_mysqli_connect ();
122
+ } catch (\mysqli_sql_exception ) {
189
123
die (sprintf ("skip Can't connect to MySQL Server - [%d] %s " , mysqli_connect_errno (), mysqli_connect_error ()));
190
124
}
191
- $ status = have_innodb ($ link );
192
- // Re-enable exceptions
193
- mysqli_report (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
194
- mysqli_close ($ link );
195
- if (!$ status ) {
196
- die (sprintf ("skip Needs InnoDB support, [%d] %s " , $ link ->errno , $ link ->error ));
125
+ if (! have_innodb ($ link )) {
126
+ die (sprintf ("skip Needs InnoDB support " ));
197
127
}
198
128
}
199
-
200
129
function tear_down_table_on_default_connection (string $ table ) {
201
130
$ link = default_mysqli_connect ();
202
- if (!mysqli_query ($ link , 'DROP TABLE IF EXISTS ' . $ table )) {
203
- printf ("[clean] Failed to drop \"$ table \" table: [%d] %s \n" , mysqli_errno ($ link ), mysqli_error ($ link ));
204
- }
131
+ mysqli_query ($ link , 'DROP TABLE IF EXISTS ' . $ table );
205
132
}
206
133
207
134
function setup_table_with_data_on_default_connection (string $ table ): mysqli {
0 commit comments