php-src/ext/pdo_mysql/tests/pdo_mysql___construct_options.phpt
Johannes Schlüter be0793d2e7 MFH: Add mysqlnd support for PDO_mysql, fixes at least bug#41997,#42499,
pecl#12794, pecl#12401

# Running the tests:
# (Note: Doesn't work currnetly on HEAD, see:
#  http://news.php.net/php.qa/64378)
#
#  PDO_MYSQL_TEST_DSN  - DSN
#    For example: mysql:dbname=test;host=localhost;port=3306
#
#  PDO_MYSQL_TEST_HOST    - database host
#  PDO_MYSQL_TEST_DB      - database (schema) name
#  PDO_MYSQL_TEST_SOCKET  - database server socket
#  PDO_MYSQL_TEST_ENGINE  - storage engine to use
#  PDO_MYSQL_TEST_USER    - database user
#  PDO_MYSQL_TEST_PASS    - database user password
#  PDO_MYSQL_TEST_CHARSET - database charset
#
#  NOTE: if any of PDO_MYSQL_TEST_[HOST|DB|SOCKET|ENGINE|CHARSET] is
#  part of PDO_MYSQL_TEST_DSN, the values must match. That is, for example,
#  for PDO_MYSQL_TEST_DSN = mysql:dbname=test you MUST set PDO_MYSQL_TEST_DB=test.
2008-07-21 13:09:28 +00:00

