1--TEST--
2Random: Randomizer: Nul engines are correctly handled
3--FILE--
4<?php
5
6use Random\Engine;
7use Random\Randomizer;
8
9final class NulEngine implements Engine
10{
11    public function generate(): string
12    {
13        return str_repeat("\x00", PHP_INT_SIZE);
14    }
15}
16
17function randomizer(): Randomizer
18{
19    return new Randomizer(new NulEngine());
20}
21
22try {
23    var_dump(randomizer()->getInt(0, 1234));
24} catch (Random\BrokenRandomEngineError $e) {
25    echo $e->getMessage(), PHP_EOL;
26}
27
28try {
29    var_dump(randomizer()->nextInt());
30} catch (Random\BrokenRandomEngineError $e) {
31    echo $e->getMessage(), PHP_EOL;
32}
33
34try {
35    var_dump(bin2hex(randomizer()->getBytes(1)));
36} catch (Random\BrokenRandomEngineError $e) {
37    echo $e->getMessage(), PHP_EOL;
38}
39
40try {
41    var_dump(randomizer()->shuffleArray(range(1, 123)));
42} catch (Random\BrokenRandomEngineError $e) {
43    echo $e->getMessage(), PHP_EOL;
44}
45
46try {
47    var_dump(randomizer()->pickArrayKeys(range(1, 123), 1));
48} catch (Random\BrokenRandomEngineError $e) {
49    echo $e->getMessage(), PHP_EOL;
50}
51
52try {
53    var_dump(randomizer()->pickArrayKeys(range(1, 123), 10));
54} catch (Random\BrokenRandomEngineError $e) {
55    echo $e->getMessage(), PHP_EOL;
56}
57
58try {
59    var_dump(randomizer()->shuffleBytes('foobar'));
60} catch (Random\BrokenRandomEngineError $e) {
61    echo $e->getMessage(), PHP_EOL;
62}
63
64try {
65    var_dump(randomizer()->getBytesFromString('123', 10));
66} catch (Random\BrokenRandomEngineError $e) {
67    echo $e->getMessage(), PHP_EOL;
68}
69
70try {
71    var_dump(randomizer()->getBytesFromString(str_repeat('a', 500), 10));
72} catch (Random\BrokenRandomEngineError $e) {
73    echo $e->getMessage(), PHP_EOL;
74}
75
76?>
77--EXPECTF--
78int(0)
79int(0)
80string(2) "00"
81array(123) {
82  [0]=>
83  int(2)
84  [1]=>
85  int(3)
86  [2]=>
87  int(4)
88  [3]=>
89  int(5)
90  [4]=>
91  int(6)
92  [5]=>
93  int(7)
94  [6]=>
95  int(8)
96  [7]=>
97  int(9)
98  [8]=>
99  int(10)
100  [9]=>
101  int(11)
102  [10]=>
103  int(12)
104  [11]=>
105  int(13)
106  [12]=>
107  int(14)
108  [13]=>
109  int(15)
110  [14]=>
111  int(16)
112  [15]=>
113  int(17)
114  [16]=>
115  int(18)
116  [17]=>
117  int(19)
118  [18]=>
119  int(20)
120  [19]=>
121  int(21)
122  [20]=>
123  int(22)
124  [21]=>
125  int(23)
126  [22]=>
127  int(24)
128  [23]=>
129  int(25)
130  [24]=>
131  int(26)
132  [25]=>
133  int(27)
134  [26]=>
135  int(28)
136  [27]=>
137  int(29)
138  [28]=>
139  int(30)
140  [29]=>
141  int(31)
142  [30]=>
143  int(32)
144  [31]=>
145  int(33)
146  [32]=>
147  int(34)
148  [33]=>
149  int(35)
150  [34]=>
151  int(36)
152  [35]=>
153  int(37)
154  [36]=>
155  int(38)
156  [37]=>
157  int(39)
158  [38]=>
159  int(40)
160  [39]=>
161  int(41)
162  [40]=>
163  int(42)
164  [41]=>
165  int(43)
166  [42]=>
167  int(44)
168  [43]=>
169  int(45)
170  [44]=>
171  int(46)
172  [45]=>
173  int(47)
174  [46]=>
175  int(48)
176  [47]=>
177  int(49)
178  [48]=>
179  int(50)
180  [49]=>
181  int(51)
182  [50]=>
183  int(52)
184  [51]=>
185  int(53)
186  [52]=>
187  int(54)
188  [53]=>
189  int(55)
190  [54]=>
191  int(56)
192  [55]=>
193  int(57)
194  [56]=>
195  int(58)
196  [57]=>
197  int(59)
198  [58]=>
199  int(60)
200  [59]=>
201  int(61)
202  [60]=>
203  int(62)
204  [61]=>
205  int(63)
206  [62]=>
207  int(64)
208  [63]=>
209  int(65)
210  [64]=>
211  int(66)
212  [65]=>
213  int(67)
214  [66]=>
215  int(68)
216  [67]=>
217  int(69)
218  [68]=>
219  int(70)
220  [69]=>
221  int(71)
222  [70]=>
223  int(72)
224  [71]=>
225  int(73)
226  [72]=>
227  int(74)
228  [73]=>
229  int(75)
230  [74]=>
231  int(76)
232  [75]=>
233  int(77)
234  [76]=>
235  int(78)
236  [77]=>
237  int(79)
238  [78]=>
239  int(80)
240  [79]=>
241  int(81)
242  [80]=>
243  int(82)
244  [81]=>
245  int(83)
246  [82]=>
247  int(84)
248  [83]=>
249  int(85)
250  [84]=>
251  int(86)
252  [85]=>
253  int(87)
254  [86]=>
255  int(88)
256  [87]=>
257  int(89)
258  [88]=>
259  int(90)
260  [89]=>
261  int(91)
262  [90]=>
263  int(92)
264  [91]=>
265  int(93)
266  [92]=>
267  int(94)
268  [93]=>
269  int(95)
270  [94]=>
271  int(96)
272  [95]=>
273  int(97)
274  [96]=>
275  int(98)
276  [97]=>
277  int(99)
278  [98]=>
279  int(100)
280  [99]=>
281  int(101)
282  [100]=>
283  int(102)
284  [101]=>
285  int(103)
286  [102]=>
287  int(104)
288  [103]=>
289  int(105)
290  [104]=>
291  int(106)
292  [105]=>
293  int(107)
294  [106]=>
295  int(108)
296  [107]=>
297  int(109)
298  [108]=>
299  int(110)
300  [109]=>
301  int(111)
302  [110]=>
303  int(112)
304  [111]=>
305  int(113)
306  [112]=>
307  int(114)
308  [113]=>
309  int(115)
310  [114]=>
311  int(116)
312  [115]=>
313  int(117)
314  [116]=>
315  int(118)
316  [117]=>
317  int(119)
318  [118]=>
319  int(120)
320  [119]=>
321  int(121)
322  [120]=>
323  int(122)
324  [121]=>
325  int(123)
326  [122]=>
327  int(1)
328}
329array(1) {
330  [0]=>
331  int(0)
332}
333Failed to generate an acceptable random number in 50 attempts
334string(6) "oobarf"
335string(10) "1111111111"
336string(10) "aaaaaaaaaa"
337