endtoend-test-psl/tests/Psl/Arr/RandomTest.php

38 lines
809 B
PHP
Raw Normal View History

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
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'];
$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'];
$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);
$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
}