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