3 // this is in the same namespace as Crypto for mocking 'rand' and 'random_init'
4 namespace Friendica\Util;
6 use PHPUnit\Framework\TestCase;
8 class CryptoTest extends TestCase
11 * Replaces rand results with given mocks
14 private function assertRand($min, $max)
17 $phpMock['rand'] = function($mMin, $mMax) use ($min, $max) {
18 $this->assertEquals($min, $mMin);
19 $this->assertEquals($max, $mMax);
25 * Replaces random_int results with given mocks
28 private function assertRandomInt($min, $max)
31 $phpMock['random_int'] = function($mMin, $mMax) use ($min, $max) {
32 $this->assertEquals($min, $mMin);
33 $this->assertEquals($max, $mMax);
38 public function testRandomDigitsRandomInt()
40 $this->assertRandomInt(0, 9);
42 $test = Crypto::randomDigits(1);
43 $this->assertEquals(1, strlen($test));
44 $this->assertEquals(1, $test);
46 $test = Crypto::randomDigits(8);
47 $this->assertEquals(8, strlen($test));
48 $this->assertEquals(11111111, $test);
53 * A workaround to replace the PHP native random_int() (>= 7.0) with a mocked function
57 function random_int($min, $max)
60 if (isset($phpMock['random_int'])) {
61 $result = call_user_func_array($phpMock['random_int'], func_get_args());