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