xref: /php-src/ext/filter/tests/029.phpt (revision 74859783)
1--TEST--
2filter_var() and FILTER_CALLBACK
3--EXTENSIONS--
4filter
5--FILE--
6<?php
7
8/* Simple callback function */
9function test($var) {
10    return strtoupper($var);
11}
12
13var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test")));
14var_dump(filter_var("~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?\"}{:", FILTER_CALLBACK, array("options"=>"test")));
15var_dump(filter_var("", FILTER_CALLBACK, array("options"=>"test")));
16
17try {
18    filter_var("qwe", FILTER_CALLBACK, array("options"=>"no such func"));
19} catch (TypeError $exception) {
20    echo $exception->getMessage() . "\n";
21}
22
23try {
24    filter_var("qwe", FILTER_CALLBACK, array("options"=>""));
25} catch (TypeError $exception) {
26    echo $exception->getMessage() . "\n";
27}
28
29try {
30    filter_var("qwe", FILTER_CALLBACK);
31} catch (TypeError $exception) {
32    echo $exception->getMessage() . "\n";
33}
34
35/* Simple class method callback */
36class test_class {
37    static function test ($var) {
38        return strtolower($var);
39    }
40}
41
42var_dump(filter_var("dAtA", FILTER_CALLBACK, array("options"=>array("test_class", "test"))));
43var_dump(filter_var("~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?\"}{:", FILTER_CALLBACK, array("options"=>array("test_class","test"))));
44var_dump(filter_var("", FILTER_CALLBACK, array("options"=>array("test_class","test"))));
45
46/* empty function without return value */
47function test1($var) {
48}
49
50var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test1")));
51var_dump(filter_var("~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?\"}{:", FILTER_CALLBACK, array("options"=>"test1")));
52var_dump(filter_var("", FILTER_CALLBACK, array("options"=>"test1")));
53
54/* attempting to change data by reference */
55function test2(&$var) {
56    $var = 1;
57}
58
59var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test2")));
60var_dump(filter_var("~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?\"}{:", FILTER_CALLBACK, array("options"=>"test2")));
61var_dump(filter_var("", FILTER_CALLBACK, array("options"=>"test2")));
62
63/* thrown exception in the callback */
64function test3($var) {
65    throw new Exception("test");
66}
67
68try {
69    var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test3")));
70} catch (Exception $e) {
71    var_dump($e->getMessage());
72}
73
74echo "Done\n";
75?>
76--EXPECTF--
77string(4) "DATA"
78string(46) "~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?"}{:"
79string(0) ""
80filter_var(): Option must be a valid callback
81filter_var(): Option must be a valid callback
82filter_var(): Option must be a valid callback
83string(4) "data"
84string(46) "~!@#$%^&*()_qwertyuiopasdfghjklzxcvbnm<>>?"}{:"
85string(0) ""
86NULL
87NULL
88NULL
89
90Warning: test2(): Argument #1 ($var) must be passed by reference, value given in %s on line %d
91NULL
92
93Warning: test2(): Argument #1 ($var) must be passed by reference, value given in %s on line %d
94NULL
95
96Warning: test2(): Argument #1 ($var) must be passed by reference, value given in %s on line %d
97NULL
98string(4) "test"
99Done
100