2019-12-24 01:52:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psl\Tests\Arr;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2019-12-25 00:35:14 +01:00
|
|
|
use Psl\Arr;
|
|
|
|
use Psl\Exception;
|
2019-12-24 01:52:07 +01:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
final class RandomTest extends TestCase
|
2019-12-24 01:52:07 +01:00
|
|
|
{
|
2019-12-25 00:35:14 +01:00
|
|
|
public function testRandom(): void
|
|
|
|
{
|
|
|
|
$values = ['a', 'b', 'c'];
|
2020-09-05 17:23:37 +02:00
|
|
|
$value = Arr\random($values);
|
2019-12-25 00:35:14 +01:00
|
|
|
|
|
|
|
static::assertNotNull($value);
|
|
|
|
static::assertContains($value, $values);
|
|
|
|
}
|
|
|
|
|
2019-12-26 15:45:26 +01:00
|
|
|
public function testRandomWithOneItem(): void
|
|
|
|
{
|
|
|
|
$values = ['a'];
|
2020-09-05 17:23:37 +02:00
|
|
|
$value = Arr\random($values);
|
2019-12-26 15:45:26 +01:00
|
|
|
|
|
|
|
static::assertSame('a', $value);
|
|
|
|
}
|
|
|
|
|
2019-12-25 00:35:14 +01:00
|
|
|
public function testRandomThrowsWhenTheGivenArrayIsEmpty(): void
|
|
|
|
{
|
|
|
|
$this->expectException(Exception\InvariantViolationException::class);
|
2021-02-16 01:49:55 +01:00
|
|
|
$this->expectExceptionMessage('Expected a non-empty iterable.');
|
2019-12-25 00:35:14 +01:00
|
|
|
|
|
|
|
Arr\random([]);
|
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|