xref: /PHP-8.0/ext/intl/tests/formatter_fail.phpt (revision 9975986b)
1--TEST--
2numfmt creation failures
3--SKIPIF--
4<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
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
110IntlException: Constructor failed in %s on line %d
111'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR'
112'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR'
113'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR'
114
115TypeError: NumberFormatter::__construct(): Argument #1 ($locale) must be of type string, array given in %s on line %d
116'U_ZERO_ERROR'
117
118TypeError: NumberFormatter::create(): Argument #1 ($locale) must be of type string, array given in %s on line %d
119'U_ZERO_ERROR'
120
121TypeError: numfmt_create(): Argument #1 ($locale) must be of type string, array given in %s on line %d
122'U_ZERO_ERROR'
123
124IntlException: Constructor failed in %s on line %d
125'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR'
126'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR'
127'numfmt_create: number formatter creation failed: U_UNSUPPORTED_ERROR'
128
129IntlException: Constructor failed in %s on line %d
130'numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR'
131'numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR'
132'numfmt_create: number formatter creation failed: U_MEMORY_ALLOCATION_ERROR'
133