1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
<?php
declare(strict_types=1);
namespace Ramsey\Uuid\Test\Generator;
use Exception;
use Ramsey\Uuid\Exception\RandomSourceException;
use Ramsey\Uuid\Generator\RandomBytesGenerator;
use Ramsey\Uuid\Test\TestCase;
use phpmock\mockery\PHPMockery;
use function hex2bin;
class RandomBytesGeneratorTest extends TestCase
{
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
*/
public function lengthAndHexDataProvider(): array
{
return [
[6, '4f17dd046fb8'],
[10, '4d25f6fe5327cb04267a'],
[12, '1ea89f83bd49cacfdf119e24'],
];
}
/**
* @param int<1, max> $length
*
* @throws Exception
*
* @dataProvider lengthAndHexDataProvider
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGenerateReturnsRandomBytes(int $length, string $hex): void
{
$bytes = hex2bin($hex);
PHPMockery::mock('Ramsey\Uuid\Generator', 'random_bytes')
->once()
->with($length)
->andReturn($bytes);
$generator = new RandomBytesGenerator();
$this->assertSame($bytes, $generator->generate($length));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGenerateThrowsExceptionWhenExceptionThrownByRandombytes(): void
{
PHPMockery::mock('Ramsey\Uuid\Generator', 'random_bytes')
->once()
->with(16)
->andThrow(new Exception('Could not gather sufficient random data'));
$generator = new RandomBytesGenerator();
$this->expectException(RandomSourceException::class);
$this->expectExceptionMessage('Could not gather sufficient random data');
$generator->generate(16);
}
}
|