PHP  
 PHP_HEAD
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

Test Failure Report for ext/pdo_mysql/tests/pdo_mysql___construct.phpt ('MySQL PDO->__construct() - Generic + DSN')

Script

1: <?php
2:     
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR 'mysql_pdo_test.inc');
3:
4:     function 
tryandcatch($offset$code) {
5:
6:         try {
7:             eval(
$code);
8:             
assert(sprintf("[%03d] Should have failed\n"$offset) != '');
9:         } catch (
PDOException $e) {
10:             return 
sprintf("[%03d] %s, [%s] %s\n",
11:                 
$offset,
12:                 
$e->getMessage(),
13:                 (isset(
$db) && is_object($db)) ? $db->errorCode() : 'n/a',
14:                 (isset(
$db) && is_object($db)) ? implode(' '$db->errorInfo()) : 'n/a');
15:         }
16:
17:         return 
'';
18:     }
19:
20:     try {
21:
22:         try {
23:             if (
NULL !== ($db = @new PDO()))
24:                 
printf("[001] Too few parameters\n");
25:         } catch (
TypeError $ex) {
26:         }
27:
28:         print 
tryandcatch(2'$db = new PDO(chr(0));');
29:         print 
tryandcatch(3'$db = new PDO("a" . chr(0) . "b");');
30:         print 
tryandcatch(4'$db = new PDO("MYSQL");');
31:         print 
tryandcatch(5'$db = new PDO("mysql");');
32:         print 
tryandcatch(6'$db = new PDO("mysql ");');
33:         print 
tryandcatch(7'$db = new PDO("fantasyandfriends :");');
34:
35:         
$dsn PDO_MYSQL_TEST_DSN;
36:         
// MySQL Server might accept anonymous connections, don't
37:         // print anything
38:         
tryandcatch(8'$db = new PDO("' $dsn '");');
39:
40:         
$user 'dontcreatesuchauser';
41:         
$pass 'withthispassword';
42:         print 
tryandcatch(9'$db = new PDO("' $dsn '", "' $user '", "' $pass '");');
43:
44:         
// should fail
45:         
$dsn 'mysql:';
46:         
// don't print the message since it can be different
47:         
tryandcatch(10'$db = new PDO("' $dsn '", "' $user '", "' $pass '");');
48:
49:         
$dsn PDO_MYSQL_TEST_DSN;
50:         
$user PDO_MYSQL_TEST_USER;
51:         
$pass    PDO_MYSQL_TEST_PASS;
52:         
// should work...
53:         
$db = new PDO($dsn$user$pass);
54:
55:         
// Reaction on host not specified differs for different configs, so no printing
56:         
$dsn 'mysql:invalid=foo';
57:         
tryandcatch(11'$db = new PDO("' $dsn '", "' $user '", "' $pass '");');
58:
59:         
$dsn 'mysql:' str_repeat('howmuch=canpdoeat;'1000);
60:         
tryandcatch(12'$db = new PDO("' $dsn '", "' $user '", "' $pass '");');
61:
62:         
$dsn 'mysql:' str_repeat('abcdefghij'1024 10) . '=somevalue';
63:         
tryandcatch(13'$db = new PDO("' $dsn '", "' $user '", "' $pass '");');
64:
65:         if (
PDO_MYSQL_TEST_HOST) {
66:             
$host PDO_MYSQL_TEST_HOST;
67:             
$invalid_host $host 'invalid';
68:
69:             
// last host specification should be the one used
70:             
$dsn MySQLPDOTest::getDSN(array('host' => $host), 'host=' $invalid_host);
71:             try { 
$db = @new PDO($dsn$user$pass); assert(false); printf("%s\n"$dsn); } catch (PDOException $e) {
72:                 
$tmp $e->getMessage();
73:                 if (!
stristr($tmp'HY000') && !stristr($tmp'2005') && !stristr($tmp'2002'))
74:                     
printf("[014] Cannot find proper error codes: %s\n"$tmp);
75:             }
76:
77:             
$dsn MySQLPDOTest::getDSN(array('host' => $invalid_host), 'host=' $host);
78:             try { 
$db = @new PDO($dsn$user$pass); } catch (PDOException $e) {
79:                 
printf("[015] DSN=%s, %s\n"$dsn$e->getMessage());
80:             }
81:
82:             
$invalid_host '-' chr(0);
83:
84:             
$dsn MySQLPDOTest::getDSN(array('host' => $invalid_host));
85:             try { 
$db = @new PDO($dsn$user$pass); assert(false); printf("%s\n"$dsn); } catch (PDOException $e) {
86:                 
$tmp $e->getMessage();
87:                 if (!
stristr($tmp'HY000') && !stristr($tmp'2005') && !stristr($tmp'2002'))
88:                     
printf("[016] Cannot find proper error codes: %s\n"$tmp);
89:             }
90:
91:             
// parsing should not get confused by chr(0)
92:             
$dsn MySQLPDOTest::getDSN(array('host' => $invalid_host), 'host=' $host);
93:             try { 
$db = @new PDO($dsn$user$pass); } catch (PDOException $e) {
94:                 
printf("[017] DSN=%s, %s\n"$dsn$e->getMessage());
95:             }
96:
97:         }
98:
99:         
// what about long values for a valid option ...
100:         // hostnames > 1024 chars break on some NIS-enabled FreeBSD...
101:         
$dsn MySQLPDOTest::getDSN(array('host' => str_repeat('0123456789'100)));
102:         try { 
$db = @new PDO($dsn$user$pass); assert(false); printf("%s\n"$dsn); } catch (PDOException $e) {
103:             
$tmp $e->getMessage();
104:             if (!
stristr($tmp'HY000') && !stristr($tmp'2005') && !stristr($tmp'2002'))
105:                 
printf("[018] Cannot find proper error codes: %s\n"$tmp);
106:         }
107:
108:         if (
PDO_MYSQL_TEST_PORT && (PDO_MYSQL_TEST_SOCKET == '')) {
109:             
// Playing with the port makes only sense if no socket gets used
110:
111:             
$port PDO_MYSQL_TEST_PORT;
112:             
// let's hope we don't hit a MySQL running on that port...
113:             
$invalid_port $port 2;
114:
115:             
$dsn MySQLPDOTest::getDSN(array('port' => $port), 'port=' $invalid_port);
116:             try { 
$db = @new PDO($dsn$user$pass); assert(false); printf("%s\n"$dsn); } catch (PDOException $e) {
117:                 
$tmp $e->getMessage();
118:                 if (!
stristr($tmp'HY000') && !stristr($tmp'2005'))
119:                     
printf("[019] Cannot find proper error codes: %s\n"$tmp);
120:             }
121:
122:             
$dsn MySQLPDOTest::getDSN(array('port' => $invalid_port), 'port=' $port);
123:             try { 
$db = @new PDO($dsn$user$pass); } catch (PDOException $e) {
124:                 
printf("[020] DSN=%s, %s\n"$dsn$e->getMessage());
125:             }
126:
127:             
$invalid_port 'abc';
128:             
$dsn MySQLPDOTest::getDSN(array('port' => $port), 'port=' $invalid_port);
129:             try {
130:                 
$db = @new PDO($dsn$user$pass);
131:                 
// atoi('abc') = 0, 0 -> fallback to default 3306 -> may or may not fail!
132:             
} catch (PDOException $e) {
133:             }
134:
135:         }
136:
137:         if (
PDO_MYSQL_TEST_DB) {
138:             
$db PDO_MYSQL_TEST_DB;
139:             
$invalid_db 'letshopeitdoesnotexist';
140:
141:             
$dsn MySQLPDOTest::getDSN(array('dbname' => $db), 'dbname=' $invalid_db);
142:             try { 
$db = @new PDO($dsn$user$pass); assert(false); printf("%s\n"$dsn); } catch (PDOException $e) {
143:                 
$tmp $e->getMessage();
144:                 if (!
stristr($tmp'42000') && !stristr($tmp'1049'))
145:                     
printf("[022] Cannot find proper error codes: %s\n"$tmp);
146:             }
147:
148:             
$dsn MySQLPDOTest::getDSN(array('dbname' => $invalid_db), 'dbname=' $db);
149:             try { 
$db = @new PDO($dsn$user$pass); } catch (PDOException $e) {
150:                 
printf("[023] DSN=%s, %s\n"$dsn$e->getMessage());
151:             }
152:
153:         }
154:
155:         if (
PDO_MYSQL_TEST_SOCKET && (stristr(PDO_MYSQL_TEST_DSNPDO_MYSQL_TEST_SOCKET) !== false)) {
156:             
$socket PDO_MYSQL_TEST_SOCKET;
157:             
$invalid_socket '/lets/hope/it/does/not/exist';
158:
159:             
$dsn MySQLPDOTest::getDSN(array('unix_socket' => $socket), 'unix_socket=' $invalid_socket);
160:             try { 
$db = @new PDO($dsn$user$pass); assert(false); printf("%s\n"$dsn); } catch (PDOException $e) {
161:                 
$tmp $e->getMessage();
162:                 if (!
stristr($tmp'HY000') && !stristr($tmp'2002'))
163:                     
printf("[024] Cannot find proper error codes: %s\n"$tmp);
164:             }
165:
166:             
$dsn MySQLPDOTest::getDSN(array('unix_socket' => $invalid_socket), 'unix_socket=' $socket);
167:             try { 
$db = @new PDO($dsn$user$pass); } catch (PDOException $e) {
168:                 
printf("[025] DSN=%s, %s\n"$dsn$e->getMessage());
169:             }
170:
171:         }
172:
173:         
$have_charset_support false;
174:         
$dsn MySQLPDOTest::getDSN();
175:         try {
176:             
$db = new PDO($dsn$user$pass);
177:             
$stmt $db->query('SELECT VERSION() as _version');
178:             
$version $stmt->fetch(PDO::FETCH_ASSOC);
179:
180:             
$tmp explode('.'$version['_version']);
181:             if ((
count($tmp) == 3) &&
182:                     ((
$tmp[0] >= && $tmp[1] >= 1) || ($tmp[0] >= 5))) {
183:                 
// MySQL Server 4.1 - charset support available
184:                 
$have_charset_support true;
185:             }
186:
187:         } catch (
PDOException $e) {
188:             
printf("[026] DSN=%s, %s\n"$dsn$e->getMessage());
189:         }
190:
191:         if (
PDO_MYSQL_TEST_CHARSET) {
192:             
$charset PDO_MYSQL_TEST_CHARSET;
193:             
$invalid_charset 'invalid';
194:
195:             if (
$have_charset_support) {
196:                 
$dsn MySQLPDOTest::getDSN();
197:                 
$db = new PDO($dsn$user$pass);
198:                 
$stmt $db->query(sprintf('SHOW CHARACTER SET LIKE "%s"'$charset));
199:                 
$tmp $stmt->fetch(PDO::FETCH_ASSOC);
200:                 
$have_charset = (empty($tmp)) ? false true;
201:
202:                 if (
$have_charset) {
203:                     
$dsn MySQLPDOTest::getDSN(array('charset' => $charset), 'charset=' $invalid_charset);
204:                     try {
205:                         
$db = @new PDO($dsn$user$pass);
206:                         
/* NOTE: MySQL does a fallback to the charset suggested during the handshake - no error - no bug! */
207:                     
} catch (PDOException $e) {
208:                         
$tmp $e->getMessage();
209:                         
/* TODO: add proper codes */
210:                         
if (!stristr($tmp'sqlstatecode') || !stristr($tmp'mysqlinternalerrcode'))
211:                             
printf("[027] TODO - Cannot find proper error codes: %s\n"$tmp);
212:                     }
213:
214:                     
$dsn MySQLPDOTest::getDSN(array('charset' => $invalid_charset), 'charset=' $charset);
215:                     try {
216:                         
$db = @new PDO($dsn$user$pass);
217:                         
/* Strictly speaking we should test more: character_set_client, character_set_results, and character_set_connection */
218:                         
$stmt $db->query('SELECT @@character_set_connection AS _charset');
219:                         
$tmp $stmt->fetch(PDO::FETCH_ASSOC);
220:                         if (
$tmp['_charset'] != $charset)
221:                             
printf("[028] Character sets has not been set, @@character_set_connection reports '%s', expecting '%s'",
222:                                 
$tmp['_charset'], $charset);
223:                     } catch (
PDOException $e) {
224:                         
printf("[029] DSN=%s, %s\n"$dsn$e->getMessage());
225:                     }
226:                 } else {
227:                     
printf("[030] You're trying to run the tests with charset '%s' which seems not supported by the server!"$charset);
228:                 }
229:
230:             }
231:
232:         }
233:
234:         if (
$have_charset_support) {
235:             
// In case the PDO_MYSQL_TEST_CHARSET interferes with any defaults
236:             // we do another test to verify that the charset has been set.
237:             
$dsn MySQLPDOTest::getDSN();
238:             
$db = new PDO($dsn$user$pass);
239:             
$stmt $db->query('SHOW CHARACTER SET LIKE "latin1"');
240:             
$tmp $stmt->fetch(PDO::FETCH_ASSOC);
241:             
$have_latin1 =(empty($tmp)) ? false true;
242:             
$stmt $db->query('SHOW CHARACTER SET LIKE "latin2"');
243:             
$tmp $stmt->fetch(PDO::FETCH_ASSOC);
244:             
$have_latin2 =(empty($tmp)) ? false true;
245:
246:             if (
$have_latin1 && $have_latin2) {
247:                 
// very likely we do have both of them...
248:                 
try {
249:                     
$dsn MySQLPDOTest::getDSN(array('charset' => 'latin1'));
250:                     
$db = new PDO($dsn$user$pass);
251:                     
$stmt $db->query('SELECT @@character_set_connection AS _charset');
252:                     
$tmp $stmt->fetch(PDO::FETCH_ASSOC);
253:                     if (
$tmp['_charset'] != 'latin1')
254:                         
printf("[031] DSN = %s, Character sets has not been set, @@character_set_connection reports '%s', expecting '%s'",
255:                             
$dsn$tmp['_charset'], 'latin1');
256:
257:                 } catch (
PDOException $e) {
258:                     
printf("[032] %s\n"$e->getMessage());
259:                 }
260:
261:                 try {
262:                     
$dsn MySQLPDOTest::getDSN(array('charset' => 'latin2'));
263:                     
$db = new PDO($dsn$user$pass);
264:                     
$stmt $db->query('SELECT @@character_set_connection AS _charset');
265:                     
$tmp $stmt->fetch(PDO::FETCH_ASSOC);
266:                     if (
$tmp['_charset'] != 'latin2')
267:                         
printf("[033] DSN = %s, character sets has not been set, @@character_set_connection reports '%s', expecting '%s'",
268:                             
$dsn$tmp['_charset'], 'latin2');
269:
270:                 } catch (
PDOException $e) {
271:                     
printf("[034] %s\n"$e->getMessage());
272:                 }
273:
274:             }
275:         }
276:
277:     } catch (
PDOException $e) {
278:         
printf("[001] %s, [%s] %s\n",
279:             
$e->getMessage(),
280:             (
is_object($db)) ? $db->errorCode() : 'n/a',
281:             (
is_object($db)) ? implode(' '$db->errorInfo()) : 'n/a');
282:     }
283:
284:     print 
"done!";
285:
?>
286:

