1--TEST--
2Test strtr() function : usage variations - unexpected inputs for 'from' argument
3--FILE--
4<?php
5/* Test strtr() function: with unexpected inputs for 'from'
6 *  and expected type for 'str' & 'to' arguments
7*/
8
9echo "*** Testing strtr() function: with unexpected inputs for 'from' ***\n";
10
11//get an unset variable
12$unset_var = 'string_val';
13unset($unset_var);
14
15//defining a class
16class sample  {
17  public function __toString() {
18    return "sample object";
19  }
20}
21
22//getting the resource
23$file_handle = fopen(__FILE__, "r");
24
25//defining 'str' argument
26$str = "012atm";
27
28// array of values for 'from'
29$from_arr =  array (
30
31          // integer values
32/*1*/	  0,
33          1,
34          -2,
35
36          // float values
37/*4*/	  10.5,
38          -20.5,
39          10.1234567e10,
40
41          // array values
42/*7*/	  array(),
43          array(0),
44          array(1, 2),
45
46          // boolean values
47/*10*/	  true,
48          false,
49          TRUE,
50          FALSE,
51
52          // null values
53/*14*/	  NULL,
54          null,
55
56          // objects
57/*16*/	  new sample(),
58
59          // resource
60/*17*/	  $file_handle,
61
62          // undefined variable
63/*18*/	  @$undefined_var,
64
65          // unset variable
66/*19*/	  @$unset_var
67);
68
69//defining 'to' argument
70$to = "atm012";
71
72// loop through with each element of the $from array to test strtr() function
73$count = 1;
74for($index = 0; $index < count($from_arr); $index++) {
75  echo "-- Iteration $count --\n";
76  $from = $from_arr[$index];
77  try {
78    var_dump(strtr($str, $from, $to));
79  } catch (TypeError $exception) {
80    echo $exception->getMessage() . "\n";
81  }
82  $count++;
83}
84
85fclose($file_handle);  //closing the file handle
86?>
87--EXPECT--
88*** Testing strtr() function: with unexpected inputs for 'from' ***
89-- Iteration 1 --
90string(6) "a12atm"
91-- Iteration 2 --
92string(6) "0a2atm"
93-- Iteration 3 --
94string(6) "01tatm"
95-- Iteration 4 --
96string(6) "ta2atm"
97-- Iteration 5 --
98string(6) "m1tatm"
99-- Iteration 6 --
100string(6) "tm0atm"
101-- Iteration 7 --
102strtr(): Argument #2 ($from) must be of type string, array given
103-- Iteration 8 --
104strtr(): Argument #2 ($from) must be of type string, array given
105-- Iteration 9 --
106strtr(): Argument #2 ($from) must be of type string, array given
107-- Iteration 10 --
108string(6) "0a2atm"
109-- Iteration 11 --
110string(6) "012atm"
111-- Iteration 12 --
112string(6) "0a2atm"
113-- Iteration 13 --
114string(6) "012atm"
115-- Iteration 14 --
116string(6) "012atm"
117-- Iteration 15 --
118string(6) "012atm"
119-- Iteration 16 --
120string(6) "012ttm"
121-- Iteration 17 --
122strtr(): Argument #2 ($from) must be of type array|string, resource given
123-- Iteration 18 --
124string(6) "012atm"
125-- Iteration 19 --
126string(6) "012atm"
127