|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Test; |
|
4
|
|
|
|
|
5
|
|
|
use function spatie\array_split; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
class ArraySplitTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @test |
|
12
|
|
|
*/ |
|
13
|
|
|
public function it_can_handle_an_empty_array() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->assertSame(array_split([]), []); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @dataProvider argumentsProvider |
|
20
|
|
|
* @test |
|
21
|
|
|
*/ |
|
22
|
|
|
public function it_throws_exception_when_second_parameter_is_lower_than_1($numberOfPieces, $mustThrow) |
|
23
|
|
|
{ |
|
24
|
|
|
$exception = null; |
|
25
|
|
|
try { |
|
26
|
|
|
array_split([], $numberOfPieces); |
|
27
|
|
|
} catch (\InvalidArgumentException $exception) {} |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$this->assertSame($mustThrow, $exception instanceof \InvalidArgumentException); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function argumentsProvider() |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
|
|
[2 , false], |
|
36
|
|
|
[1 , false], |
|
37
|
|
|
[0 , true], |
|
38
|
|
|
[-1 , true], |
|
39
|
|
|
[-2 , true], |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function it_splits_an_array_in_two_by_default() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->assertSame(array_split(['a', 'b']), [['a'], ['b']]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @test |
|
53
|
|
|
*/ |
|
54
|
|
|
public function it_splits_an_array_in_two_by_default_while_preserving_keys() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->assertSame(array_split(['a' => 1, 'b' => 2], 2, true), [['a' => 1], ['b' => 2]]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @dataProvider arrayProvider |
|
61
|
|
|
* |
|
62
|
|
|
* @test |
|
63
|
|
|
* |
|
64
|
|
|
* @param $array |
|
65
|
|
|
* @param $splitIntoNumber |
|
66
|
|
|
* @param $expectedArray |
|
67
|
|
|
*/ |
|
68
|
|
|
public function it_can_split_an_array_in_equal_pieces($array, $splitIntoNumber, $expectedArray) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->assertSame($expectedArray, array_split($array, $splitIntoNumber)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function arrayProvider() |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
[ |
|
77
|
|
|
['a', 'b', 'c', 'd'], 2, [['a', 'b'], ['c', 'd']], |
|
78
|
|
|
], |
|
79
|
|
|
[ |
|
80
|
|
|
['a', 'b', 'c', 'd', 'e'], 2, [['a', 'b', 'c'], ['d', 'e']], |
|
81
|
|
|
], |
|
82
|
|
|
[ |
|
83
|
|
|
['a', 'b', 'c', 'd', 'e'], 3, [['a', 'b'], ['c', 'd'], ['e']], |
|
84
|
|
|
], |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|