Expected

[002] invalid data source name, [n/a] n/a
[003] invalid data source name, [n/a] n/a
[004] invalid data source name, [n/a] n/a
[005] invalid data source name, [n/a] n/a
[006] invalid data source name, [n/a] n/a
[007] could not find driver, [n/a] n/a
[009] SQLSTATE[%s] [1045] Access denied for user 'dont%s'@'%s' (using password: YES), [n/a] n/a
[017] DSN=%s, SQLSTATE[%s] [%d] %s
done!

Output

[002] invalid data source name, [n/a] n/a
[003] invalid data source name, [n/a] n/a
[004] invalid data source name, [n/a] n/a
[005] invalid data source name, [n/a] n/a
[006] invalid data source name, [n/a] n/a
[007] could not find driver, [n/a] n/a
[009] SQLSTATE[HY000] [1045] Access denied for user 'dontcreatesuchau'@'localhost' (using password: YES), [n/a] n/a
[017] DSN=mysql:unix_socket=/var/lib/mysql/mysql.sock;dbname=test;host=-;host=localhost, SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
[022] Cannot find proper error codes: SQLSTATE[HY000] [1044] Access denied for user 'phpqa'@'localhost' to database 'letshopeitdoesnotexist'
done!

Diff

009+ [022] Cannot find proper error codes: SQLSTATE[HY000] [1044] Access denied for user 'phpqa'@'localhost' to database 'letshopeitdoesnotexist'

 

Generated at Sat, 29 Aug 2015 10:22:31 +0000 (2 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.