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

38 lines
854 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\Str;
2019-12-24 01:52:07 +01:00
class UniqueByTest extends TestCase
{
2019-12-25 00:35:14 +01:00
/**
* @dataProvider provideData
*/
public function testUniqueBy(array $expected, iterable $iterable, callable $scalar_fun): void
{
static::assertSame($expected, Arr\unique_by($iterable, $scalar_fun));
}
public function provideData(): array
{
return [
[
[0 => 'a', 4 => 'saif'],
['a', 'b', 'c', 'd', 'saif', 'jack'],
fn ($value) => Str\length($value),
],
[
[0 => 'foo', 2 => 'bar', 4 => '@baz'],
['foo', '@foo', 'bar', '@bar', '@baz'],
fn ($value) => Str\replace($value, '@', ''),
],
];
}
2019-12-24 01:52:07 +01:00
}