Author: uwendel
Date: 2008-03-07 14:23:41 +0100 (Fri, 07 Mar 2008)
New Revision: 1323
Added:
trunk/tests/ext/pdo/pdo_stmt_fetchall.phpt
trunk/tests/ext/pdo/pdo_stmt_fetchall_group.phpt
Log:
Test for PDOStatement::fetchAll(). Don't check how it works in detail. The assorted flags
are neither well documented nor have I been able to figure out the logic behind some of
them.
Added: trunk/tests/ext/pdo/pdo_stmt_fetchall.phpt
===================================================================
--- trunk/tests/ext/pdo/pdo_stmt_fetchall.phpt (rev 0)
+++ trunk/tests/ext/pdo/pdo_stmt_fetchall.phpt 2008-03-07 13:23:41 UTC (rev 1323)
@@ -0,0 +1,135 @@
+--TEST--
+PDO Common: PDOStatement->fetchAll()
+--SKIPIF--
+<?php # vim:ft=php
+if (!extension_loaded('pdo')) die('skip');
+$dir = getenv('REDIR_TEST_DIR');
+if (false == $dir) die('skip no driver');
+require_once $dir . 'pdo_test.inc';
+PDOTest::skip();
+?>
+--FILE--
+<?php
+if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) .
'/../../pdo/tests/');
+require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
+$db = PDOTest::factory();
+
+$db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY, val VARCHAR(10), grp
VARCHAR(10))');
+$db->exec('INSERT INTO test VALUES(1, \'A\', \'Group1\')');
+$db->exec('INSERT INTO test VALUES(2, \'B\', \'Group2\')');
+
+try {
+
+ // default settings
+ $query = 'SELECT id FROM test ORDER by ID';
+ $stmt = $db->prepare($query);
+ $stmt->execute();
+ $expected = $stmt->fetchAll();
+ var_dump($expected);
+
+ if (!is_array($tmp = $stmt->fetchAll()) || (count($tmp) > 0))
+ printf("[002] Expecting empty array, got %s\n", var_export($tmp, true));
+
+ $stmt->execute();
+ $row = $stmt->fetch();
+ $rows = $stmt->fetchAll();
+ if ($expected[1] !== $rows[0])
+ printf("[003] Expecting %s got %s\n", var_export($expected[1], true),
var_export($rows[0], true));
+
+ // default shall equal FETCH_BOTH
+ $stmt->execute();
+ $tmp = $stmt->fetchAll(PDO::FETCH_BOTH);
+ if ($tmp != $expected)
+ printf("[004] PDO::FETCH_BOTH and default fetch setting differ, default: %s,
FETCH_BOTH: %s\n",
+ var_export($expected, true), var_export($tmp, true));
+
+ // FETCH_ASSOC
+ $tmp = array();
+ $stmt->execute();
+ $tmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
+ if (count($tmp) == count($expected)) {
+ foreach ($tmp as $k => $v)
+ if (($v['id'] !== $expected[$k]['id']) || (count($v) > 1))
+ printf("[006] Expecting %s got %s\n", var_export($expected[$k]['id'], true),
var_export($v['id'], true));
+ } else {
+ printf("[005] Got %d rows expecting %d rows\n",
+ count($tmp), count($expected));
+ }
+
+ // FETCH_NUM
+ $tmp = array();
+ $stmt->execute();
+ $tmp = $stmt->fetchAll(PDO::FETCH_NUM);
+ if (count($tmp) == count($expected)) {
+ foreach ($tmp as $k => $v)
+ if (($v[0] !== $expected[$k][0]) || (count($v) > 1))
+ printf("[007] Expecting %s got %s\n", var_export($expected[$k][0], true),
var_export($v[0], true));
+ } else {
+ printf("[008] Got %d rows expecting %d rows\n",
+ count($tmp), count($expected));
+ }
+
+ // FETCH_OBJ
+ $tmp = array();
+ $offset = 0;
+ $stmt->execute();
+ $tmp = $stmt->fetchAll(PDO::FETCH_OBJ);
+ if (count($tmp) == count($expected)) {
+ foreach ($tmp as $k => $row) {
+ if (get_class($row) !== 'stdClass')
+ printf("[010] Expecting object of type stdClass got %s\n", get_class($row));
+
+ if (property_exists(get_class($row), 'id'))
+ printf("[011] That's a BC change to 5.3 in Feb 2008\n");
+
+ if ($row->id !== $expected[$offset]['id'])
+ printf("[012] Expecting id = %s got %s\n",
+ var_export($expected[$offset]['id'], true),
+ var_export($row->id, true));
+
+ $offset++;
+ }
+ } else {
+ printf("[009] Got %d rows expecting %d rows\n",
+ count($tmp), count($expected));
+ }
+
+
+ $stmt->execute();
+ $row = $stmt->fetch(PDO::FETCH_OBJ);
+ $row->id = 'myvalue';
+ $tmp = $stmt->fetchAll(PDO::FETCH_OBJ);
+ $tmp[0]->id = 'myvalue';
+
+
+ // PDO::FETCH_LAZY: combines PDO::FETCH_BOTH and PDO::FETCH_OBJ, creating the object
variable names as they are accessed
+ $stmt->execute();
+ $tmp = $stmt->fetchAll(PDO::FETCH_LAZY);
+
+} catch (PDOException $e) {
+ // we should never get here, we use warnings, but never trust a system...
+ printf("[001] %s, [%s} %s\n",
+ $e->getMessage(), $db->errorInfo(), implode(' ', $db->errorInfo()));
+}
+print "done!";
+?>
+--EXPECTF--
+array(2) {
+ [0]=>
+ array(2) {
+ ["id"]=>
+ string(1) "1"
+ [0]=>
+ string(1) "1"
+ }
+ [1]=>
+ array(2) {
+ ["id"]=>
+ string(1) "2"
+ [0]=>
+ string(1) "2"
+ }
+}
+
+Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: PDO::FETCH_LAZY can't
be used with PDOStatement::fetchAll() in %s on line %d
+done!
\ No newline at end of file
Added: trunk/tests/ext/pdo/pdo_stmt_fetchall_group.phpt
===================================================================
--- trunk/tests/ext/pdo/pdo_stmt_fetchall_group.phpt (rev 0)
+++ trunk/tests/ext/pdo/pdo_stmt_fetchall_group.phpt 2008-03-07 13:23:41 UTC (rev 1323)
@@ -0,0 +1,274 @@
+--TEST--
+PDO Common: PDOStatement->fetchAll() / PDO::FETCH_GROUP
+--SKIPIF--
+<?php # vim:ft=php
+if (!extension_loaded('pdo')) die('skip');
+$dir = getenv('REDIR_TEST_DIR');
+if (false == $dir) die('skip no driver');
+require_once $dir . 'pdo_test.inc';
+PDOTest::skip();
+?>
+--FILE--
+<?php
+if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) .
'/../../pdo/tests/');
+require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
+$db = PDOTest::factory();
+
+$db->exec('CREATE TABLE test(id int NOT NULL PRIMARY KEY,grp VARCHAR(10), val
VARCHAR(10))');
+$db->exec('INSERT INTO test(id, grp, val) VALUES(5, "A", "5-A")');
+$db->exec('INSERT INTO test(id, grp, val) VALUES(6, "A", "6-A")');
+$db->exec('INSERT INTO test(id, grp, val) VALUES(7, "B", "7-B")');
+
+try {
+
+ // default settings
+ $query = 'SELECT id, grp, val FROM test ORDER by ID';
+ $stmt = $db->prepare($query);
+ $stmt->execute();
+ $expected = $stmt->fetchAll(PDO::FETCH_ASSOC);
+ var_dump($expected);
+
+ $stmt->execute();
+ printf("fetchAll(PDO::FETCH_GROUP)\n");
+ var_dump($tmp = $stmt->fetchAll(PDO::FETCH_GROUP));
+
+ $stmt->execute();
+ // TODO: the rule seems to be: if no column index is given,
+ // use column index = 1 for the values and column_index = 0 for FETCH_GROUP
+ // However, this is not documented!
+ printf("fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP)\n");
+ var_dump($tmp = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP));
+
+ $stmt->execute();
+ printf("fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, 0)\n");
+ var_dump($tmp = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, 0));
+
+ $stmt->execute();
+ printf("fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, 1)\n");
+ var_dump($tmp = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, 1));
+
+ // TODO: there seems to be an int overflow somewhere
+ $stmt->execute();
+ printf("fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, -1)\n");
+ var_dump($tmp = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, -1));
+
+ // TODO: see about about the column index default used
+ $stmt->execute();
+ printf("fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE)\n");
+ var_dump($tmp = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE));
+
+ $stmt->execute();
+ printf("fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, 0)\n");
+ var_dump($tmp = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, 0));
+
+ $stmt->execute();
+ printf("fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, 1)\n");
+ var_dump($tmp = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, 1));
+
+ $stmt->execute();
+ printf("fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, PHP_INT_MAX + 1)\n");
+ var_dump($tmp = $stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, PHP_INT_MAX +
1));
+
+} catch (PDOException $e) {
+ // we should never get here, we use warnings, but never trust a system...
+ printf("[001] %s, [%s} %s\n",
+ $e->getMessage(), $db->errorInfo(), implode(' ', $db->errorInfo()));
+}
+print "done!";
+?>
+--EXPECTF--
+array(3) {
+ [0]=>
+ array(3) {
+ ["id"]=>
+ string(1) "5"
+ ["grp"]=>
+ string(1) "A"
+ ["val"]=>
+ string(3) "5-A"
+ }
+ [1]=>
+ array(3) {
+ ["id"]=>
+ string(1) "6"
+ ["grp"]=>
+ string(1) "A"
+ ["val"]=>
+ string(3) "6-A"
+ }
+ [2]=>
+ array(3) {
+ ["id"]=>
+ string(1) "7"
+ ["grp"]=>
+ string(1) "B"
+ ["val"]=>
+ string(3) "7-B"
+ }
+}
+fetchAll(PDO::FETCH_GROUP)
+array(3) {
+ [5]=>
+ array(1) {
+ [0]=>
+ array(4) {
+ ["grp"]=>
+ string(1) "A"
+ [0]=>
+ string(1) "A"
+ ["val"]=>
+ string(3) "5-A"
+ [1]=>
+ string(3) "5-A"
+ }
+ }
+ [6]=>
+ array(1) {
+ [0]=>
+ array(4) {
+ ["grp"]=>
+ string(1) "A"
+ [0]=>
+ string(1) "A"
+ ["val"]=>
+ string(3) "6-A"
+ [1]=>
+ string(3) "6-A"
+ }
+ }
+ [7]=>
+ array(1) {
+ [0]=>
+ array(4) {
+ ["grp"]=>
+ string(1) "B"
+ [0]=>
+ string(1) "B"
+ ["val"]=>
+ string(3) "7-B"
+ [1]=>
+ string(3) "7-B"
+ }
+ }
+}
+fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP)
+array(3) {
+ [5]=>
+ array(1) {
+ [0]=>
+ string(1) "A"
+ }
+ [6]=>
+ array(1) {
+ [0]=>
+ string(1) "A"
+ }
+ [7]=>
+ array(1) {
+ [0]=>
+ string(1) "B"
+ }
+}
+fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, 0)
+array(3) {
+ [5]=>
+ array(1) {
+ [0]=>
+ string(1) "5"
+ }
+ [6]=>
+ array(1) {
+ [0]=>
+ string(1) "6"
+ }
+ [7]=>
+ array(1) {
+ [0]=>
+ string(1) "7"
+ }
+}
+fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, 1)
+array(2) {
+ ["A"]=>
+ array(2) {
+ [0]=>
+ string(1) "5"
+ [1]=>
+ string(1) "6"
+ }
+ ["B"]=>
+ array(1) {
+ [0]=>
+ string(1) "7"
+ }
+}
+fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP, -1)
+array(3) {
+ [5]=>
+ array(1) {
+ [0]=>
+ string(1) "A"
+ }
+ [6]=>
+ array(1) {
+ [0]=>
+ string(1) "A"
+ }
+ [7]=>
+ array(1) {
+ [0]=>
+ string(1) "B"
+ }
+}
+fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE)
+array(3) {
+ [5]=>
+ string(1) "A"
+ [6]=>
+ string(1) "A"
+ [7]=>
+ string(1) "B"
+}
+fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, 0)
+array(3) {
+ [5]=>
+ string(1) "5"
+ [6]=>
+ string(1) "6"
+ [7]=>
+ string(1) "7"
+}
+fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, 1)
+array(2) {
+ ["A"]=>
+ array(2) {
+ [0]=>
+ string(1) "5"
+ [1]=>
+ string(1) "6"
+ }
+ ["B"]=>
+ array(1) {
+ [0]=>
+ string(1) "7"
+ }
+}
+fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, PHP_INT_MAX + 1)
+array(3) {
+ [5]=>
+ array(1) {
+ [0]=>
+ string(1) "A"
+ }
+ [6]=>
+ array(1) {
+ [0]=>
+ string(1) "A"
+ }
+ [7]=>
+ array(1) {
+ [0]=>
+ string(1) "B"
+ }
+}
+done!
\ No newline at end of file
| Thread |
|---|
| • PHP mysqlnd svn commit: r1323 - trunk/tests/ext/pdo | uwendel | 7 Mar |