friendica/tests/src/Util/CryptoTest.php

65 lines
1.3 KiB
PHP
Raw Normal View History

2018-11-05 09:37:03 +01:00
<?php
2018-11-05 21:03:55 +01:00
// this is in the same namespace as Crypto for mocking 'rand' and 'random_init'
2018-11-05 09:37:03 +01:00
namespace Friendica\Util;
use PHPUnit\Framework\TestCase;
class CryptoTest extends TestCase
{
/**
* Replaces rand results with given mocks
*
*/
private function assertRand($min, $max)
{
global $phpMock;
$phpMock['rand'] = function($mMin, $mMax) use ($min, $max) {
$this->assertEquals($min, $mMin);
$this->assertEquals($max, $mMax);
return 1;
};
}
/**
* Replaces rand results with given mocks
*
*/
private function assertRandomInt($min, $max)
{
global $phpMock;
$phpMock['random_int'] = function($mMin, $mMax) use ($min, $max) {
$this->assertEquals($min, $mMin);
$this->assertEquals($max, $mMax);
return 1;
};
}
public function testRandomDigitsRandomInt()
{
$this->assertRandomInt(0, 9);
$test = Crypto::randomDigits(1);
$this->assertEquals(1, strlen($test));
$this->assertEquals(1, $test);
$test = Crypto::randomDigits(8);
$this->assertEquals(8, strlen($test));
$this->assertEquals(11111111, $test);
}
}
/**
* A workaround to replace the PHP native random_int() (>= 7.0) with a mocked function
*
* @return int
*/
function random_int($min, $max)
{
global $phpMock;
if (isset($phpMock['random_int'])) {
$result = call_user_func_array($phpMock['random_int'], func_get_args());
return $result;
}
}