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

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 Tue, 23 Sep 2014 16:38:04 +0000 (7 days ago)

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