xref: /PHP-8.2/ext/intl/tests/formatter_fail.phpt (revision 683e7878)
1--TEST--
2numfmt creation failures
3--EXTENSIONS--
4intl
5--FILE--
6<?php
7
8function err($fmt) {
9    if(!$fmt) {
10        echo var_export(intl_get_error_message(), true)."\n";
11    }
12}
13
14function print_exception($e) {
15    echo "\n" . get_class($e) . ": " . $e->getMessage()
16       . " in " . $e->getFile() . " on line " . $e->getLine() . "\n";
17}
18
19function crt($t, $l, $s) {
20    switch(true) {
21        case $t == "O":
22            try {
23                return new NumberFormatter($l, $s);
24            } catch (Throwable $e) {
25                print_exception($e);
26                return null;
27            }
28            break;
29        case $t == "C":
30            try {
31                return NumberFormatter::create($l, $s);
32            } catch (Throwable $e) {
33                print_exception($e);
34                return null;
35            }
36            break;
37        case $t == "P":
38            try {
39                return numfmt_create($l, $s);
40            } catch (Throwable $e) {
41                print_exception($e);
42                return null;
43            }
44            break;
45    }
46}
47
48$args = array(
49    array(null, null),
50    array("whatever", 1234567),
51    array(array(), array()),
52    array("en", -1),
53    array("en_US", NumberFormatter::PATTERN_RULEBASED),
54);
55
56try {
57    $fmt = new NumberFormatter();
58} catch (TypeError $e) {
59    print_exception($e);
60    $fmt = null;
61}
62err($fmt);
63try {
64    $fmt = numfmt_create();
65} catch (TypeError $e) {
66    print_exception($e);
67    $fmt = null;
68}
69err($fmt);
70try {
71    $fmt = NumberFormatter::create();
72} catch (TypeError $e) {
73    print_exception($e);
74    $fmt = null;
75}
76err($fmt);
77
78$fmt = new NumberFormatter('en_US', NumberFormatter::DECIMAL);
79try {
80    $fmt->__construct('en_US', NumberFormatter::DECIMAL);
81} catch (Error $e) {
82    print_exception($e);
83    $fmt = null;
84}
85err($fmt);
86
87foreach($args as $arg) {
88    $fmt = crt("O", $arg[0], $arg[1]);
89    err($fmt);
90    $fmt = crt("C", $arg[0], $arg[1]);
91    err($fmt);
92    $fmt = crt("P", $arg[0], $arg[1]);
93    err($fmt);
94}
95
96?>
97--EXPECTF--
98ArgumentCountError: NumberFormatter::__construct() expects at least 2 arguments, 0 given in %s on line %d
99'U_ZERO_ERROR'
100
101ArgumentCountError: numfmt_create() expects at least 2 arguments, 0 given in %s on line %d
102'U_ZERO_ERROR'
103
104ArgumentCountError: NumberFormatter::create() expects at least 2 arguments, 0 given in %s on line %d
105'U_ZERO_ERROR'
106
107Error: NumberFormatter object is already constructed in %s on line %d
108'U_ZERO_ERROR'
109
110Deprecated: NumberFormatter::__construct(): Passing null to parameter #1 ($locale) of type string is deprecated in %s on line %d
111
112Deprecated: NumberFormatter::__construct(): Passing null to parameter #2 ($style) of type int is deprecated in %s on line %d
113
114Deprecated: NumberFormatter::create(): Passing null to parameter #1 ($locale) of type string is deprecated in %s on line %d
115
116Deprecated: NumberFormatter::create(): Passing null to parameter #2 ($style) of type int is deprecated in %s on line %d
117
118Deprecated: numfmt_create(): Passing null to parameter #1 ($locale) of type string is deprecated in %s on line %d
119
120Deprecated: numfmt_create(): Passing null to parameter #2 ($style) of type int is deprecated in %s on line %d
121
122ValueError: NumberFormatter::__construct(): Argument #1 ($locale) "%s" is invalid in %s on line %d
123'U_ZERO_ERROR'
124
125ValueError: NumberFormatter::create(): Argument #1 ($locale) "%s" is invalid in %s on line %d
126'U_ZERO_ERROR'
127
128ValueError: numfmt_create(): Argument #1 ($locale) "%s" is invalid in %s on line %d
129'U_ZERO_ERROR'
130
131TypeError: NumberFormatter::__construct(): Argument #1 ($locale) must be of type string, array given in %s on line %d
132'U_ZERO_ERROR'
133
134TypeError: NumberFormatter::create(): Argument #1 ($locale) must be of type string, array given in %s on line %d
135'U_ZERO_ERROR'
136
137TypeError: numfmt_create(): Argument #1 ($locale) must be of type string, array given in %s on line %d
138'U_ZERO_ERROR'
139
140IntlException: Constructor failed in %s on line %d
141'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR'
142'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR'
143'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR'
144
145IntlException: Constructor failed in %s on line %d
146'numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR'
147'numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR'
148'numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR'
149