endtoend-test-psl/tests/Psl/Random/StringTest.php

65 lines
1.7 KiB
PHP
Raw Normal View History

2019-12-24 01:52:07 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Random;
use PHPUnit\Framework\TestCase;
2019-12-25 20:38:27 +01:00
use Psl\Exception;
use Psl\Random;
use Psl\Str;
2019-12-24 01:52:07 +01:00
class StringTest extends TestCase
{
2019-12-25 19:11:35 +01:00
public function testString(): void
{
$random = Random\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 = Random\string(32, 'abc');
self::assertSame(32, Str\length($random));
foreach (Str\chunk($random) as $char) {
self::assertTrue(Str\contains('abc', $char));
}
}
2019-12-26 15:41:15 +01:00
public function testStringEarlyReturnForZeroLength(): void
{
self::assertSame('', Random\string(0));
}
2019-12-25 19:11:35 +01:00
public function testStringThrowsForNegativeLength(): void
{
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Expected positive length, got -1');
Random\string(-1);
}
public function testStringAlphabetMin(): void
{
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Expected $alphabet\'s length to be in [2^1, 2^56]');
Random\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));
// }
2019-12-24 01:52:07 +01:00
}