Skip to content

Commit 571b124

Browse files
committed
ext/pdo: Add tests using keyed class fetch constructor array
1 parent 55cac5b commit 571b124

2 files changed

+167
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
--TEST--
2+
PDO Common: PDO::FETCH_CLASS using named arguments in constructor array
3+
--EXTENSIONS--
4+
pdo
5+
--SKIPIF--
6+
<?php
7+
$dir = getenv('REDIR_TEST_DIR');
8+
if (false == $dir) die('skip no driver');
9+
require_once $dir . 'pdo_test.inc';
10+
PDOTest::skip();
11+
?>
12+
--FILE--
13+
<?php
14+
if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/');
15+
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
16+
$db = PDOTest::factory();
17+
18+
$db->exec('CREATE TABLE pdo_fetch_class_ctor_with_named_arguments(id int NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(10))');
19+
$db->exec("INSERT INTO pdo_fetch_class_ctor_with_named_arguments VALUES(1, 'A', 'AA')");
20+
$db->exec("INSERT INTO pdo_fetch_class_ctor_with_named_arguments VALUES(2, 'B', 'BB')");
21+
$db->exec("INSERT INTO pdo_fetch_class_ctor_with_named_arguments VALUES(3, 'C', 'CC')");
22+
23+
$stmt = $db->prepare('SELECT id, val, val2 from pdo_fetch_class_ctor_with_named_arguments');
24+
25+
class TestBase
26+
{
27+
public $id;
28+
protected $val;
29+
private $val2;
30+
31+
public function __construct(string $a, string $b) {
32+
echo 'Value of $a: ', $a, PHP_EOL,
33+
'Value of $b: ', $b, PHP_EOL;
34+
}
35+
}
36+
$stmt->execute();
37+
var_dump($stmt->fetchAll(PDO::FETCH_CLASS, 'TestBase', ['b' => 'My key is B', 'a' => 'My key is A']));
38+
39+
?>
40+
--CLEAN--
41+
<?php
42+
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
43+
$db = PDOTest::factory();
44+
PDOTest::dropTableIfExists($db, "pdo_fetch_class_ctor_with_named_arguments");
45+
?>
46+
--EXPECTF--
47+
Value of $a: My key is B
48+
Value of $b: My key is A
49+
Value of $a: My key is B
50+
Value of $b: My key is A
51+
Value of $a: My key is B
52+
Value of $b: My key is A
53+
array(3) {
54+
[0]=>
55+
object(TestBase)#%d (3) {
56+
["id"]=>
57+
string(1) "1"
58+
["val":protected]=>
59+
string(1) "A"
60+
["val2":"TestBase":private]=>
61+
string(2) "AA"
62+
}
63+
[1]=>
64+
object(TestBase)#%d (3) {
65+
["id"]=>
66+
string(1) "2"
67+
["val":protected]=>
68+
string(1) "B"
69+
["val2":"TestBase":private]=>
70+
string(2) "BB"
71+
}
72+
[2]=>
73+
object(TestBase)#%d (3) {
74+
["id"]=>
75+
string(1) "3"
76+
["val":protected]=>
77+
string(1) "C"
78+
["val2":"TestBase":private]=>
79+
string(2) "CC"
80+
}
81+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
--TEST--
2+
PDO Common: PDO::FETCH_CLASS using mixed string and int arguments in constructor array
3+
--EXTENSIONS--
4+
pdo
5+
--SKIPIF--
6+
<?php
7+
$dir = getenv('REDIR_TEST_DIR');
8+
if (false == $dir) die('skip no driver');
9+
require_once $dir . 'pdo_test.inc';
10+
PDOTest::skip();
11+
?>
12+
--FILE--
13+
<?php
14+
if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/');
15+
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
16+
$db = PDOTest::factory();
17+
18+
$db->exec('CREATE TABLE pdo_fetch_class_ctor_with_named_arguments_positional_after_named(id int NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(10))');
19+
$db->exec("INSERT INTO pdo_fetch_class_ctor_with_named_arguments_positional_after_named VALUES(1, 'A', 'AA')");
20+
$db->exec("INSERT INTO pdo_fetch_class_ctor_with_named_arguments_positional_after_named VALUES(2, 'B', 'BB')");
21+
$db->exec("INSERT INTO pdo_fetch_class_ctor_with_named_arguments_positional_after_named VALUES(3, 'C', 'CC')");
22+
23+
$stmt = $db->prepare('SELECT id, val, val2 from pdo_fetch_class_ctor_with_named_arguments_positional_after_named');
24+
25+
class TestBase
26+
{
27+
public $id;
28+
protected $val;
29+
private $val2;
30+
31+
public function __construct(string $a, string $b) {
32+
echo 'Value of $a: ', $a, PHP_EOL,
33+
'Value of $b: ', $b, PHP_EOL;
34+
}
35+
}
36+
$stmt->execute();
37+
38+
try {
39+
var_dump($stmt->fetchAll(PDO::FETCH_CLASS, 'TestBase', ['b' => 'My key is B', 'No key']));
40+
} catch (\Throwable $e) {
41+
echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
42+
}
43+
44+
?>
45+
--CLEAN--
46+
<?php
47+
require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
48+
$db = PDOTest::factory();
49+
PDOTest::dropTableIfExists($db, "pdo_fetch_class_ctor_with_named_arguments_positional_after_named");
50+
?>
51+
--EXPECTF--
52+
Value of $a: My key is B
53+
Value of $b: No key
54+
Value of $a: My key is B
55+
Value of $b: No key
56+
Value of $a: My key is B
57+
Value of $b: No key
58+
array(3) {
59+
[0]=>
60+
object(TestBase)#%d (3) {
61+
["id"]=>
62+
string(1) "1"
63+
["val":protected]=>
64+
string(1) "A"
65+
["val2":"TestBase":private]=>
66+
string(2) "AA"
67+
}
68+
[1]=>
69+
object(TestBase)#%d (3) {
70+
["id"]=>
71+
string(1) "2"
72+
["val":protected]=>
73+
string(1) "B"
74+
["val2":"TestBase":private]=>
75+
string(2) "BB"
76+
}
77+
[2]=>
78+
object(TestBase)#%d (3) {
79+
["id"]=>
80+
string(1) "3"
81+
["val":protected]=>
82+
string(1) "C"
83+
["val2":"TestBase":private]=>
84+
string(2) "CC"
85+
}
86+
}

0 commit comments

Comments
 (0)