xref: /PHP-5.3/ext/filter/tests/029.phpt (revision 2c3db736)
1--TEST--
2filter_var() and FILTER_CALLBACK
3--SKIPIF--
4<?php if (!extension_loaded("filter")) die("skip"); ?>
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")));
16var_dump(filter_var("qwe", FILTER_CALLBACK, array("options"=>"no such func")));
17var_dump(filter_var("qwe", FILTER_CALLBACK, array("options"=>"")));
18var_dump(filter_var("qwe", FILTER_CALLBACK));
19
20/* Simple class method callback */
21class test_class {
22	static function test ($var) {
23		return strtolower($var);
24	}
25}
26
27var_dump(filter_var("dAtA", FILTER_CALLBACK, array("options"=>array("test_class", "test"))));
28var_dump(filter_var("~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?\"}{:", FILTER_CALLBACK, array("options"=>array("test_class","test"))));
29var_dump(filter_var("", FILTER_CALLBACK, array("options"=>array("test_class","test"))));
30
31/* empty function without return value */
32function test1($var) {
33}
34
35var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test1")));
36var_dump(filter_var("~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?\"}{:", FILTER_CALLBACK, array("options"=>"test1")));
37var_dump(filter_var("", FILTER_CALLBACK, array("options"=>"test1")));
38
39/* attempting to change data by reference */
40function test2(&$var) {
41	$var = 1;
42}
43
44var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test2")));
45var_dump(filter_var("~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?\"}{:", FILTER_CALLBACK, array("options"=>"test2")));
46var_dump(filter_var("", FILTER_CALLBACK, array("options"=>"test2")));
47
48/* unsetting data */
49function test3(&$var) {
50	unset($var);
51}
52
53var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test3")));
54var_dump(filter_var("~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?\"}{:", FILTER_CALLBACK, array("options"=>"test3")));
55var_dump(filter_var("", FILTER_CALLBACK, array("options"=>"test3")));
56
57/* unset data and return value */
58function test4(&$var) {
59	unset($var);
60	return 1;
61}
62
63var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test4")));
64
65/* thrown exception in the callback */
66function test5(&$var) {
67	throw new Exception("test");
68}
69
70try {
71	var_dump(filter_var("data", FILTER_CALLBACK, array("options"=>"test5")));
72} catch (Exception $e) {
73	var_dump($e->getMessage());
74}
75
76echo "Done\n";
77?>
78--EXPECTF--
79string(4) "DATA"
80string(46) "~!@#$%^&*()_QWERTYUIOPASDFGHJKLZXCVBNM<>>?"}{:"
81string(0) ""
82
83Warning: filter_var(): First argument is expected to be a valid callback in %s on line %d
84NULL
85
86Warning: filter_var(): First argument is expected to be a valid callback in %s on line %d
87NULL
88
89Warning: filter_var(): First argument is expected to be a valid callback in %s on line %d
90NULL
91string(4) "data"
92string(46) "~!@#$%^&*()_qwertyuiopasdfghjklzxcvbnm<>>?"}{:"
93string(0) ""
94NULL
95NULL
96NULL
97NULL
98NULL
99NULL
100NULL
101NULL
102NULL
103int(1)
104string(4) "test"
105Done
106