1--TEST--
2Test strtr() function : usage variations - unexpected inputs for all arguments
3--FILE--
4<?php
5/* Prototype  : string strtr(string $str, string $from[, string $to]);
6                string strtr(string $str, array $replace_pairs);
7 * Description: Translates characters in str using given translation tables
8 * Source code: ext/standard/string.c
9*/
10
11/* Test strtr() function: with unexpected inputs for 'str', 'from', 'to' & 'replace_pairs' arguments */
12
13echo "*** Testing strtr() function: with unexpected inputs for all arguments ***\n";
14
15//get an unset variable
16$unset_var = 'string_val';
17unset($unset_var);
18
19//defining a class
20class sample  {
21  public function __toString() {
22    return "sample object";
23  }
24}
25
26//getting the resource
27$file_handle = fopen(__FILE__, "r");
28
29// array with different values
30$values =  array (
31
32		  // integer values
33/*1*/	  0,
34		  1,
35		  -2,
36
37		  // float values
38/*4*/	  10.5,
39		  -20.5,
40		  10.1234567e10,
41
42		  // array values
43/*7*/	  array(),
44		  array(0),
45		  array(1),
46		  array(1, 2),
47		  array('color' => 'red', 'item' => 'pen'),
48
49		  // boolean values
50/*12*/	  true,
51		  false,
52		  TRUE,
53		  FALSE,
54
55		  // null vlaues
56/*16*/	  NULL,
57		  null,
58
59		  // objects
60/*18*/	  new sample(),
61
62		  // resource
63/*19*/	  $file_handle,
64
65		  // undefined variable
66/*20*/	  @$undefined_var,
67
68		  // unset variable
69/*21*/	  @$unset_var
70);
71
72// loop through with each element of the $values array to test strtr() function
73$count = 1;
74for($index = 0; $index < count($values); $index++) {
75  echo "\n-- Iteration $count --\n";
76  var_dump( strtr($values[$index], $values[$index], $values[$index]) ); //fn call with three args
77  var_dump( strtr($values[$index], $values[$index]) );  //fn call with two args
78  $count ++;
79}
80
81fclose($file_handle);  //closing the file handle
82
83?>
84===DONE===
85--EXPECTF--
86*** Testing strtr() function: with unexpected inputs for all arguments ***
87
88-- Iteration 1 --
89string(1) "0"
90
91Warning: strtr(): The second argument is not an array in %s on line %d
92bool(false)
93
94-- Iteration 2 --
95string(1) "1"
96
97Warning: strtr(): The second argument is not an array in %s on line %d
98bool(false)
99
100-- Iteration 3 --
101string(2) "-2"
102
103Warning: strtr(): The second argument is not an array in %s on line %d
104bool(false)
105
106-- Iteration 4 --
107string(4) "10.5"
108
109Warning: strtr(): The second argument is not an array in %s on line %d
110bool(false)
111
112-- Iteration 5 --
113string(5) "-20.5"
114
115Warning: strtr(): The second argument is not an array in %s on line %d
116bool(false)
117
118-- Iteration 6 --
119string(12) "101234567000"
120
121Warning: strtr(): The second argument is not an array in %s on line %d
122bool(false)
123
124-- Iteration 7 --
125
126Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
127NULL
128
129Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
130NULL
131
132-- Iteration 8 --
133
134Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
135NULL
136
137Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
138NULL
139
140-- Iteration 9 --
141
142Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
143NULL
144
145Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
146NULL
147
148-- Iteration 10 --
149
150Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
151NULL
152
153Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
154NULL
155
156-- Iteration 11 --
157
158Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
159NULL
160
161Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
162NULL
163
164-- Iteration 12 --
165string(1) "1"
166
167Warning: strtr(): The second argument is not an array in %s on line %d
168bool(false)
169
170-- Iteration 13 --
171string(0) ""
172
173Warning: strtr(): The second argument is not an array in %s on line %d
174bool(false)
175
176-- Iteration 14 --
177string(1) "1"
178
179Warning: strtr(): The second argument is not an array in %s on line %d
180bool(false)
181
182-- Iteration 15 --
183string(0) ""
184
185Warning: strtr(): The second argument is not an array in %s on line %d
186bool(false)
187
188-- Iteration 16 --
189string(0) ""
190
191Warning: strtr(): The second argument is not an array in %s on line %d
192bool(false)
193
194-- Iteration 17 --
195string(0) ""
196
197Warning: strtr(): The second argument is not an array in %s on line %d
198bool(false)
199
200-- Iteration 18 --
201string(13) "sample object"
202
203Warning: strtr(): The second argument is not an array in %s on line %d
204bool(false)
205
206-- Iteration 19 --
207
208Warning: strtr() expects parameter 1 to be string, resource given in %s on line %d
209NULL
210
211Warning: strtr() expects parameter 1 to be string, resource given in %s on line %d
212NULL
213
214-- Iteration 20 --
215string(0) ""
216
217Warning: strtr(): The second argument is not an array in %s on line %d
218bool(false)
219
220-- Iteration 21 --
221string(0) ""
222
223Warning: strtr(): The second argument is not an array in %s on line %d
224bool(false)
225===DONE===
226