1--TEST--
2Test sprintf() function : error conditions
3--FILE--
4<?php
5echo "*** Testing sprintf() : error conditions ***\n";
6
7// Zero arguments
8echo "\n-- Testing sprintf() function with Zero arguments --\n";
9try {
10    var_dump( sprintf() );
11} catch (TypeError $e) {
12    echo $e->getMessage(), "\n";
13}
14
15echo "\n-- Testing sprintf() function with less than expected no. of arguments --\n";
16$format1 = '%s';
17$format2 = '%s%s';
18$format3 = '%s%s%s';
19$arg1 = 'one';
20$arg2 = 'two';
21
22// with one argument less than expected
23try {
24    var_dump( sprintf($format1) );
25} catch (\ArgumentCountError $e) {
26    echo $e->getMessage(), "\n";
27}
28try {
29    var_dump( sprintf($format2,$arg1) );
30} catch (\ArgumentCountError $e) {
31    echo $e->getMessage(), "\n";
32}
33try {
34    var_dump( sprintf($format3,$arg1,$arg2) );
35} catch (\ArgumentCountError $e) {
36    echo $e->getMessage(), "\n";
37}
38
39// with two argument less than expected
40try {
41    var_dump( sprintf($format2) );
42} catch (\ArgumentCountError $e) {
43    echo $e->getMessage(), "\n";
44}
45try {
46    var_dump( sprintf($format3,$arg1) );
47} catch (\ArgumentCountError $e) {
48    echo $e->getMessage(), "\n";
49}
50
51// with three argument less than expected
52try {
53    var_dump( sprintf($format3) );
54} catch (\ArgumentCountError $e) {
55    echo $e->getMessage(), "\n";
56}
57
58try {
59    var_dump(sprintf('%100$d %d'));
60} catch (\ArgumentCountError $e) {
61    echo $e->getMessage(), "\n";
62}
63
64try {
65    var_dump(sprintf("foo %", 42));
66} catch (ValueError $e) {
67    echo $e->getMessage(), "\n";
68}
69
70echo "Done";
71?>
72--EXPECT--
73*** Testing sprintf() : error conditions ***
74
75-- Testing sprintf() function with Zero arguments --
76sprintf() expects at least 1 argument, 0 given
77
78-- Testing sprintf() function with less than expected no. of arguments --
792 arguments are required, 1 given
803 arguments are required, 2 given
814 arguments are required, 3 given
823 arguments are required, 1 given
834 arguments are required, 2 given
844 arguments are required, 1 given
85101 arguments are required, 1 given
86Missing format specifier at end of string
87Done
88