1--TEST--
2MySQL PDO: PDOStatement->fetchObject() with $constructorArgs
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9$db = MySQLPDOTest::factory();
10
11try {
12    $query = "SELECT '', NULL, \"\" FROM DUAL";
13    $stmt = $db->prepare($query);
14    $ok = $stmt->execute();
15} catch (PDOException $e) {
16    die("skip: Test cannot be run with SQL mode ANSI");
17}
18if (!$ok)
19    die("skip: Test cannot be run with SQL mode ANSI");
20?>
21--FILE--
22<?php
23require_once __DIR__ . '/inc/mysql_pdo_test.inc';
24/** @var PDO $db */
25$db = MySQLPDOTest::factory();
26
27$table = 'pdo_mysql_stmt_fetchobject_ctor_args';
28MySQLPDOTest::createTestTable($table, $db);
29
30$query = "SELECT id FROM {$table} ORDER BY id ASC LIMIT 1";
31$stmt = $db->prepare($query);
32
33class Foo {
34    public int $a;
35    public int $id;
36
37    public function __construct($a) {
38        $this->a = $a;
39    }
40}
41
42class Bar {
43    public int $id;
44}
45
46$stmt->execute();
47try {
48    $obj = $stmt->fetchObject(Foo::class);
49} catch (ArgumentCountError $exception) {
50    echo $exception->getMessage() . "\n";
51}
52
53$stmt->execute();
54try {
55    $obj = $stmt->fetchObject(Foo::class, []);
56} catch (ArgumentCountError $exception) {
57    echo $exception->getMessage() . "\n";
58}
59
60$stmt->execute();
61$obj = $stmt->fetchObject(Foo::class, ["a" => 123]);
62var_dump($obj);
63
64$stmt->execute();
65$obj = $stmt->fetchObject(Bar::class);
66var_dump($obj);
67
68$stmt->execute();
69$obj = $stmt->fetchObject(Bar::class, []);
70var_dump($obj);
71
72try {
73    $stmt->execute();
74    $obj = $stmt->fetchObject(Bar::class, ["a" => 123]);
75} catch (Error $exception) {
76    echo $exception->getMessage() . "\n";
77}
78
79?>
80--CLEAN--
81<?php
82require_once __DIR__ . '/inc/mysql_pdo_test.inc';
83$db = MySQLPDOTest::factory();
84$db->exec('DROP TABLE IF EXISTS pdo_mysql_stmt_fetchobject_ctor_args');
85?>
86--EXPECTF--
87Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
88Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
89object(Foo)#%d (2) {
90  ["a"]=>
91  int(123)
92  ["id"]=>
93  int(1)
94}
95object(Bar)#%d (1) {
96  ["id"]=>
97  int(1)
98}
99object(Bar)#%d (1) {
100  ["id"]=>
101  int(1)
102}
103User-supplied statement does not accept constructor arguments
104