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 SortByKeyTest extends TestCase
|
|
|
|
{
|
2019-12-25 00:35:14 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideData
|
|
|
|
*/
|
|
|
|
public function testSortByKey(array $expected, iterable $iterable, ?callable $comparator = null): void
|
|
|
|
{
|
|
|
|
self::assertSame($expected, Arr\sort_by_key($iterable, $comparator));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideData(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
['a' => 'orange', 'b' => 'banana', 'c' => 'apple', 'd' => 'lemon'],
|
|
|
|
['d' => 'lemon', 'a' => 'orange', 'b' => 'banana', 'c' => 'apple'],
|
|
|
|
null,
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
|
|
|
['d' => 'lemon', 'c' => 'apple', 'b' => 'banana', 'a' => 'orange'],
|
|
|
|
['d' => 'lemon', 'a' => 'orange', 'b' => 'banana', 'c' => 'apple'],
|
|
|
|
fn (string $a, string $b) => Str\ord($a) > Str\ord($b) ? -1 : 1,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|