1--TEST-- 2MySQL PDO->__construct() - URI 3--SKIPIF-- 4<?php 5require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc'); 6require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 7MySQLPDOTest::skip(); 8?> 9--FILE-- 10<?php 11 require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 12 13 try { 14 15 if ($tmp = MySQLPDOTest::getTempDir()) { 16 17 $file = $tmp . DIRECTORY_SEPARATOR . 'pdomuri.tst'; 18 $dsn = MySQLPDOTest::getDSN(); 19 $user = PDO_MYSQL_TEST_USER; 20 $pass = PDO_MYSQL_TEST_PASS; 21 $uri = sprintf('uri:file://%s', (substr(PHP_OS, 0, 3) == 'WIN' ? str_replace('\\', '/', $file) : $file)); 22 23 if ($fp = @fopen($file, 'w')) { 24 // ok, great we can create a file with a DSN in it 25 fwrite($fp, $dsn); 26 fclose($fp); 27 clearstatcache(); 28 assert(file_exists($file)); 29 try { 30 $db = new PDO($uri, $user, $pass); 31 } catch (PDOException $e) { 32 printf("[002] URI=%s, DSN=%s, File=%s (%d bytes, '%s'), %s\n", 33 $uri, $dsn, 34 $file, filesize($file), file_get_contents($file), 35 $e->getMessage()); 36 } 37 unlink($file); 38 } 39 40 if ($fp = @fopen($file, 'w')) { 41 fwrite($fp, sprintf('mysql:dbname=letshopeinvalid;%s%s', 42 chr(0), $dsn)); 43 fclose($fp); 44 clearstatcache(); 45 assert(file_exists($file)); 46 try { 47 $db = new PDO($uri, $user, $pass); 48 } catch (PDOException $e) { 49 $expected = array( 50 "SQLSTATE[HY000] [1049] Unknown database 'letshopeinvalid'", 51 "SQLSTATE[HY000] [2002] No such file or directory" 52 ); 53 printf("[003] URI=%s, DSN=%s, File=%s (%d bytes, '%s'), chr(0) test, %s\n", 54 $uri, $dsn, 55 $file, filesize($file), file_get_contents($file), 56 (in_array($e->getMessage(), $expected) ? 'EXPECTED ERROR' : $e->getMessage())); 57 } 58 unlink($file); 59 } 60 61 } 62 63 /* TODO: safe mode */ 64 65 } catch (PDOException $e) { 66 printf("[001] %s, [%s] %s\n", 67 $e->getMessage(), 68 (is_object($db)) ? $db->errorCode() : 'n/a', 69 (is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a'); 70 } 71 72 print "done!"; 73?> 74--EXPECTF-- 75[003] URI=uri:file://%spdomuri.tst, DSN=mysql%sdbname=%s, File=%spdomuri.tst (%d bytes, 'mysql%sdbname=letshopeinvalid%s'), chr(0) test, EXPECTED ERROR 76done! 77