1--TEST--
2Testing several callbacks using PDO::FETCH_FUNC
3--SKIPIF--
4<?php
5if (!extension_loaded('pdo_sqlite')) print 'skip not loaded';
6?>
7--FILE--
8<?php
9
10$db = new PDO('sqlite::memory:');
11$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
12
13$db->exec('CREATE TABLE testing (id INTEGER , name VARCHAR)');
14$db->exec('INSERT INTO testing VALUES(1, "php")');
15$db->exec('INSERT INTO testing VALUES(2, "")');
16
17$st = $db->query('SELECT * FROM testing');
18$st->fetchAll(PDO::FETCH_FUNC, function($x, $y) use ($st) { var_dump($st); print "data: $x, $y\n"; });
19
20$st = $db->query('SELECT name FROM testing');
21var_dump($st->fetchAll(PDO::FETCH_FUNC, 'strtoupper'));
22
23try {
24    $st = $db->query('SELECT * FROM testing');
25    var_dump($st->fetchAll(PDO::FETCH_FUNC, 'nothing'));
26} catch (\TypeError $e) {
27    echo $e->getMessage(), \PHP_EOL;
28}
29
30try {
31    $st = $db->query('SELECT * FROM testing');
32    var_dump($st->fetchAll(PDO::FETCH_FUNC, ''));
33} catch (\TypeError $e) {
34    echo $e->getMessage(), \PHP_EOL;
35}
36
37try {
38    $st = $db->query('SELECT * FROM testing');
39    var_dump($st->fetchAll(PDO::FETCH_FUNC, NULL));
40} catch (\TypeError $e) {
41    echo $e->getMessage(), \PHP_EOL;
42}
43
44try {
45    $st = $db->query('SELECT * FROM testing');
46    var_dump($st->fetchAll(PDO::FETCH_FUNC, 1));
47} catch (\TypeError $e) {
48    echo $e->getMessage(), \PHP_EOL;
49}
50
51try {
52    $st = $db->query('SELECT * FROM testing');
53    var_dump($st->fetchAll(PDO::FETCH_FUNC, array('self', 'foo')));
54} catch (\TypeError $e) {
55    echo $e->getMessage(), \PHP_EOL;
56}
57
58class foo {
59    public function method($x) {
60        return "--- $x ---";
61    }
62}
63class bar extends foo {
64    public function __construct($db) {
65        $st = $db->query('SELECT * FROM testing');
66        var_dump($st->fetchAll(PDO::FETCH_FUNC, array($this, 'parent::method')));
67    }
68
69    static public function test($x, $y) {
70        return $x .'---'. $y;
71    }
72
73    private function test2($x, $y) {
74        return $x;
75    }
76
77    public function test3($x, $y) {
78        return $x .'==='. $y;
79    }
80}
81
82new bar($db);
83
84$st = $db->query('SELECT * FROM testing');
85var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test')));
86
87try {
88    $st = $db->query('SELECT * FROM testing');
89    var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test2')));
90} catch (\TypeError $e) {
91    echo $e->getMessage(), \PHP_EOL;
92}
93
94try {
95    $st = $db->query('SELECT * FROM testing');
96    var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'test3')));
97} catch (\TypeError $e) {
98    echo $e->getMessage(), \PHP_EOL;
99}
100
101try {
102    $st = $db->query('SELECT * FROM testing');
103    var_dump($st->fetchAll(PDO::FETCH_FUNC, array('bar', 'inexistent')));
104} catch (\TypeError $e) {
105    echo $e->getMessage(), \PHP_EOL;
106}
107
108?>
109--EXPECTF--
110object(PDOStatement)#%d (1) {
111  ["queryString"]=>
112  string(21) "SELECT * FROM testing"
113}
114data: 1, php
115object(PDOStatement)#%d (1) {
116  ["queryString"]=>
117  string(21) "SELECT * FROM testing"
118}
119data: 2,
120array(2) {
121  [0]=>
122  string(3) "PHP"
123  [1]=>
124  string(0) ""
125}
126function "nothing" not found or invalid function name
127function "" not found or invalid function name
128PDOStatement::fetchAll(): Argument #2 must be a callable, null given
129no array or string given
130cannot access "self" when no class scope is active
131array(2) {
132  [0]=>
133  string(9) "--- 1 ---"
134  [1]=>
135  string(9) "--- 2 ---"
136}
137array(2) {
138  [0]=>
139  string(7) "1---php"
140  [1]=>
141  string(4) "2---"
142}
143non-static method bar::test2() cannot be called statically
144non-static method bar::test3() cannot be called statically
145class bar does not have a method "inexistent"
146