endtoend-test-psl/tests/Psl/SecureRandom/StringTest.php
2020-07-16 17:13:12 +01:00

65 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Psl\Tests\SecureRandom;
use PHPUnit\Framework\TestCase;
use Psl\Exception;
use Psl\SecureRandom;
use Psl\Str;
class StringTest extends TestCase
{
public function testString(): void
{
$random = SecureRandom\string(32);
self::assertSame(32, Str\length($random));
foreach (Str\chunk($random) as $char) {
self::assertTrue(Str\contains(Str\ALPHABET_ALPHANUMERIC, $char));
}
}
public function testStringWithSpecificChars(): void
{
$random = SecureRandom\string(32, 'abc');
self::assertSame(32, Str\length($random));
foreach (Str\chunk($random) as $char) {
self::assertTrue(Str\contains('abc', $char));
}
}
public function testStringEarlyReturnForZeroLength(): void
{
self::assertSame('', SecureRandom\string(0));
}
public function testStringThrowsForNegativeLength(): void
{
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Expected a non-negative length.');
SecureRandom\string(-1);
}
public function testStringAlphabetMin(): void
{
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Expected $alphabet\'s length to be in [2^1, 2^56]');
SecureRandom\string(32, 'a');
}
// public function testStringAlphabetMax(): void
// {
// $this->markTestSkipped('Memory exhausting');
//
// $this->expectException(Exception\InvariantViolationException::class);
// $this->expectExceptionMessage('Expected $alphabet\'s length to be in [2^1, 2^56]');
//
// Random\string(32, Str\repeat('a', (2 ** 56) + 1));
// }
}