xref: /php-src/ext/pdo/tests/pdo_011.phpt (revision c1fec9bb)
1--TEST--
2PDO Common: PDO::FETCH_FUNC and statement overloading
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$db = PDOTest::factory();
17
18$db->exec('CREATE TABLE test011(id int NOT NULL PRIMARY KEY, val VARCHAR(10), grp VARCHAR(10))');
19$db->exec("INSERT INTO test011 VALUES(1, 'A', 'Group1')");
20$db->exec("INSERT INTO test011 VALUES(2, 'B', 'Group1')");
21$db->exec("INSERT INTO test011 VALUES(3, 'C', 'Group2')");
22$db->exec("INSERT INTO test011 VALUES(4, 'D', 'Group2')");
23
24class DerivedStatement extends PDOStatement
25{
26    private function __construct(public $name, $db)
27    {
28        echo __METHOD__ . "($name)\n";
29    }
30
31    function reTrieve($id, $val) {
32        echo __METHOD__ . "($id,$val)\n";
33        return array($id=>$val);
34    }
35}
36
37$select1 = $db->prepare('SELECT grp, id FROM test011');
38$select2 = $db->prepare('SELECT id, val FROM test011');
39$derived = $db->prepare('SELECT id, val FROM test011', array(PDO::ATTR_STATEMENT_CLASS=>array('DerivedStatement', array('Overloaded', $db))));
40
41class Test1
42{
43    public function __construct(public $id, public $val)
44    {
45        echo __METHOD__ . "($id,$val)\n";
46    }
47
48    static public function factory($id, $val)
49    {
50        echo __METHOD__ . "($id,$val)\n";
51        return new self($id, $val);
52    }
53}
54
55function callback($id,$val='N/A')
56{
57    echo __METHOD__ . "($id,$val)\n";
58    return array($id=>$val);
59}
60
61$f = new Test1(0,0);
62
63$select1->execute();
64var_dump($select1->fetchAll(PDO::FETCH_FUNC|PDO::FETCH_GROUP, 'callback'));
65
66$select2->execute();
67var_dump($select2->fetchAll(PDO::FETCH_FUNC, 'callback'));
68
69$select2->execute();
70var_dump($select2->fetchAll(PDO::FETCH_FUNC, array('Test1','factory')));
71
72$select2->execute();
73var_dump($select2->fetchAll(PDO::FETCH_FUNC, array($f, 'factory')));
74
75var_dump(get_class($derived));
76$derived->execute();
77var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'retrieve')));
78$derived->execute();
79var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'reTrieve')));
80$derived->execute();
81var_dump($derived->fetchAll(PDO::FETCH_FUNC, array($derived, 'RETRIEVE')));
82
83?>
84--CLEAN--
85<?php
86require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
87$db = PDOTest::factory();
88PDOTest::dropTableIfExists($db, "test011");
89?>
90--EXPECTF--
91DerivedStatement::__construct(Overloaded)
92Test1::__construct(0,0)
93callback(1,N/A)
94callback(2,N/A)
95callback(3,N/A)
96callback(4,N/A)
97array(2) {
98  ["Group1"]=>
99  array(2) {
100    [0]=>
101    array(1) {
102      [1]=>
103      string(3) "N/A"
104    }
105    [1]=>
106    array(1) {
107      [2]=>
108      string(3) "N/A"
109    }
110  }
111  ["Group2"]=>
112  array(2) {
113    [0]=>
114    array(1) {
115      [3]=>
116      string(3) "N/A"
117    }
118    [1]=>
119    array(1) {
120      [4]=>
121      string(3) "N/A"
122    }
123  }
124}
125callback(1,A)
126callback(2,B)
127callback(3,C)
128callback(4,D)
129array(4) {
130  [0]=>
131  array(1) {
132    [1]=>
133    string(1) "A"
134  }
135  [1]=>
136  array(1) {
137    [2]=>
138    string(1) "B"
139  }
140  [2]=>
141  array(1) {
142    [3]=>
143    string(1) "C"
144  }
145  [3]=>
146  array(1) {
147    [4]=>
148    string(1) "D"
149  }
150}
151Test1::factory(1,A)
152Test1::__construct(1,A)
153Test1::factory(2,B)
154Test1::__construct(2,B)
155Test1::factory(3,C)
156Test1::__construct(3,C)
157Test1::factory(4,D)
158Test1::__construct(4,D)
159array(4) {
160  [0]=>
161  object(Test1)#%d (2) {
162    ["id"]=>
163    string(1) "1"
164    ["val"]=>
165    string(1) "A"
166  }
167  [1]=>
168  object(Test1)#%d (2) {
169    ["id"]=>
170    string(1) "2"
171    ["val"]=>
172    string(1) "B"
173  }
174  [2]=>
175  object(Test1)#%d (2) {
176    ["id"]=>
177    string(1) "3"
178    ["val"]=>
179    string(1) "C"
180  }
181  [3]=>
182  object(Test1)#%d (2) {
183    ["id"]=>
184    string(1) "4"
185    ["val"]=>
186    string(1) "D"
187  }
188}
189Test1::factory(1,A)
190Test1::__construct(1,A)
191Test1::factory(2,B)
192Test1::__construct(2,B)
193Test1::factory(3,C)
194Test1::__construct(3,C)
195Test1::factory(4,D)
196Test1::__construct(4,D)
197array(4) {
198  [0]=>
199  object(Test1)#%d (2) {
200    ["id"]=>
201    string(1) "1"
202    ["val"]=>
203    string(1) "A"
204  }
205  [1]=>
206  object(Test1)#%d (2) {
207    ["id"]=>
208    string(1) "2"
209    ["val"]=>
210    string(1) "B"
211  }
212  [2]=>
213  object(Test1)#%d (2) {
214    ["id"]=>
215    string(1) "3"
216    ["val"]=>
217    string(1) "C"
218  }
219  [3]=>
220  object(Test1)#%d (2) {
221    ["id"]=>
222    string(1) "4"
223    ["val"]=>
224    string(1) "D"
225  }
226}
227string(16) "DerivedStatement"
228DerivedStatement::reTrieve(1,A)
229DerivedStatement::reTrieve(2,B)
230DerivedStatement::reTrieve(3,C)
231DerivedStatement::reTrieve(4,D)
232array(4) {
233  [0]=>
234  array(1) {
235    [1]=>
236    string(1) "A"
237  }
238  [1]=>
239  array(1) {
240    [2]=>
241    string(1) "B"
242  }
243  [2]=>
244  array(1) {
245    [3]=>
246    string(1) "C"
247  }
248  [3]=>
249  array(1) {
250    [4]=>
251    string(1) "D"
252  }
253}
254DerivedStatement::reTrieve(1,A)
255DerivedStatement::reTrieve(2,B)
256DerivedStatement::reTrieve(3,C)
257DerivedStatement::reTrieve(4,D)
258array(4) {
259  [0]=>
260  array(1) {
261    [1]=>
262    string(1) "A"
263  }
264  [1]=>
265  array(1) {
266    [2]=>
267    string(1) "B"
268  }
269  [2]=>
270  array(1) {
271    [3]=>
272    string(1) "C"
273  }
274  [3]=>
275  array(1) {
276    [4]=>
277    string(1) "D"
278  }
279}
280DerivedStatement::reTrieve(1,A)
281DerivedStatement::reTrieve(2,B)
282DerivedStatement::reTrieve(3,C)
283DerivedStatement::reTrieve(4,D)
284array(4) {
285  [0]=>
286  array(1) {
287    [1]=>
288    string(1) "A"
289  }
290  [1]=>
291  array(1) {
292    [2]=>
293    string(1) "B"
294  }
295  [2]=>
296  array(1) {
297    [3]=>
298    string(1) "C"
299  }
300  [3]=>
301  array(1) {
302    [4]=>
303    string(1) "D"
304  }
305}
306