xref: /php-src/ext/pdo/tests/pdo_023.phpt (revision c1fec9bb)
1--TEST--
2PDO Common: extending PDO
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#[AllowDynamicProperties]
18class PDOStatementX extends PDOStatement
19{
20    public $test1 = 1;
21
22    protected function __construct()
23    {
24        $this->test2 = 2;
25        $this->test2 = 22;
26        echo __METHOD__ . "()\n";
27    }
28
29    function __destruct()
30    {
31        echo __METHOD__ . "()\n";
32    }
33}
34
35#[AllowDynamicProperties]
36class PDODatabaseX extends PDO
37{
38    public $test1 = 1;
39
40    function __destruct()
41    {
42        echo __METHOD__ . "()\n";
43    }
44
45    function testMethod()
46    {
47        $this->test2 = 2;
48        var_dump($this->test1);
49        var_dump($this->test2);
50        $this->test2 = 22;
51    }
52
53    function query($sql, ...$rest): PDOStatement|false
54    {
55        echo __METHOD__ . "()\n";
56        $stmt = parent::prepare($sql, array(PDO::ATTR_STATEMENT_CLASS=>array('PDOStatementx')));
57        $stmt->execute();
58        return $stmt;
59    }
60}
61
62$db = PDOTest::factory('PDODatabaseX');
63$db->testMethod();
64var_dump($db);
65
66$db->query('CREATE TABLE test023(id INT NOT NULL PRIMARY KEY, val VARCHAR(10))');
67$db->query("INSERT INTO test023 VALUES(0, 'A')");
68$db->query("INSERT INTO test023 VALUES(1, 'B')");
69
70
71$stmt = $db->query('SELECT val, id FROM test023');
72var_dump($stmt);
73var_dump($stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE));
74
75$stmt = NULL;
76$db = NULL;
77
78
79?>
80--CLEAN--
81<?php
82require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
83$db = PDOTest::factory();
84PDOTest::dropTableIfExists($db, "test023");
85?>
86--EXPECTF--
87int(1)
88int(2)
89object(PDODatabaseX)#%d (2) {
90  ["test1"]=>
91  int(1)
92  ["test2"]=>
93  int(22)
94}
95PDODatabaseX::query()
96PDOStatementX::__construct()
97PDOStatementX::__destruct()
98PDODatabaseX::query()
99PDOStatementX::__construct()
100PDOStatementX::__destruct()
101PDODatabaseX::query()
102PDOStatementX::__construct()
103PDOStatementX::__destruct()
104PDODatabaseX::query()
105PDOStatementX::__construct()
106object(PDOStatementX)#%d (3) {
107  ["queryString"]=>
108  string(27) "SELECT val, id FROM test023"
109  ["test1"]=>
110  int(1)
111  ["test2"]=>
112  int(22)
113}
114array(2) {
115  ["A"]=>
116  string(1) "0"
117  ["B"]=>
118  string(1) "1"
119}
120PDOStatementX::__destruct()
121PDODatabaseX::__destruct()
122