170 lines
7.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--TEST--
MySQL PDO->__construct(), options
--SKIPIF--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
?>
--FILE--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
function set_option_and_check($offset, $option, $value, $option_desc) {
$dsn = MySQLPDOTest::getDSN();
$user = PDO_MYSQL_TEST_USER;
$pass = PDO_MYSQL_TEST_PASS;
try {
$db = new PDO($dsn, $user, $pass, array($option => $value));
if (!is_object($db) || ($value !== ($tmp = @$db->getAttribute($option))))
printf("[%03d] Execting '%s'/%s got '%s'/%s' for options '%s'\n",
$offset,
$value, gettype($value),
$tmp, gettype($tmp),
$option_desc);
} catch (PDOException $e) {
printf("[%03d] %s\n", $offset, $e->getMessage());
}
}
try {
$dsn = MySQLPDOTest::getDSN();
$user = PDO_MYSQL_TEST_USER;
$pass = PDO_MYSQL_TEST_PASS;
$valid_options = array(
/* pdo_dbh.c */
PDO::ATTR_PERSISTENT => 'PDO::ATTR_PERSISTENT',
PDO::ATTR_AUTOCOMMIT => 'PDO::ATTR_AUTOCOMMIT',
/* mysql_driver.c */
/* TODO Possible bug PDO::ATTR_TIMEOUT != MYSQLI_OPT_CONNECT_TIMEOUT*/
PDO::ATTR_TIMEOUT => 'PDO::ATTR_TIMEOUT',
PDO::ATTR_EMULATE_PREPARES => 'PDO::ATTR_EMULATE_PREPARES',
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => 'PDO::MYSQL_ATTR_USE_BUFFERED_QUERY',
PDO::MYSQL_ATTR_LOCAL_INFILE => 'PDO::MYSQL_ATTR_LOCAL_INFILE',
PDO::MYSQL_ATTR_DIRECT_QUERY => 'PDO::MYSQL_ATTR_DIRECT_QUERY',
);
$defaults = array(
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_AUTOCOMMIT => 1,
/* TODO - why is this a valid option if getAttribute() does not support it?! */
PDO::ATTR_TIMEOUT => false,
PDO::ATTR_EMULATE_PREPARES => 1,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => 1,
/* TODO getAttribute() does not handle it */
PDO::MYSQL_ATTR_LOCAL_INFILE => false,
/* TODO getAttribute() does not handle it */
PDO::MYSQL_ATTR_DIRECT_QUERY => 1,
);
if (NULL !== ($db = @new PDO($dsn, $user, $pass, 'wrong type')))
printf("[001] Expecting NULL got %s/%s\n", gettype($db), $db);
if (!is_object($db = new PDO($dsn, $user, $pass, array())))
printf("[002] Expecting object got %s/%s¸\n", gettype($db), $db);
do {
$invalid = mt_rand(-1000, 1000);
} while (isset($valid_options[$invalid]));
if (is_object($db = new PDO($dsn, $user, $pass, array($invalid => true))))
printf("[003] [TODO][CHANGEREQUEST] Please, lets not ignore invalid options and bail out!\n");
$db = new PDO($dsn, $user, $pass);
foreach ($valid_options as $option => $name) {
/* TODO getAttribute() is pretty poor in supporting the options, suppress errors */
$tmp = @$db->getAttribute($option);
if ($tmp !== $defaults[$option])
printf("[003a] Expecting default value for '%s' of '%s'/%s, getAttribute() reports setting '%s'/%s\n",
$name, $defaults[$option], gettype($defaults[$option]),
$tmp, gettype($tmp));
}
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_AUTOCOMMIT => true));
if (!is_object($db) || !$db->getAttribute(PDO::ATTR_AUTOCOMMIT))
printf("[004] Autocommit should be on\n");
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_AUTOCOMMIT => false));
if (!is_object($db) || $db->getAttribute(PDO::ATTR_AUTOCOMMIT))
printf("[005] Autocommit should be off\n");
/* TODO: no way to check ATTR_TIMEOUT settings */
if (!is_object($db = new PDO($dsn, $user, $pass, array(PDO::ATTR_TIMEOUT => 10))))
printf("[006] ATTR_TIMEOUT should be accepted\n");
if (!is_object($db = new PDO($dsn, $user, $pass, array(PDO::ATTR_TIMEOUT => PHP_INT_MAX))))
printf("[007] ATTR_TIMEOUT should be accepted\n");
if (!is_object($db = new PDO($dsn, $user, $pass, array(PDO::ATTR_TIMEOUT => -PHP_INT_MAX))))
printf("[008] ATTR_TIMEOUT should be accepted\n");
/* TODO: Its ugly that PDO::ATTR_EMULATE_PREPARES == PDO::MYSQL_ATTR_DIRECT_QUERY */
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => true));
if (!is_object($db))
printf("[009] ATTR_EMULATE_PREPARES should be accepted and on\n");
if (!$db->getAttribute(PDO::ATTR_EMULATE_PREPARES))
printf("[010] [TODO][CHANGEREQUEST] ATTR_EMULATE_PREPARES should be on\n");
if (!$db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
printf("[011] As PDO::MYSQL_ATTR_DIRECT_QUERY == PDO::ATTR_EMULATE_PREPARES
and PDO::ATTR_EMULATE_PREPARES overrules the other, PDO::MYSQL_ATTR_DIRECT_QUERY should be on\n");
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => false));
if (!is_object($db))
printf("[012] ATTR_EMULATE_PREPARES should be accepted and on\n");
if ($db->getAttribute(PDO::ATTR_EMULATE_PREPARES))
printf("[013] [TODO][CHANGEREQUEST] ATTR_EMULATE_PREPARES should be off\n");
if ($db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
printf("[014] As PDO::MYSQL_ATTR_DIRECT_QUERY == PDO::ATTR_EMULATE_PREPARES
and PDO::ATTR_EMULATE_PREPARES overrules the other, PDO::MYSQL_ATTR_DIRECT_QUERY should be off\n");
// PDO::ATTR_EMULATE_PREPARES overrules PDO::MYSQL_ATTR_DIRECT_QUERY
// TODO: is it clever that a generic setting overrules a specific setting?
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => true, PDO::MYSQL_ATTR_DIRECT_QUERY => false));
if (!$db->getAttribute(PDO::ATTR_EMULATE_PREPARES))
printf("[015] PDO::ATTR_EMULATE_PREPARES should be on\n");
if (!$db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
printf("[016] PDO::MYSQL_ATTR_DIRECT_QUERY should be on\n");
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::MYSQL_ATTR_DIRECT_QUERY => true));
if ($db->getAttribute(PDO::ATTR_EMULATE_PREPARES))
printf("[017] PDO::ATTR_EMULATE_PREPARES should be off\n");
if ($db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
printf("[018] PDO::MYSQL_ATTR_DIRECT_QUERY should be off\n");
set_option_and_check(19, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1, 'PDO::MYSQL_ATTR_USE_BUFFERED_QUERY');
set_option_and_check(20, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 0, 'PDO::MYSQL_ATTR_USE_BUFFERED_QUERY');
set_option_and_check(21, PDO::MYSQL_ATTR_LOCAL_INFILE, true, 'PDO::MYSQL_ATTR_LOCAL_INFILE');
set_option_and_check(22, PDO::MYSQL_ATTR_LOCAL_INFILE, false, 'PDO::MYSQL_ATTR_LOCAL_INFILE');
set_option_and_check(33, PDO::MYSQL_ATTR_DIRECT_QUERY, 1, 'PDO::MYSQL_ATTR_DIRECT_QUERY');
set_option_and_check(34, PDO::MYSQL_ATTR_DIRECT_QUERY, 0, 'PDO::MYSQL_ATTR_DIRECT_QUERY');
} catch (PDOException $e) {
printf("[001] %s, [%s] %s\n",
$e->getMessage(),
(is_object($db)) ? $db->errorCode() : 'n/a',
(is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a');
}
print "done!";
--EXPECTF--
[003] [TODO][CHANGEREQUEST] Please, lets not ignore invalid options and bail out!
[003a] Expecting default value for 'PDO::ATTR_EMULATE_PREPARES' of '1'/integer, getAttribute() reports setting ''/boolean
Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in %s on line %d
[010] [TODO][CHANGEREQUEST] ATTR_EMULATE_PREPARES should be on
Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in %s on line %d
Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in %s on line %d
[015] PDO::ATTR_EMULATE_PREPARES should be on
[016] PDO::MYSQL_ATTR_DIRECT_QUERY should be on
Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in %s on line %d
[018] PDO::MYSQL_ATTR_DIRECT_QUERY should be off
[021] Execting '1'/boolean got ''/boolean' for options 'PDO::MYSQL_ATTR_LOCAL_INFILE'
done!