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