Author: uwendel
Date: 2008-03-05 16:37:14 +0100 (Wed, 05 Mar 2008)
New Revision: 1321
Added:
trunk/tests/ext/pdo/pdo_stmt_fetch_class.phpt
Log:
PDO::FETCH_CLASS - private issues reported as a bug on php.net
Added: trunk/tests/ext/pdo/pdo_stmt_fetch_class.phpt
===================================================================
--- trunk/tests/ext/pdo/pdo_stmt_fetch_class.phpt (rev 0)
+++ trunk/tests/ext/pdo/pdo_stmt_fetch_class.phpt 2008-03-05 15:37:14 UTC (rev 1321)
@@ -0,0 +1,246 @@
+--TEST--
+PDO Common: PDOStatement->fetch() / PDO::FETCH_CLASS
+--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\')');
+
+function fetchAndCheck(&$stmt, $class, $expected, $offset) {
+
+ $index = 0;
+ $stmt->execute();
+ while (is_object($row = $stmt->fetch())) {
+ if (get_class($row) !== $class) {
+ printf("[%03d - 1] Expecting object of type %s got %s\n",
+ $offset, $class, get_class($row));
+ return false;
+ }
+
+ if ($row->id !== $expected[$index]['id']) {
+ printf("[%03d - 2] Expecting id = %s got %s\n",
+ $offset, $expected[$index]['id'], $row->id);
+ return false;
+ }
+
+ if ($row->val !== $expected[$index]['val']) {
+ printf("[%03d - 2] Expecting val = %s got %s\n",
+ $offset, $expected[$index]['val'], $row->val);
+ return false;
+ }
+
+ if ($row->grp !== $expected[$index]['grp']) {
+ printf("[%03d - 2] Expecting id = %s got %s\n",
+ $offset, $expected[$index]['grp'], $row->grp);
+ return false;
+ }
+ $index++;
+ }
+
+ return true;
+}
+
+try {
+
+ $stmt = $db->prepare('SELECT id, val, grp FROM test ORDER BY id');
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'stdClass'))
+ printf("[002] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+
+ $expected = array();
+ $stmt->execute();
+ while (is_object($row = $stmt->fetch())) {
+ if (get_class($row) !== 'stdClass')
+ printf("[003] Expecting object of type stdClass got %s\n", get_class($row));
+
+ $expected[] = array('id' => $row->id, 'val' => $row->val, 'grp' =>
$row->grp);
+ }
+ if (false !== $row)
+ printf("[004] Expecting false got %s\n", var_export($row, true));
+
+ class myclass {}
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass'))
+ printf("[005] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ fetchAndCheck($stmt, 'myclass', $expected, 6);
+
+ class myclass_with_public_properties {
+ public $id;
+ public $val;
+ public $grp;
+ }
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass_with_public_properties'))
+ printf("[007] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ fetchAndCheck($stmt, 'myclass_with_public_properties', $expected, 8);
+
+ class myclass_with_protected_properties {
+ protected $id;
+ protected $val;
+ protected $grp;
+ public function getId() {
+ return $this->id;
+ }
+ }
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass_with_protected_properties'))
+ printf("[009] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ $stmt->execute();
+ $row = $stmt->fetch();
+ var_dump($row->getId());
+
+ class myclass_with_private_properties {
+ private $id;
+ private $val;
+ private $grp;
+ public function getId() {
+ return $this->id;
+ }
+ }
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass_with_private_properties'))
+ printf("[010] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ $stmt->execute();
+ $row = $stmt->fetch();
+ var_dump($row->getId());
+
+ class myclass_with_constructor {
+ public static $calls = 0;
+ public function __construct() {
+ printf("myclass_with_constructor: %d\n", self::$calls++);
+ }
+ }
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass_with_constructor'))
+ printf("[011] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ fetchAndCheck($stmt, 'myclass_with_constructor', $expected, 12);
+
+ class myclass_with_constructor_params {
+ public static $calls = 0;
+ public function __construct($param) {
+ printf("myclass_with_constructor_params: %d / %d\n", self::$calls++, $param);
+ }
+ }
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass_with_constructor_params',
array(1)))
+ printf("[013] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ fetchAndCheck($stmt, 'myclass_with_constructor_params', $expected, 14);
+
+ class myclass_with_constructor_params_missing {
+ public static $calls = 0;
+ public function __construct($param) {
+ printf("myclass_with_constructor_params_missing: %d\n", self::$calls++);
+ }
+ }
+
+ function __autoload($class) {
+ printf("__autoload(%s)\n", $class);
+ }
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS,
'myclass_with_constructor_params_missing'))
+ printf("[015] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ fetchAndCheck($stmt, 'myclass_with_constructor_params_missing', $expected, 16);
+
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass_does_not_exist'))
+ printf("[017] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ $stmt->execute();
+ var_dump($stmt->fetch());
+
+ class myclass_with_private_constructor {
+ public static $calls = 0;
+ private function __construct() {
+ printf("myclass_with_private_constructor: %d\n", self::$calls++);
+ }
+ }
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass_with_private_constructor'))
+ printf("[018] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ fetchAndCheck($stmt, 'myclass_with_private_constructor', $expected, 19);
+
+ class myclass_derived extends myclass {
+ }
+ if (!$stmt->setFetchMode(PDO::FETCH_CLASS, 'myclass_derived'))
+ printf("[020] [%d] %s\n", $stmt->errorCode(), var_export($stmt->errorInfo(),
true));
+ fetchAndCheck($stmt, 'myclass_derived', $expected, 21);
+
+
+} 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!";
+?>
+--BUGFREE_EXPECTF--
+string(1) "1"
+string(1) "1"
+myclass_with_constructor: 0
+myclass_with_constructor: 1
+myclass_with_constructor_params: 0 / 1
+myclass_with_constructor_params: 1 / 1
+
+Warning: Missing argument 1 for myclass_with_constructor_params_missing::__construct() in
%s on line %d
+myclass_with_constructor_params_missing: 0
+
+Warning: Missing argument 1 for myclass_with_constructor_params_missing::__construct() in
%s on line %d
+myclass_with_constructor_params_missing: 1
+__autoload(myclass_does_not_exist)
+[017] [0] array (
+ 0 => '00000',
+ 1 => '',
+ 2 => ''
+)
+array(6) {
+ ["id"]=>
+ string(1) "1"
+ [0]=>
+ string(1) "1"
+ ["val"]=>
+ string(1) "A"
+ [1]=>
+ string(1) "A"
+ ["grp"]=>
+ string(6) "Group1"
+ [2]=>
+ string(6) "Group1"
+}
+myclass_with_private_constructor: 0
+myclass_with_private_constructor: 1
+done!
+--EXPECTF--
+string(1) "1"
+string(1) "1"
+myclass_with_constructor: 0
+myclass_with_constructor: 1
+myclass_with_constructor_params: 0 / 1
+myclass_with_constructor_params: 1 / 1
+
+Warning: Missing argument 1 for myclass_with_constructor_params_missing::__construct() in
%s on line %d
+myclass_with_constructor_params_missing: 0
+
+Warning: Missing argument 1 for myclass_with_constructor_params_missing::__construct() in
%s on line %d
+myclass_with_constructor_params_missing: 1
+__autoload(myclass_does_not_exist)
+[017] [0] array (
+ 0 => '00000',
+)
+array(6) {
+ ["id"]=>
+ string(1) "1"
+ [0]=>
+ string(1) "1"
+ ["val"]=>
+ string(1) "A"
+ [1]=>
+ string(1) "A"
+ ["grp"]=>
+ string(6) "Group1"
+ [2]=>
+ string(6) "Group1"
+}
+myclass_with_private_constructor: 0
+myclass_with_private_constructor: 1
+done!
\ No newline at end of file
| Thread |
|---|
| • PHP mysqlnd svn commit: r1321 - trunk/tests/ext/pdo | uwendel | 5 Mar |