Hey all.

A recent twitter-thread[1] left me thinking that while we have by now multiple Clock-implementations to ease testing with Dates and times there is not really a way currently to ease testing with randomnes.

PHP itself provides the random_int and random_bytes functions and some libraries provide additional sources of randomnes. But there is no way of being able to "assert a certain random value" which makes functions requiring randomness hard to test.

ANd so far I have not found any interoperability standard in that direction in the PHP-Ecosystem. There are some libraries (not a huge number) but I did not find an interface for interoperability and better testing.

I would therefore like to propose a "RandomGeneratorInterface" similar to the "CLockInterface" in that it provides an interface that can then be used to either inject a "SystemRandomGenerator" or a "FrozenRandomGenerator" into a class to further down be able to use predefined randomness-values to be able to write meaningful and reusable tests as well as use a decent randomness-value.

The added advantage can be that future improvements on randomness-generation can easily be used in applications as only a new implementation of the RandomGeneratorInterface needs to be injected.

My current idea of an interface would look like this, but this is open for debate within a working group.

interface RandomGeneratorInterface {

public function generateInt(int $min = PHP_INT_MIN, int $max = PHP_INT_MAX): int;
    public function generateBytes(int $length = 64): string;
public function generateString(int $length = 64, $chars = [a-zA-Z0-9]): string;
}

This interface is influenced by Anthony Ferraras random-lib[2] as well as by the PHP-internal CSPRNG-functions.

Is this something the FIG would benefit from?

Looking forward to a discussion.

Cheers

Andreas


[1]: https://twitter.com/ramsey/status/1574099413861306368
[2]: https://packagist.org/packages/ircmaxell/random-lib
--
                                                              ,,,
                                                             (o o)
+---------------------------------------------------------ooO-(_)-Ooo-+
| Andreas Heigl                                                       |
| mailto:andr...@heigl.org                  N 50°22'59.5" E 08°23'58" |
| https://andreas.heigl.org                                           |
+---------------------------------------------------------------------+
| https://hei.gl/appointmentwithandreas                               |
+---------------------------------------------------------------------+

--
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to php-fig+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/635d95e7-a88f-0670-5478-7fa2c58d0f1d%40heigl.org.

Reply via email to