1--TEST-- 2MySQL PDO->__construct(), options 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 function set_option_and_check($offset, $option, $value, $option_desc) { 14 15 $dsn = MySQLPDOTest::getDSN(); 16 $user = PDO_MYSQL_TEST_USER; 17 $pass = PDO_MYSQL_TEST_PASS; 18 19 try { 20 $db = new PDO($dsn, $user, $pass, array($option => $value)); 21 if (!is_object($db) || ($value !== ($tmp = @$db->getAttribute($option)))) 22 printf("[%03d] Execting '%s'/%s got '%s'/%s' for options '%s'\n", 23 $offset, 24 $value, gettype($value), 25 $tmp, gettype($tmp), 26 $option_desc); 27 } catch (PDOException $e) { 28 printf("[%03d] %s\n", $offset, $e->getMessage()); 29 } 30 31 } 32 33 try { 34 35 $dsn = MySQLPDOTest::getDSN(); 36 $user = PDO_MYSQL_TEST_USER; 37 $pass = PDO_MYSQL_TEST_PASS; 38 39 $valid_options = array( 40 /* pdo_dbh.c */ 41 PDO::ATTR_PERSISTENT => 'PDO::ATTR_PERSISTENT', 42 PDO::ATTR_AUTOCOMMIT => 'PDO::ATTR_AUTOCOMMIT', 43 /* mysql_driver.c */ 44 /* TODO Possible bug PDO::ATTR_TIMEOUT != MYSQLI_OPT_CONNECT_TIMEOUT*/ 45 PDO::ATTR_TIMEOUT => 'PDO::ATTR_TIMEOUT', 46 PDO::ATTR_EMULATE_PREPARES => 'PDO::ATTR_EMULATE_PREPARES', 47 48 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => 'PDO::MYSQL_ATTR_USE_BUFFERED_QUERY', 49 PDO::MYSQL_ATTR_LOCAL_INFILE => 'PDO::MYSQL_ATTR_LOCAL_INFILE', 50 PDO::MYSQL_ATTR_DIRECT_QUERY => 'PDO::MYSQL_ATTR_DIRECT_QUERY', 51 52 PDO::MYSQL_ATTR_INIT_COMMAND => 'PDO::MYSQL_ATTR_INIT_COMMAND', 53 PDO::ATTR_EMULATE_PREPARES => 'PDO::ATTR_EMULATE_PREPARES', 54 ); 55 56 $defaults = array( 57 PDO::ATTR_PERSISTENT => false, 58 PDO::ATTR_AUTOCOMMIT => 1, 59 /* TODO - why is this a valid option if getAttribute() does not support it?! */ 60 PDO::ATTR_TIMEOUT => false, 61 PDO::ATTR_EMULATE_PREPARES => 1, 62 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => 1, 63 /* TODO getAttribute() does not handle it */ 64 PDO::MYSQL_ATTR_LOCAL_INFILE => false, 65 /* TODO getAttribute() does not handle it */ 66 PDO::MYSQL_ATTR_DIRECT_QUERY => 1, 67 PDO::MYSQL_ATTR_INIT_COMMAND => '', 68 ); 69 70 if (NULL !== ($db = @new PDO($dsn, $user, $pass, 'wrong type'))) 71 printf("[001] Expecting NULL got %s/%s\n", gettype($db), $db); 72 73 if (!is_object($db = new PDO($dsn, $user, $pass, array()))) 74 printf("[002] Expecting object got %s/%s¸\n", gettype($db), $db); 75 76 do { 77 $invalid = mt_rand(-1000, 1000); 78 } while (isset($valid_options[$invalid])); 79 if (is_object($db = new PDO($dsn, $user, $pass, array($invalid => true)))) 80 printf("[003] [TODO][CHANGEREQUEST] Please, lets not ignore invalid options and bail out!\n"); 81 82 $db = new PDO($dsn, $user, $pass); 83 foreach ($valid_options as $option => $name) { 84 /* TODO getAttribute() is pretty poor in supporting the options, suppress errors */ 85 $tmp = @$db->getAttribute($option); 86 if ($tmp !== $defaults[$option]) 87 printf("[003a] Expecting default value for '%s' of '%s'/%s, getAttribute() reports setting '%s'/%s\n", 88 $name, $defaults[$option], gettype($defaults[$option]), 89 $tmp, gettype($tmp)); 90 } 91 92 $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_AUTOCOMMIT => true)); 93 if (!is_object($db) || !$db->getAttribute(PDO::ATTR_AUTOCOMMIT)) 94 printf("[004] Autocommit should be on\n"); 95 96 $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_AUTOCOMMIT => false)); 97 if (!is_object($db) || $db->getAttribute(PDO::ATTR_AUTOCOMMIT)) 98 printf("[005] Autocommit should be off\n"); 99 100 /* TODO: no way to check ATTR_TIMEOUT settings */ 101 if (!is_object($db = new PDO($dsn, $user, $pass, array(PDO::ATTR_TIMEOUT => 10)))) 102 printf("[006] ATTR_TIMEOUT should be accepted\n"); 103 104 if (!is_object($db = new PDO($dsn, $user, $pass, array(PDO::ATTR_TIMEOUT => PHP_INT_MAX)))) 105 printf("[007] ATTR_TIMEOUT should be accepted\n"); 106 107 if (!is_object($db = new PDO($dsn, $user, $pass, array(PDO::ATTR_TIMEOUT => -PHP_INT_MAX)))) 108 printf("[008] ATTR_TIMEOUT should be accepted\n"); 109 110 /* TODO: Its ugly that PDO::ATTR_EMULATE_PREPARES == PDO::MYSQL_ATTR_DIRECT_QUERY */ 111 $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => true)); 112 if (!is_object($db)) 113 printf("[009] ATTR_EMULATE_PREPARES should be accepted and on\n"); 114 if (!$db->getAttribute(PDO::ATTR_EMULATE_PREPARES)) 115 printf("[010] [TODO][CHANGEREQUEST] ATTR_EMULATE_PREPARES should be on\n"); 116 if (!$db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 117 printf("[011] As PDO::MYSQL_ATTR_DIRECT_QUERY == PDO::ATTR_EMULATE_PREPARES 118 and PDO::ATTR_EMULATE_PREPARES overrules the other, PDO::MYSQL_ATTR_DIRECT_QUERY should be on\n"); 119 120 $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => false)); 121 if (!is_object($db)) 122 printf("[012] ATTR_EMULATE_PREPARES should be accepted and on\n"); 123 if ($db->getAttribute(PDO::ATTR_EMULATE_PREPARES)) 124 printf("[013] [TODO][CHANGEREQUEST] ATTR_EMULATE_PREPARES should be off\n"); 125 if ($db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 126 printf("[014] As PDO::MYSQL_ATTR_DIRECT_QUERY == PDO::ATTR_EMULATE_PREPARES 127 and PDO::ATTR_EMULATE_PREPARES overrules the other, PDO::MYSQL_ATTR_DIRECT_QUERY should be off\n"); 128 129 // PDO::ATTR_EMULATE_PREPARES overrules PDO::MYSQL_ATTR_DIRECT_QUERY 130 // TODO: is it clever that a generic setting overrules a specific setting? 131 $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => true, PDO::MYSQL_ATTR_DIRECT_QUERY => false)); 132 if (!$db->getAttribute(PDO::ATTR_EMULATE_PREPARES)) 133 printf("[015] PDO::ATTR_EMULATE_PREPARES should be on\n"); 134 if (!$db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 135 printf("[016] PDO::MYSQL_ATTR_DIRECT_QUERY should be on\n"); 136 137 $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::MYSQL_ATTR_DIRECT_QUERY => true)); 138 if ($db->getAttribute(PDO::ATTR_EMULATE_PREPARES)) 139 printf("[017] PDO::ATTR_EMULATE_PREPARES should be off\n"); 140 if ($db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY)) 141 printf("[018] PDO::MYSQL_ATTR_DIRECT_QUERY should be off\n"); 142 143 set_option_and_check(19, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1, 'PDO::MYSQL_ATTR_USE_BUFFERED_QUERY'); 144 set_option_and_check(20, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 0, 'PDO::MYSQL_ATTR_USE_BUFFERED_QUERY'); 145 146 set_option_and_check(21, PDO::MYSQL_ATTR_LOCAL_INFILE, true, 'PDO::MYSQL_ATTR_LOCAL_INFILE'); 147 set_option_and_check(22, PDO::MYSQL_ATTR_LOCAL_INFILE, false, 'PDO::MYSQL_ATTR_LOCAL_INFILE'); 148 149 set_option_and_check(23, PDO::MYSQL_ATTR_INIT_COMMAND, 'SET @a=1', 'PDO::MYSQL_ATTR_INIT_COMMAND'); 150 set_option_and_check(24, PDO::MYSQL_ATTR_INIT_COMMAND, '', 'PDO::MYSQL_ATTR_INIT_COMMAND'); 151 set_option_and_check(25, PDO::MYSQL_ATTR_INIT_COMMAND, 'INSERT INTO nonexistent(invalid) VALUES (1)', 'PDO::MYSQL_ATTR_INIT_COMMAND'); 152 153 set_option_and_check(33, PDO::MYSQL_ATTR_DIRECT_QUERY, 1, 'PDO::MYSQL_ATTR_DIRECT_QUERY'); 154 set_option_and_check(34, PDO::MYSQL_ATTR_DIRECT_QUERY, 0, 'PDO::MYSQL_ATTR_DIRECT_QUERY'); 155 156 } catch (PDOException $e) { 157 printf("[001] %s, [%s] %s\n", 158 $e->getMessage(), 159 (is_object($db)) ? $db->errorCode() : 'n/a', 160 (is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a'); 161 } 162 163 print "done!"; 164?> 165--EXPECTF-- 166[003] [TODO][CHANGEREQUEST] Please, lets not ignore invalid options and bail out! 167[003a] Expecting default value for 'PDO::MYSQL_ATTR_INIT_COMMAND' of ''/string, getAttribute() reports setting ''/boolean 168[015] PDO::ATTR_EMULATE_PREPARES should be on 169[016] PDO::MYSQL_ATTR_DIRECT_QUERY should be on 170[017] PDO::ATTR_EMULATE_PREPARES should be off 171[018] PDO::MYSQL_ATTR_DIRECT_QUERY should be off 172[021] Execting '1'/boolean got ''/boolean' for options 'PDO::MYSQL_ATTR_LOCAL_INFILE' 173[023] Execting 'SET @a=1'/string got ''/boolean' for options 'PDO::MYSQL_ATTR_INIT_COMMAND' 174[024] SQLSTATE[42000] [1065] Query was empty 175[025] SQLSTATE[42S02] [1146] Table '%s.nonexistent' doesn't exist 176done! 177