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
|
|
|
|
*/
|
2020-08-27 16:14:37 +02:00
|
|
|
public function testUniqueBy(array $expected, array $array, callable $scalar_fun): void
|
2019-12-25 00:35:14 +01:00
|
|
|
{
|
2020-08-27 16:14:37 +02:00
|
|
|
static::assertSame($expected, Arr\unique_by($array, $scalar_fun));
|
2019-12-25 00:35:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideData(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
[0 => 'a', 4 => 'saif'],
|
|
|
|
['a', 'b', 'c', 'd', 'saif', 'jack'],
|
2020-08-27 16:14:37 +02:00
|
|
|
fn (string $value): int => Str\length($value),
|
2019-12-25 00:35:14 +01:00
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
[0 => 'foo', 2 => 'bar', 4 => '@baz'],
|
|
|
|
['foo', '@foo', 'bar', '@bar', '@baz'],
|
2020-08-27 16:14:37 +02:00
|
|
|
fn (string $value): string => Str\replace($value, '@', ''),
|
2019-12-25 00:35:14 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|