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