diff --git a/ext/pdo/tests/pdo_034.phpt b/ext/pdo/tests/pdo_034.phpt deleted file mode 100644 index 89b691e6cfc3f..0000000000000 --- a/ext/pdo/tests/pdo_034.phpt +++ /dev/null @@ -1,69 +0,0 @@ ---TEST-- -PDO Common: PDO::FETCH_KEY_PAIR fetch mode test ---EXTENSIONS-- -pdo ---SKIPIF-- - ---FILE-- -exec("CREATE TABLE test034 (a varchar(100), b varchar(100), c varchar(100))"); - -for ($i = 0; $i < 5; $i++) { - $db->exec("INSERT INTO test034 (a,b,c) VALUES('test".$i."','".$i."','".$i."')"); -} - -var_dump($db->query("SELECT a,b FROM test034")->fetch(PDO::FETCH_KEY_PAIR)); -var_dump($db->query("SELECT a,b FROM test034")->fetchAll(PDO::FETCH_KEY_PAIR)); -var_dump($db->query("SELECT * FROM test034")->fetch(PDO::FETCH_KEY_PAIR)); -var_dump($db->query("SELECT a,a FROM test034")->fetchAll(PDO::FETCH_KEY_PAIR)); - -?> ---CLEAN-- - ---EXPECTF-- -array(1) { - ["test0"]=> - string(1) "0" -} -array(5) { - ["test0"]=> - string(1) "0" - ["test1"]=> - string(1) "1" - ["test2"]=> - string(1) "2" - ["test3"]=> - string(1) "3" - ["test4"]=> - string(1) "4" -} - -Warning: PDOStatement::fetch(): SQLSTATE[HY000]: General error: PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain exactly 2 columns. in %spdo_034.php on line %d - -Warning: PDOStatement::fetch(): SQLSTATE[HY000]: General error%spdo_034.php on line %d -bool(false) -array(5) { - ["test0"]=> - string(5) "test0" - ["test1"]=> - string(5) "test1" - ["test2"]=> - string(5) "test2" - ["test3"]=> - string(5) "test3" - ["test4"]=> - string(5) "test4" -} diff --git a/ext/pdo/tests/pdo_fetch_default.phpt b/ext/pdo/tests/pdo_fetch_default.phpt new file mode 100644 index 0000000000000..4dd37c9b66186 --- /dev/null +++ b/ext/pdo/tests/pdo_fetch_default.phpt @@ -0,0 +1,91 @@ +--TEST-- +PDO Common: PDO::FETCH_DEFAULT +--EXTENSIONS-- +pdo +--SKIPIF-- + +--CLEAN-- + +--FILE-- +setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, $expectedFetchMode); + +print "Original:\n"; +$stmt = $db->query($query); +print_r($stmt->fetch()); + +/* +FIXME https://github.com/php/php-src/issues/20214 +print "\nPDOStatement::setFetchMode:\n"; +$stmt = $db->query($query); +$stmt->setFetchMode(\PDO::FETCH_DEFAULT); +print_r($stmt->fetch()); +*/ + +print "\nPDOStatement::fetch:\n"; +$stmt = $db->query($query); +print_r($stmt->fetch(\PDO::FETCH_DEFAULT)); + +print "\nPDOStatement::fetchAll:\n"; +$stmt = $db->query($query); +print_r($stmt->fetchAll(\PDO::FETCH_DEFAULT)); + +print "\nPDO::setAttribute:\n"; +try { + $db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_DEFAULT); +} catch (ValueError $e) { + print "Could not set fetch mode using PDO::setAttribute: ". $e->getMessage() ."\n"; +} + +if ($db->getAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE) !== $expectedFetchMode) { + print __LINE__ ." Fetch mode changed\n"; +} + +print "Done!\n"; + +--EXPECT-- +Original: +stdClass Object +( + [name] => Chris + [country] => Ukraine +) + +PDOStatement::fetch: +stdClass Object +( + [name] => Chris + [country] => Ukraine +) + +PDOStatement::fetchAll: +Array +( + [0] => stdClass Object + ( + [name] => Chris + [country] => Ukraine + ) + +) + +PDO::setAttribute: +Could not set fetch mode using PDO::setAttribute: PDO::setAttribute(): Argument #2 ($value) Fetch mode must be a bitmask of PDO::FETCH_* constants +Done! diff --git a/ext/pdo/tests/pdo_fetch_keypair.phpt b/ext/pdo/tests/pdo_fetch_keypair.phpt new file mode 100644 index 0000000000000..2c1b43ee865d9 --- /dev/null +++ b/ext/pdo/tests/pdo_fetch_keypair.phpt @@ -0,0 +1,129 @@ +--TEST-- +PDO Common: PDO::FETCH_KEY_PAIR +--EXTENSIONS-- +pdo +--SKIPIF-- + +--CLEAN-- + +--FILE-- +query($query); +while (false !== ($result = $stmt->fetch($fetchMode))) { + print_r($result); +} + +print "\nfetchAll:\n"; +$stmt = $db->query($query); +$result = $stmt->fetchAll($fetchMode); +print_r($result); + +print "\nToo many columns:\n"; +$query = "SELECT {$table}.country, {$table}.name, {$table}.userid + FROM {$table}"; +$stmt = $db->query($query); +$result = $stmt->fetchAll($fetchMode); +var_dump($result); +# ODBC does not like leaving the result unfetched +$ignore = $stmt->fetchAll(\PDO::FETCH_OBJ); + +print "\nToo few columns:\n"; +$query = "SELECT {$table}.country + FROM {$table}"; +$stmt = $db->query($query); +$result = $stmt->fetchAll($fetchMode); +var_dump($result); +# ODBC does not like leaving the result unfetched +$ignore = $stmt->fetchAll(\PDO::FETCH_OBJ); + +print "\nSame column:\n"; +$query = "SELECT {$table}.country, {$table}.country + FROM {$table}"; +$stmt = $db->query($query); +$result = $stmt->fetchAll($fetchMode); +print_r($result); + +--EXPECTF-- +fetch: +Array +( + [Ukraine] => Chris +) +Array +( + [England] => Jamie +) +Array +( + [Germany] => Robin +) +Array +( + [Ukraine] => Sean +) +Array +( + [Germany] => Toni +) +Array +( + [Germany] => Toni +) +Array +( + [France] => Sean +) + +fetchAll: +Array +( + [Ukraine] => Sean + [England] => Jamie + [Germany] => Toni + [France] => Sean +) + +Too many columns: + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain exactly 2 columns. in %s on line %d + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error in %s on line %d +array(0) { +} + +Too few columns: + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain exactly 2 columns. in %s on line %s + +Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error in %s on line %d +array(0) { +} + +Same column: +Array +( + [Ukraine] => Ukraine + [England] => England + [Germany] => Germany + [France] => France +) diff --git a/ext/pdo/tests/pdo_fetch_named.phpt b/ext/pdo/tests/pdo_fetch_named.phpt new file mode 100644 index 0000000000000..5564bf5ce6a39 --- /dev/null +++ b/ext/pdo/tests/pdo_fetch_named.phpt @@ -0,0 +1,110 @@ +--TEST-- +PDO Common: PDO::FETCH_NAMED +--EXTENSIONS-- +pdo +--SKIPIF-- + +--CLEAN-- + +--FILE-- +query($query); +while (false !== ($result = $stmt->fetch($fetchMode))) { + var_dump($result); +} + +print "\nfetchAll:\n"; +$stmt = $db->query($query); +$result = $stmt->fetchAll($fetchMode); +var_dump($result); + +--EXPECT-- +fetch: +array(4) { + ["userid"]=> + string(3) "104" + ["name"]=> + array(2) { + [0]=> + string(5) "Chris" + [1]=> + NULL + } + ["country"]=> + string(7) "Ukraine" + ["referred_by_userid"]=> + NULL +} +array(4) { + ["userid"]=> + string(3) "107" + ["name"]=> + array(2) { + [0]=> + string(5) "Robin" + [1]=> + string(5) "Chris" + } + ["country"]=> + string(7) "Germany" + ["referred_by_userid"]=> + string(3) "104" +} + +fetchAll: +array(2) { + [0]=> + array(4) { + ["userid"]=> + string(3) "104" + ["name"]=> + array(2) { + [0]=> + string(5) "Chris" + [1]=> + NULL + } + ["country"]=> + string(7) "Ukraine" + ["referred_by_userid"]=> + NULL + } + [1]=> + array(4) { + ["userid"]=> + string(3) "107" + ["name"]=> + array(2) { + [0]=> + string(5) "Robin" + [1]=> + string(5) "Chris" + } + ["country"]=> + string(7) "Germany" + ["referred_by_userid"]=> + string(3) "104" + } +} diff --git a/ext/pdo/tests/pdo_fetch_named_group.phpt b/ext/pdo/tests/pdo_fetch_named_group.phpt new file mode 100644 index 0000000000000..b84bf66437ab3 --- /dev/null +++ b/ext/pdo/tests/pdo_fetch_named_group.phpt @@ -0,0 +1,211 @@ +--TEST-- +PDO Common: PDO::FETCH_NAMED with PDO::FETCH_GROUP +--EXTENSIONS-- +pdo +--SKIPIF-- + +--CLEAN-- + +--FILE-- +query($query . " WHERE {$table}.userid IN (104, 107)"); +while (false !== ($result = $stmt->fetch($fetchMode))) { + var_dump($result); +} + +print "\nfetchAll:\n"; +$stmt = $db->query($query); +$result = $stmt->fetchAll($fetchMode); +var_dump($result); + +--EXPECT-- +fetch: +array(4) { + ["country"]=> + array(2) { + [0]=> + string(7) "Ukraine" + [1]=> + string(7) "Ukraine" + } + ["userid"]=> + string(3) "104" + ["name"]=> + array(2) { + [0]=> + string(5) "Chris" + [1]=> + NULL + } + ["referred_by_userid"]=> + NULL +} +array(4) { + ["country"]=> + array(2) { + [0]=> + string(7) "Germany" + [1]=> + string(7) "Germany" + } + ["userid"]=> + string(3) "107" + ["name"]=> + array(2) { + [0]=> + string(5) "Robin" + [1]=> + string(5) "Chris" + } + ["referred_by_userid"]=> + string(3) "104" +} + +fetchAll: +array(4) { + ["Ukraine"]=> + array(2) { + [0]=> + array(4) { + ["userid"]=> + string(3) "104" + ["name"]=> + array(2) { + [0]=> + string(5) "Chris" + [1]=> + NULL + } + ["country"]=> + string(7) "Ukraine" + ["referred_by_userid"]=> + NULL + } + [1]=> + array(4) { + ["userid"]=> + string(3) "108" + ["name"]=> + array(2) { + [0]=> + string(4) "Sean" + [1]=> + NULL + } + ["country"]=> + string(7) "Ukraine" + ["referred_by_userid"]=> + NULL + } + } + ["England"]=> + array(1) { + [0]=> + array(4) { + ["userid"]=> + string(3) "105" + ["name"]=> + array(2) { + [0]=> + string(5) "Jamie" + [1]=> + NULL + } + ["country"]=> + string(7) "England" + ["referred_by_userid"]=> + NULL + } + } + ["Germany"]=> + array(3) { + [0]=> + array(4) { + ["userid"]=> + string(3) "107" + ["name"]=> + array(2) { + [0]=> + string(5) "Robin" + [1]=> + string(5) "Chris" + } + ["country"]=> + string(7) "Germany" + ["referred_by_userid"]=> + string(3) "104" + } + [1]=> + array(4) { + ["userid"]=> + string(3) "109" + ["name"]=> + array(2) { + [0]=> + string(4) "Toni" + [1]=> + NULL + } + ["country"]=> + string(7) "Germany" + ["referred_by_userid"]=> + NULL + } + [2]=> + array(4) { + ["userid"]=> + string(3) "110" + ["name"]=> + array(2) { + [0]=> + string(4) "Toni" + [1]=> + NULL + } + ["country"]=> + string(7) "Germany" + ["referred_by_userid"]=> + NULL + } + } + ["France"]=> + array(1) { + [0]=> + array(4) { + ["userid"]=> + string(3) "111" + ["name"]=> + array(2) { + [0]=> + string(4) "Sean" + [1]=> + NULL + } + ["country"]=> + string(6) "France" + ["referred_by_userid"]=> + NULL + } + } +} diff --git a/ext/pdo/tests/pdo_fetch_named_unique.phpt b/ext/pdo/tests/pdo_fetch_named_unique.phpt new file mode 100644 index 0000000000000..c1af2363d12a9 --- /dev/null +++ b/ext/pdo/tests/pdo_fetch_named_unique.phpt @@ -0,0 +1,106 @@ +--TEST-- +PDO Common: PDO::FETCH_NAMED with PDO::FETCH_UNIQUE +--EXTENSIONS-- +pdo +--SKIPIF-- + +--CLEAN-- + +--FILE-- +query($query); +while (false !== ($result = $stmt->fetch($fetchMode))) { + var_dump($result); +} + +print "\nfetchAll:\n"; +$stmt = $db->query($query); +$result = $stmt->fetchAll($fetchMode); +var_dump($result); + +--EXPECT-- +fetch: +array(4) { + ["userid"]=> + string(3) "104" + ["name"]=> + array(2) { + [0]=> + string(5) "Chris" + [1]=> + NULL + } + ["country"]=> + string(7) "Ukraine" + ["referred_by_userid"]=> + NULL +} +array(4) { + ["userid"]=> + string(3) "107" + ["name"]=> + array(2) { + [0]=> + string(5) "Robin" + [1]=> + string(5) "Chris" + } + ["country"]=> + string(7) "Germany" + ["referred_by_userid"]=> + string(3) "104" +} + +fetchAll: +array(2) { + [104]=> + array(3) { + ["name"]=> + array(2) { + [0]=> + string(5) "Chris" + [1]=> + NULL + } + ["country"]=> + string(7) "Ukraine" + ["referred_by_userid"]=> + NULL + } + [107]=> + array(3) { + ["name"]=> + array(2) { + [0]=> + string(5) "Robin" + [1]=> + string(5) "Chris" + } + ["country"]=> + string(7) "Germany" + ["referred_by_userid"]=> + string(3) "104" + } +} diff --git a/ext/pdo/tests/pdo_fetch_setup.php b/ext/pdo/tests/pdo_fetch_setup.php new file mode 100644 index 0000000000000..f666de7ad959b --- /dev/null +++ b/ext/pdo/tests/pdo_fetch_setup.php @@ -0,0 +1,53 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + +# Note: code below is written to be compatible in PHP 5.1+ (and sqlite of that era) (when PDO was introduced) +# This allows it to easily be used with 3v4l (et al) for historical behavior checks + +# This data set specifically includes the following features: +# * referred_by_userid for JOINs and duplicated column name +# * duplicated record (ignoring userid) +# * multiple entries using the same country (for FETCH_GROUP and FETCH_UNIQUE) +# * 2 countries which have the same name (but aren't otherwise duplicates) (for FETCH_GROUP and FETCH_UNIQUE) +# * names and countries between 4 and 7 chars for nice TSV alignment + +if (! isset($table)) { + die("Must set \$table before including pdo_fetch_setup.php"); +} + +# SQL Server requires this; Firebird error on it. +$nullable = (($db->getAttribute(PDO::ATTR_DRIVER_NAME) === 'dblib') ? 'NULL' : ''); + +$db->exec( + "CREATE TABLE {$table} ( + userid INT PRIMARY KEY, + name VARCHAR(20), + country VARCHAR(20), + referred_by_userid INT {$nullable} + )" +); + +$records = array( + array(104, 'Chris', 'Ukraine', NULL), + array(105, 'Jamie', 'England', NULL), + array(107, 'Robin', 'Germany', 104), + array(108, 'Sean', 'Ukraine', NULL), + array(109, 'Toni', 'Germany', NULL), + array(110, 'Toni', 'Germany', NULL), + array(111, 'Sean', 'France', NULL) +); +$insertSql = "INSERT INTO {$table} + (userid, name, country, referred_by_userid) + VALUES (:userid, :name, :country, :refid)"; +$insert = $db->prepare($insertSql); + +foreach ($records as $record) { + $insert->execute(array( + 'userid' => $record[0], + 'name' => $record[1], + 'country' => $record[2], + 'refid' => $record[3] + )); +}