summaryrefslogtreecommitdiff
path: root/ext/pdo_mysql/tests/pdo_mysql_attr_statement_class.phpt
blob: 77f231d28fa653d6a53faeb67dc1ea96e25ce0d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
--TEST--
PDO::ATTR_STATEMENT_CLASS
--SKIPIF--
<?php
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();
$db = MySQLPDOTest::factory();
?>
--FILE--
<?php
    require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
    $db = MySQLPDOTest::factory();
    MySQLPDOTest::createTestTable($db);

    $default =  $db->getAttribute(PDO::ATTR_STATEMENT_CLASS);
    var_dump($default);

    try {
        $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, 'foo');
    } catch (\TypeError $e) {
        echo $e->getMessage(), \PHP_EOL;
    }
    try {
        $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['classname']);
    } catch (\TypeError $e) {
        echo $e->getMessage(), \PHP_EOL;
    }
    // unknown class
    try {
        $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['classname', []]);
    } catch (\TypeError $e) {
        echo $e->getMessage(), \PHP_EOL;
    }

    // class not derived from PDOStatement
    class myclass {
        function __construct() {
            printf("myclass\n");
        }
    }

    try {
        $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['myclass', []]);
    } catch (\TypeError $e) {
        echo $e->getMessage(), \PHP_EOL;
    }

    // public constructor not allowed
    class mystatement extends PDOStatement {
        public function __construct() {
            printf("mystatement\n");
        }
    }

    try {
        if (false !== ($tmp = $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['mystatement', []])))
            printf("[006] Expecting boolean/false got %s\n", var_export($tmp, true));
    } catch (\Error $e) {
        echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
    }


    // ... but a public destructor is allowed
    class mystatement2 extends PDOStatement {
        public function __destruct() {
            printf("mystatement\n");
        }
    }

    if (true !== ($tmp = $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('mystatement2', array()))))
        printf("[007] Expecting boolean/true got %s\n", var_export($tmp, true));

    // private constructor
    class mystatement3 extends PDOStatement {
        private function __construct($msg) {
            printf("mystatement3\n");
            var_dump($msg);
        }
    }
    if (true !== ($tmp = $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('mystatement3', array('param1')))))
        printf("[008] Expecting boolean/true got %s\n", var_export($tmp, true));

    // private constructor
    class mystatement4 extends PDOStatement {
        private function __construct($msg) {
            printf("%s\n", get_class($this));
            var_dump($msg);
        }
    }
    if (true !== ($tmp = $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('mystatement4', array('param1')))))
        printf("[008] Expecting boolean/true got %s\n", var_export($tmp, true));

    var_dump($db->getAttribute(PDO::ATTR_STATEMENT_CLASS));
    $stmt = $db->query('SELECT id, label FROM test ORDER BY id ASC LIMIT 2');

    class mystatement5 extends mystatement4 {
        public function fetchAll($fetch_style = 1, ...$fetch_args) {
            return "no data :)";
        }
    }

    if (true !== ($tmp = $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('mystatement5', array('mystatement5')))))
        printf("[009] Expecting boolean/true got %s\n", var_export($tmp, true));
    $stmt = $db->query('SELECT id, label FROM test ORDER BY id ASC LIMIT 2');
    var_dump($stmt->fetchAll());

    if (true !== ($tmp = $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('PDOStatement'))))
        printf("[010] Expecting boolean/true got %s\n", var_export($tmp, true));

    $stmt = $db->query('SELECT id, label FROM test ORDER BY id ASC LIMIT 1');
    var_dump($stmt->fetchAll());

    // Yes, this is a fatal error and I want it to fail.
    abstract class mystatement6 extends mystatement5 {
    }
    try {
        $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, ['mystatement6', ['mystatement6']]);
        $stmt = $db->query('SELECT id, label FROM test ORDER BY id ASC LIMIT 1');
    } catch (\Error $e) {
        echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
    }

?>
--EXPECT--
array(1) {
  [0]=>
  string(12) "PDOStatement"
}
PDO::ATTR_STATEMENT_CLASS value must be of type array, string given
PDO::ATTR_STATEMENT_CLASS class must be a valid class
PDO::ATTR_STATEMENT_CLASS class must be a valid class
PDO::ATTR_STATEMENT_CLASS class must be derived from PDOStatement
TypeError: User-supplied statement class cannot have a public constructor
array(2) {
  [0]=>
  string(12) "mystatement4"
  [1]=>
  array(1) {
    [0]=>
    string(6) "param1"
  }
}
mystatement4
string(6) "param1"
mystatement5
string(12) "mystatement5"
string(10) "no data :)"
array(1) {
  [0]=>
  array(4) {
    ["id"]=>
    string(1) "1"
    [0]=>
    string(1) "1"
    ["label"]=>
    string(1) "a"
    [1]=>
    string(1) "a"
  }
}
Error: Cannot instantiate abstract class mystatement6