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\Collection;
|
|
|
|
use Psl\Exception;
|
|
|
|
use Psl\Iter;
|
2019-12-24 01:52:07 +01:00
|
|
|
|
|
|
|
class CountValuesTest extends TestCase
|
|
|
|
{
|
2019-12-25 00:35:14 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideData
|
|
|
|
*/
|
|
|
|
public function testCountValues(array $expected, iterable $iterable): void
|
|
|
|
{
|
|
|
|
static::assertSame($expected, Arr\count_values($iterable));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideData(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
['foo' => 1],
|
|
|
|
['foo'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
['foo' => 2, 'bar' => 1, 'baz' => 5],
|
|
|
|
['foo', 'bar', 'baz', 'foo', 'baz', 'baz', 'baz', 'baz'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
['foo' => 2, 'bar' => 1, 'baz' => 4],
|
|
|
|
Arr\concat(
|
|
|
|
new Collection\Vector(['foo', 'bar', 'baz']),
|
|
|
|
new Collection\Map([1 => 'foo']),
|
|
|
|
(fn () => yield 'baz')(),
|
2020-02-21 02:03:40 +01:00
|
|
|
new Collection\MutableVector(['baz', 'baz']),
|
2019-12-25 00:35:14 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1],
|
|
|
|
Iter\range(1, 5),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[1 => 5, 2 => 5, 3 => 5, 4 => 5, 5 => 5],
|
|
|
|
Arr\concat(
|
|
|
|
Iter\range(1, 5),
|
|
|
|
Iter\range(1, 5),
|
|
|
|
Iter\range(1, 5),
|
|
|
|
Iter\range(1, 5),
|
|
|
|
Iter\range(1, 5),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2020-07-07 15:17:36 +02:00
|
|
|
|
|
|
|
public function testCountThrowsForNonArrayKeyValues(): void
|
|
|
|
{
|
|
|
|
$this->expectException(Exception\InvariantViolationException::class);
|
|
|
|
$this->expectExceptionMessage('Expected all values to be of type array-key, value of type (object) provided.');
|
|
|
|
|
|
|
|
Arr\count_values([
|
|
|
|
new Collection\Map([]),
|
|
|
|
]);
|
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|