xref: /php-src/ext/pdo/tests/pdo_030.phpt (revision f4a5db3e)
1--TEST--
2PDO Common: extending PDO (4)
3--EXTENSIONS--
4pdo
5--SKIPIF--
6<?php
7$dir = getenv('REDIR_TEST_DIR');
8if (false == $dir) die('skip no driver');
9require_once $dir . 'pdo_test.inc';
10PDOTest::skip();
11?>
12--FILE--
13<?php
14if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.__DIR__ . '/../../pdo/tests/');
15require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
16
17$data = array(
18    array('10', 'Abc', 'zxy'),
19    array('20', 'Def', 'wvu'),
20    array('30', 'Ghi', 'tsr'),
21);
22
23class PDOStatementX extends PDOStatement
24{
25    public $dbh;
26
27    protected function __construct($dbh)
28    {
29        $this->dbh = $dbh;
30        $this->setFetchMode(PDO::FETCH_ASSOC);
31        echo __METHOD__ . "()\n";
32    }
33
34    function __destruct()
35    {
36        echo __METHOD__ . "()\n";
37    }
38
39    function execute($params = array()): bool
40    {
41        echo __METHOD__ . "()\n";
42        parent::execute();
43    }
44}
45
46class PDODatabase extends PDO
47{
48    function __destruct()
49    {
50        echo __METHOD__ . "()\n";
51    }
52
53    function query($sql, ...$rest): PDOStatement|false
54    {
55        echo __METHOD__ . "()\n";
56        return parent::query($sql);
57    }
58}
59
60$db = PDOTest::factory('PDODatabase');
61var_dump(get_class($db));
62
63$db->exec('CREATE TABLE test030(id INT NOT NULL PRIMARY KEY, val VARCHAR(10), val2 VARCHAR(16))');
64
65$stmt = $db->prepare("INSERT INTO test030 VALUES(?, ?, ?)");
66var_dump(get_class($stmt));
67foreach ($data as $row) {
68    $stmt->execute($row);
69}
70
71unset($stmt);
72
73echo "===QUERY===\n";
74
75var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS));
76$db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOStatementx', array($db)));
77var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS));
78$stmt = $db->query('SELECT * FROM test030');
79var_dump(get_class($stmt));
80var_dump(get_class($stmt->dbh));
81
82echo "===FOREACH===\n";
83
84foreach($stmt as $obj) {
85    var_dump($obj);
86}
87
88echo "===DONE===\n";
89exit(0);
90?>
91--CLEAN--
92<?php
93require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
94$db = PDOTest::factory();
95PDOTest::dropTableIfExists($db, "test030");
96?>
97--EXPECTF--
98string(11) "PDODatabase"
99string(12) "PDOStatement"
100===QUERY===
101array(1) {
102  [0]=>
103  string(12) "PDOStatement"
104}
105array(2) {
106  [0]=>
107  string(13) "PDOStatementX"
108  [1]=>
109  array(1) {
110    [0]=>
111    object(PDODatabase)#%d (0) {
112    }
113  }
114}
115PDODatabase::query()
116PDOStatementX::__construct()
117string(13) "PDOStatementX"
118string(11) "PDODatabase"
119===FOREACH===
120array(3) {
121  ["id"]=>
122  string(2) "10"
123  ["val"]=>
124  string(3) "Abc"
125  ["val2"]=>
126  string(3) "zxy"
127}
128array(3) {
129  ["id"]=>
130  string(2) "20"
131  ["val"]=>
132  string(3) "Def"
133  ["val2"]=>
134  string(3) "wvu"
135}
136array(3) {
137  ["id"]=>
138  string(2) "30"
139  ["val"]=>
140  string(3) "Ghi"
141  ["val2"]=>
142  string(3) "tsr"
143}
144===DONE===
145PDOStatementX::__destruct()
146PDODatabase::__destruct()
147