mirror of
https://github.com/php/php-src.git
synced 2026-03-24 00:02:20 +01:00
ext/pdo: Improve testing for FETCH_FUNC mode (#17325)
This commit is contained in:
committed by
GitHub
parent
b7c76ded31
commit
60252b7845
@@ -1,305 +0,0 @@
|
||||
--TEST--
|
||||
PDO Common: PDO::FETCH_FUNC and statement overloading
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$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='.__DIR__ . '/../../pdo/tests/');
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
|
||||
$db->exec('CREATE TABLE test011(id int NOT NULL PRIMARY KEY, val VARCHAR(10), grp VARCHAR(10))');
|
||||
$db->exec("INSERT INTO test011 VALUES(1, 'A', 'Group1')");
|
||||
$db->exec("INSERT INTO test011 VALUES(2, 'B', 'Group1')");
|
||||
$db->exec("INSERT INTO test011 VALUES(3, 'C', 'Group2')");
|
||||
$db->exec("INSERT INTO test011 VALUES(4, 'D', 'Group2')");
|
||||
|
||||
class DerivedStatement extends PDOStatement
|
||||
{
|
||||
private function __construct(public $name, $db)
|
||||
{
|
||||
echo __METHOD__ . "($name)\n";
|
||||
}
|
||||
|
||||
function reTrieve($id, $val) {
|
||||
echo __METHOD__ . "($id,$val)\n";
|
||||
return array($id=>$val);
|
||||
}
|
||||
}
|
||||
|
||||
$select1 = $db->prepare('SELECT grp, id FROM test011');
|
||||
$select2 = $db->prepare('SELECT id, val FROM test011');
|
||||
$derived = $db->prepare('SELECT id, val FROM test011', array(PDO::ATTR_STATEMENT_CLASS=>array('DerivedStatement', array('Overloaded', $db))));
|
||||
|
||||
class Test1
|
||||
{
|
||||
public function __construct(public $id, public $val)
|
||||
{
|
||||
echo __METHOD__ . "($id,$val)\n";
|
||||
}
|
||||
|
||||
static public function factory($id, $val)
|
||||
{
|
||||
echo __METHOD__ . "($id,$val)\n";
|
||||
return new self($id, $val);
|
||||
}
|
||||
}
|
||||
|
||||
function callback($id,$val='N/A')
|
||||
{
|
||||
echo __METHOD__ . "($id,$val)\n";
|
||||
return array($id=>$val);
|
||||
}
|
||||
|
||||
$f = new Test1(0,0);
|
||||
|
||||
$select1->execute();
|
||||
var_dump($select1->fetchAll(PDO::FETCH_FUNC|PDO::FETCH_GROUP, 'callback'));
|
||||
|
||||
$select2->execute();
|
||||
var_dump($select2->fetchAll(PDO::FETCH_FUNC, 'callback'));
|
||||
|
||||
$select2->execute();
|
||||
var_dump($select2->fetchAll(PDO::FETCH_FUNC, array('Test1','factory')));
|
||||
|
||||
$select2->execute();
|
||||
var_dump($select2->fetchAll(PDO::FETCH_FUNC, array($f, 'factory')));
|
||||
|
||||
var_dump(get_class($derived));
|
||||
$derived->execute();
|
||||
var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'retrieve')));
|
||||
$derived->execute();
|
||||
var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'reTrieve')));
|
||||
$derived->execute();
|
||||
var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'RETRIEVE')));
|
||||
|
||||
?>
|
||||
--CLEAN--
|
||||
<?php
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
PDOTest::dropTableIfExists($db, "test011");
|
||||
?>
|
||||
--EXPECTF--
|
||||
DerivedStatement::__construct(Overloaded)
|
||||
Test1::__construct(0,0)
|
||||
callback(1,N/A)
|
||||
callback(2,N/A)
|
||||
callback(3,N/A)
|
||||
callback(4,N/A)
|
||||
array(2) {
|
||||
["Group1"]=>
|
||||
array(2) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[1]=>
|
||||
string(3) "N/A"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[2]=>
|
||||
string(3) "N/A"
|
||||
}
|
||||
}
|
||||
["Group2"]=>
|
||||
array(2) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[3]=>
|
||||
string(3) "N/A"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[4]=>
|
||||
string(3) "N/A"
|
||||
}
|
||||
}
|
||||
}
|
||||
callback(1,A)
|
||||
callback(2,B)
|
||||
callback(3,C)
|
||||
callback(4,D)
|
||||
array(4) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[1]=>
|
||||
string(1) "A"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[2]=>
|
||||
string(1) "B"
|
||||
}
|
||||
[2]=>
|
||||
array(1) {
|
||||
[3]=>
|
||||
string(1) "C"
|
||||
}
|
||||
[3]=>
|
||||
array(1) {
|
||||
[4]=>
|
||||
string(1) "D"
|
||||
}
|
||||
}
|
||||
Test1::factory(1,A)
|
||||
Test1::__construct(1,A)
|
||||
Test1::factory(2,B)
|
||||
Test1::__construct(2,B)
|
||||
Test1::factory(3,C)
|
||||
Test1::__construct(3,C)
|
||||
Test1::factory(4,D)
|
||||
Test1::__construct(4,D)
|
||||
array(4) {
|
||||
[0]=>
|
||||
object(Test1)#%d (2) {
|
||||
["id"]=>
|
||||
string(1) "1"
|
||||
["val"]=>
|
||||
string(1) "A"
|
||||
}
|
||||
[1]=>
|
||||
object(Test1)#%d (2) {
|
||||
["id"]=>
|
||||
string(1) "2"
|
||||
["val"]=>
|
||||
string(1) "B"
|
||||
}
|
||||
[2]=>
|
||||
object(Test1)#%d (2) {
|
||||
["id"]=>
|
||||
string(1) "3"
|
||||
["val"]=>
|
||||
string(1) "C"
|
||||
}
|
||||
[3]=>
|
||||
object(Test1)#%d (2) {
|
||||
["id"]=>
|
||||
string(1) "4"
|
||||
["val"]=>
|
||||
string(1) "D"
|
||||
}
|
||||
}
|
||||
Test1::factory(1,A)
|
||||
Test1::__construct(1,A)
|
||||
Test1::factory(2,B)
|
||||
Test1::__construct(2,B)
|
||||
Test1::factory(3,C)
|
||||
Test1::__construct(3,C)
|
||||
Test1::factory(4,D)
|
||||
Test1::__construct(4,D)
|
||||
array(4) {
|
||||
[0]=>
|
||||
object(Test1)#%d (2) {
|
||||
["id"]=>
|
||||
string(1) "1"
|
||||
["val"]=>
|
||||
string(1) "A"
|
||||
}
|
||||
[1]=>
|
||||
object(Test1)#%d (2) {
|
||||
["id"]=>
|
||||
string(1) "2"
|
||||
["val"]=>
|
||||
string(1) "B"
|
||||
}
|
||||
[2]=>
|
||||
object(Test1)#%d (2) {
|
||||
["id"]=>
|
||||
string(1) "3"
|
||||
["val"]=>
|
||||
string(1) "C"
|
||||
}
|
||||
[3]=>
|
||||
object(Test1)#%d (2) {
|
||||
["id"]=>
|
||||
string(1) "4"
|
||||
["val"]=>
|
||||
string(1) "D"
|
||||
}
|
||||
}
|
||||
string(16) "DerivedStatement"
|
||||
DerivedStatement::reTrieve(1,A)
|
||||
DerivedStatement::reTrieve(2,B)
|
||||
DerivedStatement::reTrieve(3,C)
|
||||
DerivedStatement::reTrieve(4,D)
|
||||
array(4) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[1]=>
|
||||
string(1) "A"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[2]=>
|
||||
string(1) "B"
|
||||
}
|
||||
[2]=>
|
||||
array(1) {
|
||||
[3]=>
|
||||
string(1) "C"
|
||||
}
|
||||
[3]=>
|
||||
array(1) {
|
||||
[4]=>
|
||||
string(1) "D"
|
||||
}
|
||||
}
|
||||
DerivedStatement::reTrieve(1,A)
|
||||
DerivedStatement::reTrieve(2,B)
|
||||
DerivedStatement::reTrieve(3,C)
|
||||
DerivedStatement::reTrieve(4,D)
|
||||
array(4) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[1]=>
|
||||
string(1) "A"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[2]=>
|
||||
string(1) "B"
|
||||
}
|
||||
[2]=>
|
||||
array(1) {
|
||||
[3]=>
|
||||
string(1) "C"
|
||||
}
|
||||
[3]=>
|
||||
array(1) {
|
||||
[4]=>
|
||||
string(1) "D"
|
||||
}
|
||||
}
|
||||
DerivedStatement::reTrieve(1,A)
|
||||
DerivedStatement::reTrieve(2,B)
|
||||
DerivedStatement::reTrieve(3,C)
|
||||
DerivedStatement::reTrieve(4,D)
|
||||
array(4) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[1]=>
|
||||
string(1) "A"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[2]=>
|
||||
string(1) "B"
|
||||
}
|
||||
[2]=>
|
||||
array(1) {
|
||||
[3]=>
|
||||
string(1) "C"
|
||||
}
|
||||
[3]=>
|
||||
array(1) {
|
||||
[4]=>
|
||||
string(1) "D"
|
||||
}
|
||||
}
|
||||
114
ext/pdo/tests/pdo_fetch_func_001.phpt
Normal file
114
ext/pdo/tests/pdo_fetch_func_001.phpt
Normal file
@@ -0,0 +1,114 @@
|
||||
--TEST--
|
||||
PDO Common: Testing PDO::FETCH_FUNC with various callables
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$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='.__DIR__ . '/../../pdo/tests/');
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
|
||||
$db->exec('CREATE TABLE pdo_fetch_function_001(id int NOT NULL PRIMARY KEY, pl_name VARCHAR(10))');
|
||||
$db->exec("INSERT INTO pdo_fetch_function_001 VALUES (1, 'php')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_001 VALUES (2, 'sql')");
|
||||
|
||||
echo "Anonymous function:\n";
|
||||
$st = $db->query('SELECT * FROM pdo_fetch_function_001');
|
||||
$st->fetchAll(
|
||||
PDO::FETCH_FUNC,
|
||||
function($x, $y) {
|
||||
echo $x, ' ', $y, PHP_EOL;
|
||||
}
|
||||
);
|
||||
|
||||
echo "Internal function:\n";
|
||||
$st = $db->query('SELECT pl_name FROM pdo_fetch_function_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, 'strtoupper'));
|
||||
|
||||
echo "Relative class callable:\n";
|
||||
class foo {
|
||||
public function method($x) {
|
||||
return __METHOD__ . "($x)";
|
||||
}
|
||||
}
|
||||
class bar extends foo {
|
||||
public function __construct($db) {
|
||||
$st = $db->query('SELECT * FROM pdo_fetch_function_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, [$this, 'parent::method']));
|
||||
}
|
||||
|
||||
static public function factory($x, $y) {
|
||||
return __METHOD__ . "($x, $y)";
|
||||
}
|
||||
}
|
||||
|
||||
$bar = new bar($db);
|
||||
|
||||
echo '["bar", "factory"] callable:', "\n";
|
||||
$st = $db->query('SELECT * FROM pdo_fetch_function_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, ['bar', 'factory']));
|
||||
|
||||
echo '[$bar, "factory"] callable:', "\n";
|
||||
$st = $db->query('SELECT * FROM pdo_fetch_function_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, [$bar, 'factory']));
|
||||
|
||||
echo '"bar::factory" callable:', "\n";
|
||||
$st = $db->query('SELECT * FROM pdo_fetch_function_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, 'bar::factory'));
|
||||
|
||||
?>
|
||||
--CLEAN--
|
||||
<?php
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
PDOTest::dropTableIfExists($db, "pdo_fetch_function_001");
|
||||
?>
|
||||
--EXPECTF--
|
||||
Anonymous function:
|
||||
1 php
|
||||
2 sql
|
||||
Internal function:
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(3) "PHP"
|
||||
[1]=>
|
||||
string(3) "SQL"
|
||||
}
|
||||
Relative class callable:
|
||||
|
||||
Deprecated: Callables of the form ["bar", "parent::method"] are deprecated in %s on line %d
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(14) "foo::method(1)"
|
||||
[1]=>
|
||||
string(14) "foo::method(2)"
|
||||
}
|
||||
["bar", "factory"] callable:
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(20) "bar::factory(1, php)"
|
||||
[1]=>
|
||||
string(20) "bar::factory(2, sql)"
|
||||
}
|
||||
[$bar, "factory"] callable:
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(20) "bar::factory(1, php)"
|
||||
[1]=>
|
||||
string(20) "bar::factory(2, sql)"
|
||||
}
|
||||
"bar::factory" callable:
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(20) "bar::factory(1, php)"
|
||||
[1]=>
|
||||
string(20) "bar::factory(2, sql)"
|
||||
}
|
||||
99
ext/pdo/tests/pdo_fetch_function_basic.phpt
Normal file
99
ext/pdo/tests/pdo_fetch_function_basic.phpt
Normal file
@@ -0,0 +1,99 @@
|
||||
--TEST--
|
||||
PDO Common: PDO::FETCH_FUNC with a simple callback
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$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='.__DIR__ . '/../../pdo/tests/');
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
|
||||
$db->exec('CREATE TABLE pdo_fetch_function_basic(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10))');
|
||||
$db->exec("INSERT INTO pdo_fetch_function_basic VALUES (1, 'A', 'alpha')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_basic VALUES (2, 'B', 'beta')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_basic VALUES (3, 'C', 'gamma')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_basic VALUES (4, 'D', 'delta')");
|
||||
|
||||
echo "SELECT id, val1:\n";
|
||||
|
||||
function associateValWithId(int $id, string $val): array {
|
||||
echo __FUNCTION__, '(', var_export($id, true), ', ', var_export($val, true) , ")\n";
|
||||
return [$id=>$val];
|
||||
}
|
||||
|
||||
$selectIdVal = $db->prepare('SELECT id, val1 FROM pdo_fetch_function_basic');
|
||||
$selectIdVal->execute();
|
||||
$result = $selectIdVal->fetchAll(PDO::FETCH_FUNC, 'associateValWithId');
|
||||
var_dump($result);
|
||||
|
||||
echo "SELECT *:\n";
|
||||
|
||||
function selectAllCallback(int $id, string $val1, string $val2): string {
|
||||
echo __FUNCTION__, '(', var_export($id, true),
|
||||
', ', var_export($val1, true),
|
||||
', ', var_export($val2, true) , ")\n";
|
||||
return $val1 . $val2;
|
||||
}
|
||||
|
||||
$selectAll = $db->prepare('SELECT * FROM pdo_fetch_function_basic');
|
||||
$selectAll->execute();
|
||||
$result = $selectAll->fetchAll(PDO::FETCH_FUNC, 'selectAllCallback');
|
||||
var_dump($result);
|
||||
|
||||
?>
|
||||
--CLEAN--
|
||||
<?php
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
PDOTest::dropTableIfExists($db, "pdo_fetch_function_basic");
|
||||
?>
|
||||
--EXPECT--
|
||||
SELECT id, val1:
|
||||
associateValWithId(1, 'A')
|
||||
associateValWithId(2, 'B')
|
||||
associateValWithId(3, 'C')
|
||||
associateValWithId(4, 'D')
|
||||
array(4) {
|
||||
[0]=>
|
||||
array(1) {
|
||||
[1]=>
|
||||
string(1) "A"
|
||||
}
|
||||
[1]=>
|
||||
array(1) {
|
||||
[2]=>
|
||||
string(1) "B"
|
||||
}
|
||||
[2]=>
|
||||
array(1) {
|
||||
[3]=>
|
||||
string(1) "C"
|
||||
}
|
||||
[3]=>
|
||||
array(1) {
|
||||
[4]=>
|
||||
string(1) "D"
|
||||
}
|
||||
}
|
||||
SELECT *:
|
||||
selectAllCallback(1, 'A', 'alpha')
|
||||
selectAllCallback(2, 'B', 'beta')
|
||||
selectAllCallback(3, 'C', 'gamma')
|
||||
selectAllCallback(4, 'D', 'delta')
|
||||
array(4) {
|
||||
[0]=>
|
||||
string(6) "Aalpha"
|
||||
[1]=>
|
||||
string(5) "Bbeta"
|
||||
[2]=>
|
||||
string(6) "Cgamma"
|
||||
[3]=>
|
||||
string(6) "Ddelta"
|
||||
}
|
||||
50
ext/pdo/tests/pdo_fetch_function_incorrect_call.phpt
Normal file
50
ext/pdo/tests/pdo_fetch_function_incorrect_call.phpt
Normal file
@@ -0,0 +1,50 @@
|
||||
--TEST--
|
||||
PDO Common: PDO::FETCH_FUNC with a call that is invalid
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$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='.__DIR__ . '/../../pdo/tests/');
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
|
||||
$db->exec('CREATE TABLE pdo_fetch_function_incorrect_call(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10))');
|
||||
$db->exec("INSERT INTO pdo_fetch_function_incorrect_call VALUES(1, 'A', 'alpha')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_incorrect_call VALUES(2, 'B', 'beta')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_incorrect_call VALUES(3, 'C', 'gamma')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_incorrect_call VALUES(4, 'D', 'delta')");
|
||||
|
||||
$selectIdVal = $db->prepare('SELECT val1, val2 FROM pdo_fetch_function_incorrect_call');
|
||||
|
||||
function bogusCallback(stdClass $obj, string $str, array $arr) {
|
||||
echo "Called\n";
|
||||
return "how?";
|
||||
}
|
||||
|
||||
$selectIdVal->execute();
|
||||
|
||||
echo "Fetch all with bogus call:\n";
|
||||
try {
|
||||
$result = $selectIdVal->fetchAll(PDO::FETCH_FUNC, 'bogusCallback');
|
||||
var_dump($result);
|
||||
} catch (Throwable $e) {
|
||||
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--CLEAN--
|
||||
<?php
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
PDOTest::dropTableIfExists($db, "pdo_fetch_function_incorrect_call");
|
||||
?>
|
||||
--EXPECT--
|
||||
Fetch all with bogus call:
|
||||
TypeError: bogusCallback(): Argument #1 ($obj) must be of type stdClass, string given
|
||||
96
ext/pdo/tests/pdo_fetch_function_incorrect_callables.phpt
Normal file
96
ext/pdo/tests/pdo_fetch_function_incorrect_callables.phpt
Normal file
@@ -0,0 +1,96 @@
|
||||
--TEST--
|
||||
PDO Common: PDO::FETCH_FUNC with invalid callables
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$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='.__DIR__ . '/../../pdo/tests/');
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
|
||||
$db->exec('CREATE TABLE pdo_fetch_function_incorrect_callable(id int NOT NULL PRIMARY KEY, pl_name VARCHAR(10))');
|
||||
$db->exec("INSERT INTO pdo_fetch_function_incorrect_callable VALUES (1, 'php')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_incorrect_callable VALUES (2, 'sql')");
|
||||
$stmt = $db->query('SELECT * FROM pdo_fetch_function_incorrect_callable');
|
||||
|
||||
class Bar {
|
||||
static private function privateStatic($x, $y) {
|
||||
return $x;
|
||||
}
|
||||
|
||||
public function instanceMethod($x, $y) {
|
||||
return $x .'==='. $y;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
var_dump($stmt->fetchAll(PDO::FETCH_FUNC, 'nothing'));
|
||||
} catch (\Throwable $e) {
|
||||
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
var_dump($stmt->fetchAll(PDO::FETCH_FUNC, ''));
|
||||
} catch (\Throwable $e) {
|
||||
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
var_dump($stmt->fetchAll(PDO::FETCH_FUNC, NULL));
|
||||
} catch (\Throwable $e) {
|
||||
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
var_dump($stmt->fetchAll(PDO::FETCH_FUNC, 1));
|
||||
} catch (\Throwable $e) {
|
||||
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
var_dump($stmt->fetchAll(PDO::FETCH_FUNC, ['self', 'foo']));
|
||||
} catch (\Throwable $e) {
|
||||
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
var_dump($stmt->fetchAll(PDO::FETCH_FUNC, ['bar', 'instanceMethod']));
|
||||
} catch (\Throwable $e) {
|
||||
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
var_dump($stmt->fetchAll(PDO::FETCH_FUNC, ['bar', 'privateStatic']));
|
||||
} catch (\Throwable $e) {
|
||||
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
var_dump($stmt->fetchAll(PDO::FETCH_FUNC, ['bar', 'nonexistent']));
|
||||
} catch (\Throwable $e) {
|
||||
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--CLEAN--
|
||||
<?php
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
PDOTest::dropTableIfExists($db, "pdo_fetch_function_incorrect_callable");
|
||||
?>
|
||||
--EXPECT--
|
||||
TypeError: function "nothing" not found or invalid function name
|
||||
TypeError: function "" not found or invalid function name
|
||||
TypeError: PDOStatement::fetchAll(): Argument #2 must be a callable, null given
|
||||
TypeError: no array or string given
|
||||
TypeError: cannot access "self" when no class scope is active
|
||||
TypeError: non-static method Bar::instanceMethod() cannot be called statically
|
||||
TypeError: cannot access private method Bar::privateStatic()
|
||||
TypeError: class Bar does not have a method "nonexistent"
|
||||
@@ -0,0 +1,65 @@
|
||||
--TEST--
|
||||
PDO Common: PDO::FETCH_FUNC and statement overloading
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$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='.__DIR__ . '/../../pdo/tests/');
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
|
||||
$db->exec('CREATE TABLE pdo_fetch_function_overload_statement_class(id int NOT NULL PRIMARY KEY, val VARCHAR(10))');
|
||||
$db->exec("INSERT INTO pdo_fetch_function_overload_statement_class VALUES (1, 'A')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_overload_statement_class VALUES (2, 'B')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_overload_statement_class VALUES (3, 'C')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_overload_statement_class VALUES (4, 'D')");
|
||||
|
||||
class DerivedStatement extends PDOStatement
|
||||
{
|
||||
private function __construct(public $name, $db)
|
||||
{
|
||||
echo __METHOD__ . "($name)\n";
|
||||
}
|
||||
|
||||
function reTrieve($id, $val) {
|
||||
echo __METHOD__ . "($id,$val)\n";
|
||||
return "$id => $val";
|
||||
}
|
||||
}
|
||||
|
||||
$derived = $db->prepare('SELECT id, val FROM pdo_fetch_function_overload_statement_class', [PDO::ATTR_STATEMENT_CLASS => ['DerivedStatement', ['Overloaded', $db]]]);
|
||||
var_dump(get_class($derived));
|
||||
$derived->execute();
|
||||
var_dump($derived->fetchAll(PDO::FETCH_FUNC, [$derived, 'retrieve']));
|
||||
|
||||
?>
|
||||
--CLEAN--
|
||||
<?php
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
PDOTest::dropTableIfExists($db, "pdo_fetch_function_overload_statement_class");
|
||||
?>
|
||||
--EXPECT--
|
||||
DerivedStatement::__construct(Overloaded)
|
||||
string(16) "DerivedStatement"
|
||||
DerivedStatement::reTrieve(1,A)
|
||||
DerivedStatement::reTrieve(2,B)
|
||||
DerivedStatement::reTrieve(3,C)
|
||||
DerivedStatement::reTrieve(4,D)
|
||||
array(4) {
|
||||
[0]=>
|
||||
string(6) "1 => A"
|
||||
[1]=>
|
||||
string(6) "2 => B"
|
||||
[2]=>
|
||||
string(6) "3 => C"
|
||||
[3]=>
|
||||
string(6) "4 => D"
|
||||
}
|
||||
63
ext/pdo/tests/pdo_fetch_function_with_grouping.phpt
Normal file
63
ext/pdo/tests/pdo_fetch_function_with_grouping.phpt
Normal file
@@ -0,0 +1,63 @@
|
||||
--TEST--
|
||||
PDO Common: PDO::FETCH_FUNC with a simple callback and grouping (PDO::FETCH_FUNC|PDO::FETCH_GROUP)
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$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='.__DIR__ . '/../../pdo/tests/');
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
|
||||
$db->exec('CREATE TABLE pdo_fetch_function_with_grouping(id int NOT NULL PRIMARY KEY, val VARCHAR(10), grp VARCHAR(10))');
|
||||
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_grouping VALUES (1, 'A', 'Group1')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_grouping VALUES (2, 'B', 'Group1')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_grouping VALUES (3, 'C', 'Group2')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_grouping VALUES (4, 'D', 'Group2')");
|
||||
|
||||
$selectGroupId = $db->prepare('SELECT grp, id, val FROM pdo_fetch_function_with_grouping');
|
||||
|
||||
function associateValWithId(int $id, string $value) {
|
||||
echo __FUNCTION__, '(', var_export($id, true), ', ', var_export($value, true) , ")\n";
|
||||
return "$id => $value";
|
||||
}
|
||||
|
||||
$selectGroupId->execute();
|
||||
$result = $selectGroupId->fetchAll(PDO::FETCH_FUNC|PDO::FETCH_GROUP, 'associateValWithId');
|
||||
var_dump($result);
|
||||
|
||||
?>
|
||||
--CLEAN--
|
||||
<?php
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
PDOTest::dropTableIfExists($db, "pdo_fetch_function_with_grouping");
|
||||
?>
|
||||
--EXPECT--
|
||||
associateValWithId(1, 'A')
|
||||
associateValWithId(2, 'B')
|
||||
associateValWithId(3, 'C')
|
||||
associateValWithId(4, 'D')
|
||||
array(2) {
|
||||
["Group1"]=>
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(6) "1 => A"
|
||||
[1]=>
|
||||
string(6) "2 => B"
|
||||
}
|
||||
["Group2"]=>
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(6) "3 => C"
|
||||
[1]=>
|
||||
string(6) "4 => D"
|
||||
}
|
||||
}
|
||||
55
ext/pdo/tests/pdo_fetch_function_with_unique.phpt
Normal file
55
ext/pdo/tests/pdo_fetch_function_with_unique.phpt
Normal file
@@ -0,0 +1,55 @@
|
||||
--TEST--
|
||||
PDO Common: PDO::FETCH_FUNC with a simple callback and uniqueness (PDO::FETCH_FUNC|PDO::FETCH_UNIQUE)
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$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='.__DIR__ . '/../../pdo/tests/');
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
|
||||
$db->exec('CREATE TABLE pdo_fetch_function_with_uniqueness(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10))');
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_uniqueness VALUES (1, 'A', 'alpha')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_uniqueness VALUES (2, 'B', 'beta')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_uniqueness VALUES (3, 'C', 'gamma')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_uniqueness VALUES (4, 'D', 'delta')");
|
||||
|
||||
function to_upper_with_log(string $str): string {
|
||||
echo __FUNCTION__, '(', var_export($str, true), ')', PHP_EOL;
|
||||
return strtoupper($str);
|
||||
}
|
||||
|
||||
$pdoQuery = $db->prepare('SELECT val1, val2 FROM pdo_fetch_function_with_uniqueness');
|
||||
$pdoQuery->execute();
|
||||
$result = $pdoQuery->fetchAll(PDO::FETCH_FUNC|PDO::FETCH_UNIQUE, 'to_upper_with_log');
|
||||
var_dump($result);
|
||||
|
||||
?>
|
||||
--CLEAN--
|
||||
<?php
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
PDOTest::dropTableIfExists($db, "pdo_fetch_function_with_uniqueness");
|
||||
?>
|
||||
--EXPECT--
|
||||
to_upper_with_log('alpha')
|
||||
to_upper_with_log('beta')
|
||||
to_upper_with_log('gamma')
|
||||
to_upper_with_log('delta')
|
||||
array(4) {
|
||||
["A"]=>
|
||||
string(5) "ALPHA"
|
||||
["B"]=>
|
||||
string(4) "BETA"
|
||||
["C"]=>
|
||||
string(5) "GAMMA"
|
||||
["D"]=>
|
||||
string(5) "DELTA"
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
--TEST--
|
||||
PDO Common: PDO::FETCH_FUNC with a simple callback and uniqueness+grouping (PDO::FETCH_FUNC|PDO::FETCH_UNIQUE|PDO::FETCH_GROUP)
|
||||
--EXTENSIONS--
|
||||
pdo
|
||||
--SKIPIF--
|
||||
<?php
|
||||
$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='.__DIR__ . '/../../pdo/tests/');
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
|
||||
$db->exec('CREATE TABLE pdo_fetch_function_with_uniqueness_and_grouping(id int NOT NULL PRIMARY KEY, val1 VARCHAR(10), val2 VARCHAR(10), grp VARCHAR(10))');
|
||||
// Firebird does not allow inserting multiple rows with INSERT INTO
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_uniqueness_and_grouping VALUES (1, 'A', 'alpha', 'Group1')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_uniqueness_and_grouping VALUES (2, 'B', 'beta', 'Group1')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_uniqueness_and_grouping VALUES (3, 'C', 'gamma', 'Group2')");
|
||||
$db->exec("INSERT INTO pdo_fetch_function_with_uniqueness_and_grouping VALUES (4, 'D', 'delta', 'Group2')");
|
||||
|
||||
function to_upper_with_log(string $str): string {
|
||||
echo __FUNCTION__, '(', var_export($str, true), ')', PHP_EOL;
|
||||
return strtoupper($str);
|
||||
}
|
||||
|
||||
$pdoQuery = $db->prepare('SELECT grp, val2, val1 FROM pdo_fetch_function_with_uniqueness_and_grouping');
|
||||
$pdoQuery->execute();
|
||||
$result = $pdoQuery->fetchAll(PDO::FETCH_FUNC|PDO::FETCH_UNIQUE|PDO::FETCH_GROUP, 'to_upper_with_log');
|
||||
var_dump($result);
|
||||
|
||||
?>
|
||||
--CLEAN--
|
||||
<?php
|
||||
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
|
||||
$db = PDOTest::factory();
|
||||
PDOTest::dropTableIfExists($db, "pdo_fetch_function_with_uniqueness_and_grouping");
|
||||
?>
|
||||
--EXPECT--
|
||||
to_upper_with_log('alpha')
|
||||
to_upper_with_log('beta')
|
||||
to_upper_with_log('gamma')
|
||||
to_upper_with_log('delta')
|
||||
array(2) {
|
||||
["Group1"]=>
|
||||
string(4) "BETA"
|
||||
["Group2"]=>
|
||||
string(5) "DELTA"
|
||||
}
|
||||
@@ -96,4 +96,5 @@ class PDOTest {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -1,151 +0,0 @@
|
||||
--TEST--
|
||||
Testing several callbacks using PDO::FETCH_FUNC
|
||||
--EXTENSIONS--
|
||||
pdo_sqlite
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
$db = new PDO('sqlite::memory:');
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
|
||||
|
||||
$db->exec('CREATE TABLE test_fetch_func_001 (id INTEGER , name VARCHAR)');
|
||||
$db->exec('INSERT INTO test_fetch_func_001 VALUES(1, "php"), (2, "")');
|
||||
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
$st->fetchAll(
|
||||
PDO::FETCH_FUNC,
|
||||
function($x, $y) use ($st) {
|
||||
var_dump($st, $x, $y);
|
||||
}
|
||||
);
|
||||
|
||||
$st = $db->query('SELECT name FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, 'strtoupper'));
|
||||
|
||||
try {
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, 'nothing'));
|
||||
} catch (\TypeError $e) {
|
||||
echo $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, ''));
|
||||
} catch (\TypeError $e) {
|
||||
echo $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, NULL));
|
||||
} catch (\TypeError $e) {
|
||||
echo $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, 1));
|
||||
} catch (\TypeError $e) {
|
||||
echo $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, array('self', 'foo')));
|
||||
} catch (\TypeError $e) {
|
||||
echo $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
class foo {
|
||||
public function method($x) {
|
||||
return "--- $x ---";
|
||||
}
|
||||
}
|
||||
class bar extends foo {
|
||||
public function __construct($db) {
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, array($this, 'parent::method')));
|
||||
}
|
||||
|
||||
static public function test1($x, $y) {
|
||||
return $x .'---'. $y;
|
||||
}
|
||||
|
||||
private function test2($x, $y) {
|
||||
return $x;
|
||||
}
|
||||
|
||||
public function test3($x, $y) {
|
||||
return $x .'==='. $y;
|
||||
}
|
||||
}
|
||||
|
||||
new bar($db);
|
||||
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test1')));
|
||||
|
||||
try {
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test2')));
|
||||
} catch (\TypeError $e) {
|
||||
echo $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test3')));
|
||||
} catch (\TypeError $e) {
|
||||
echo $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
$st = $db->query('SELECT * FROM test_fetch_func_001');
|
||||
var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'inexistent')));
|
||||
} catch (\TypeError $e) {
|
||||
echo $e->getMessage(), \PHP_EOL;
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
object(PDOStatement)#%d (1) {
|
||||
["queryString"]=>
|
||||
string(33) "SELECT * FROM test_fetch_func_001"
|
||||
}
|
||||
int(1)
|
||||
string(3) "php"
|
||||
object(PDOStatement)#%d (1) {
|
||||
["queryString"]=>
|
||||
string(33) "SELECT * FROM test_fetch_func_001"
|
||||
}
|
||||
int(2)
|
||||
string(0) ""
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(3) "PHP"
|
||||
[1]=>
|
||||
string(0) ""
|
||||
}
|
||||
function "nothing" not found or invalid function name
|
||||
function "" not found or invalid function name
|
||||
PDOStatement::fetchAll(): Argument #2 must be a callable, null given
|
||||
no array or string given
|
||||
cannot access "self" when no class scope is active
|
||||
|
||||
Deprecated: Callables of the form ["bar", "parent::method"] are deprecated in %s on line %d
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(9) "--- 1 ---"
|
||||
[1]=>
|
||||
string(9) "--- 2 ---"
|
||||
}
|
||||
array(2) {
|
||||
[0]=>
|
||||
string(7) "1---php"
|
||||
[1]=>
|
||||
string(4) "2---"
|
||||
}
|
||||
non-static method bar::test2() cannot be called statically
|
||||
non-static method bar::test3() cannot be called statically
|
||||
class bar does not have a method "inexistent"
|
||||
Reference in New Issue
Block a user