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
|
|
|
|
|
|
|
class RandomTest extends TestCase
|
|
|
|
{
|
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);
|
2020-07-09 00:28:29 +02:00
|
|
|
$this->expectExceptionMessage('Expected a non-empty-array.');
|
2019-12-25 00:35:14 +01:00
|
|
|
|
|
|
|
Arr\random([]);
|
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|