1--TEST--
2Test mb_substitute_character() function : usage variation
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7echo "*** Testing mb_substitute_character(): various types in weak typing mode ***\n";
8
9// Initialise function arguments not being substituted (if any)
10
11//get an unset variable
12$unset_var = 10;
13unset ($unset_var);
14
15// define some classes
16class classWithToString
17{
18    public function __toString() {
19        return "Class A object";
20    }
21}
22
23class classWithoutToString
24{
25}
26
27// heredoc string
28$heredoc = <<<EOT
29hello world
30EOT;
31
32// get a resource variable
33$fp = fopen(__FILE__, "r");
34
35// add arrays
36$index_array = array (1, 2, 3);
37$assoc_array = array ('one' => 1, 'two' => 2);
38
39//array of values to iterate over
40$inputs = array(
41
42      // int data
43      'int 0' => 0,
44      'int 1' => 1,
45      'int 12345' => 12345,
46      'int -12345' => -2345,
47
48      // float data
49      'float 10.5' => 10.5,
50      'float -10.5' => -10.5,
51      'float 10.0e19' => 10.0e19, // Cannot be represented as int
52      'float -10.0e19' => -10.0e19, // Cannot be represented as int
53      'float .5' => .5,
54
55      // array data
56      'empty array' => array(),
57      'int indexed array' => $index_array,
58      'associative array' => $assoc_array,
59      'nested arrays' => array('foo', $index_array, $assoc_array),
60
61      // null data
62      'uppercase NULL' => NULL,
63      'lowercase null' => null,
64
65      // boolean data
66      'lowercase true' => true,
67      'lowercase false' =>false,
68      'uppercase TRUE' =>TRUE,
69      'uppercase FALSE' =>FALSE,
70
71      // empty data
72      'empty string DQ' => "",
73      'empty string SQ' => '',
74
75      // string data
76      'string DQ' => "string",
77      'string SQ' => 'string',
78      'mixed case string' => "sTrInG",
79      'heredoc' => $heredoc,
80
81      // object data
82      'instance of classWithToString' => new classWithToString(),
83      'instance of classWithoutToString' => new classWithoutToString(),
84
85      // undefined data
86      'undefined var' => @$undefined_var,
87
88      // unset data
89      'unset var' => @$unset_var,
90);
91
92// loop through each element of the array for substchar
93
94mb_internal_encoding('utf-8');
95foreach($inputs as $key =>$value) {
96      echo "--$key--\n";
97      try {
98          var_dump( mb_substitute_character($value) );
99      } catch (\ValueError|\TypeError $e) {
100          echo get_class($e) . ': ' . $e->getMessage() . \PHP_EOL;
101      }
102}
103
104fclose($fp);
105
106?>
107--EXPECTF--
108*** Testing mb_substitute_character(): various types in weak typing mode ***
109--int 0--
110bool(true)
111--int 1--
112bool(true)
113--int 12345--
114bool(true)
115--int -12345--
116ValueError: mb_substitute_character(): Argument #1 ($substitute_character) is not a valid codepoint
117--float 10.5--
118
119Deprecated: Implicit conversion from float 10.5 to int loses precision in %s on line %d
120bool(true)
121--float -10.5--
122
123Deprecated: Implicit conversion from float -10.5 to int loses precision in %s on line %d
124ValueError: mb_substitute_character(): Argument #1 ($substitute_character) is not a valid codepoint
125--float 10.0e19--
126ValueError: mb_substitute_character(): Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint
127--float -10.0e19--
128ValueError: mb_substitute_character(): Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint
129--float .5--
130
131Deprecated: Implicit conversion from float 0.5 to int loses precision in %s on line %d
132bool(true)
133--empty array--
134TypeError: mb_substitute_character(): Argument #1 ($substitute_character) must be of type string|int|null, array given
135--int indexed array--
136TypeError: mb_substitute_character(): Argument #1 ($substitute_character) must be of type string|int|null, array given
137--associative array--
138TypeError: mb_substitute_character(): Argument #1 ($substitute_character) must be of type string|int|null, array given
139--nested arrays--
140TypeError: mb_substitute_character(): Argument #1 ($substitute_character) must be of type string|int|null, array given
141--uppercase NULL--
142int(0)
143--lowercase null--
144int(0)
145--lowercase true--
146bool(true)
147--lowercase false--
148bool(true)
149--uppercase TRUE--
150bool(true)
151--uppercase FALSE--
152bool(true)
153--empty string DQ--
154ValueError: mb_substitute_character(): Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint
155--empty string SQ--
156ValueError: mb_substitute_character(): Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint
157--string DQ--
158ValueError: mb_substitute_character(): Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint
159--string SQ--
160ValueError: mb_substitute_character(): Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint
161--mixed case string--
162ValueError: mb_substitute_character(): Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint
163--heredoc--
164ValueError: mb_substitute_character(): Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint
165--instance of classWithToString--
166ValueError: mb_substitute_character(): Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint
167--instance of classWithoutToString--
168TypeError: mb_substitute_character(): Argument #1 ($substitute_character) must be of type string|int|null, classWithoutToString given
169--undefined var--
170int(0)
171--unset var--
172int(0)
173