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