1--TEST--
2Random: Randomizer: Engines returning an empty string are detected and rejected
3--FILE--
4<?php
5
6use Random\Engine;
7use Random\Randomizer;
8
9final class EmptyStringEngine implements Engine
10{
11    public function generate(): string
12    {
13        return '';
14    }
15}
16
17function randomizer(): Randomizer
18{
19    return new Randomizer(new EmptyStringEngine());
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, 1234)));
42} catch (Random\BrokenRandomEngineError $e) {
43    echo $e->getMessage(), PHP_EOL;
44}
45
46try {
47    var_dump(randomizer()->pickArrayKeys(range(1, 1234), 1));
48} catch (Random\BrokenRandomEngineError $e) {
49    echo $e->getMessage(), PHP_EOL;
50}
51
52try {
53    var_dump(randomizer()->pickArrayKeys(range(1, 1234), 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--EXPECT--
78A random engine must return a non-empty string
79A random engine must return a non-empty string
80A random engine must return a non-empty string
81A random engine must return a non-empty string
82A random engine must return a non-empty string
83A random engine must return a non-empty string
84A random engine must return a non-empty string
85A random engine must return a non-empty string
86A random engine must return a non-empty string
87