/
GroupTest.php
121 lines (105 loc) · 4.25 KB
/
GroupTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* @package Functional-php
* @author Lars Strojny <lstrojny@php.net>
* @copyright 2011-2021 Lars Strojny
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/lstrojny/functional-php
*/
namespace Functional\Tests;
use ArrayIterator;
use Functional\Exceptions\InvalidArgumentException;
use function Functional\group;
class GroupTest extends AbstractTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->list = ['value1', 'value2', 'value3', 'value4'];
$this->listIterator = new ArrayIterator($this->list);
$this->hash = ['k1' => 'val1', 'k2' => 'val2', 'k3' => 'val3'];
$this->hashIterator = new ArrayIterator($this->hash);
}
public function test(): void
{
$fn = function ($v, $k, $collection) {
InvalidArgumentException::assertCollection($collection, __FUNCTION__, 3);
return (\is_int($k) ? ($k % 2 == 0) : ($v[3] % 2 == 0)) ? 'foo' : '';
};
self::assertSame(['foo' => [0 => 'value1', 2 => 'value3'], '' => [1 => 'value2', 3 => 'value4']], group($this->list, $fn));
self::assertSame(['foo' => [0 => 'value1', 2 => 'value3'], '' => [1 => 'value2', 3 => 'value4']], group($this->listIterator, $fn));
self::assertSame(['' => ['k1' => 'val1', 'k3' => 'val3'], 'foo' => ['k2' => 'val2']], group($this->hash, $fn));
self::assertSame(['' => ['k1' => 'val1', 'k3' => 'val3'], 'foo' => ['k2' => 'val2']], group($this->hashIterator, $fn));
}
public function testExceptionIsThrownWhenCallbacksReturnsInvalidKey(): void
{
$array = ['v1', 'v2', 'v3', 'v4', 'v5', 'v6'];
$keyMap = [true, 1, -1, 2.1, 'str', null];
$fn = function ($v, $k, $collection) use (&$keyMap) {
return $keyMap[$k];
};
$result = [
1 => [0 => 'v1', 1 => 'v2'],
-1 => [2 => 'v3'],
2 => [3 => 'v4'],
'str' => [4 => 'v5'],
null => [5 => 'v6']
];
self::assertSame($result, group($array, $fn));
self::assertSame($result, group(new ArrayIterator($array), $fn));
$invalidTypes = [
'resource' => \stream_context_create(),
'object' => new \stdClass(),
'array' => []
];
foreach ($invalidTypes as $type => $value) {
$keyMap = [$value];
try {
group(['v1'], $fn);
self::fail(\sprintf('Error expected for array key type "%s"', $type));
} catch (\Exception $e) {
self::assertSame(
\sprintf(
'Functional\group(): callback returned invalid array key of type "%s". Expected NULL, string, integer, double or boolean',
$type
),
$e->getMessage()
);
}
}
}
public function testExceptionIsThrownInArray(): void
{
$this->expectException('DomainException');
$this->expectExceptionMessage('Callback exception');
group($this->list, [$this, 'exception']);
}
public function testExceptionIsThrownInHash(): void
{
$this->expectException('DomainException');
$this->expectExceptionMessage('Callback exception');
group($this->hash, [$this, 'exception']);
}
public function testExceptionIsThrownInIterator(): void
{
$this->expectException('DomainException');
$this->expectExceptionMessage('Callback exception');
group($this->listIterator, [$this, 'exception']);
}
public function testExceptionIsThrownInHashIterator(): void
{
$this->expectException('DomainException');
$this->expectExceptionMessage('Callback exception');
group($this->hashIterator, [$this, 'exception']);
}
public function testPassNoCollection(): void
{
$this->expectArgumentError('Functional\group() expects parameter 1 to be array or instance of Traversable');
group('invalidCollection', 'strlen');
}
public function testPassNonCallable(): void
{
$this->expectCallableArgumentError('Functional\group', 2);
group($this->list, 'undefinedFunction');
}
}