1--TEST--
2MySQL PDO: PDOStatement->fetchObject() with $constructorArgs
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once(__DIR__ . DIRECTORY_SEPARATOR . '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__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
24/** @var PDO $db */
25$db = MySQLPDOTest::factory();
26MySQLPDOTest::createTestTable($db);
27
28$query = "SELECT id FROM test ORDER BY id ASC LIMIT 1";
29$stmt = $db->prepare($query);
30
31class Foo {
32    public int $a;
33    public int $id;
34
35    public function __construct($a) {
36        $this->a = $a;
37    }
38}
39
40class Bar {
41    public int $id;
42}
43
44$stmt->execute();
45try {
46    $obj = $stmt->fetchObject(Foo::class);
47} catch (ArgumentCountError $exception) {
48    echo $exception->getMessage() . "\n";
49}
50
51$stmt->execute();
52try {
53    $obj = $stmt->fetchObject(Foo::class, []);
54} catch (ArgumentCountError $exception) {
55    echo $exception->getMessage() . "\n";
56}
57
58$stmt->execute();
59$obj = $stmt->fetchObject(Foo::class, ["a" => 123]);
60var_dump($obj);
61
62$stmt->execute();
63$obj = $stmt->fetchObject(Bar::class);
64var_dump($obj);
65
66$stmt->execute();
67$obj = $stmt->fetchObject(Bar::class, []);
68var_dump($obj);
69
70try {
71    $stmt->execute();
72    $obj = $stmt->fetchObject(Bar::class, ["a" => 123]);
73} catch (Error $exception) {
74    echo $exception->getMessage() . "\n";
75}
76
77?>
78--CLEAN--
79<?php
80require __DIR__ . '/mysql_pdo_test.inc';
81MySQLPDOTest::dropTestTable();
82?>
83--EXPECTF--
84Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
85Too few arguments to function Foo::__construct(), 0 passed and exactly 1 expected
86object(Foo)#%d (2) {
87  ["a"]=>
88  int(123)
89  ["id"]=>
90  int(1)
91}
92object(Bar)#%d (1) {
93  ["id"]=>
94  int(1)
95}
96object(Bar)#%d (1) {
97  ["id"]=>
98  int(1)
99}
100User-supplied statement does not accept constructor arguments
101