-
Notifications
You must be signed in to change notification settings - Fork 8
/
thruTest.php
87 lines (78 loc) · 1.54 KB
/
thruTest.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
<?php
/**
* @covers Dash\thru
* @covers Dash\Curry\thru
*/
class thruTest extends PHPUnit\Framework\TestCase
{
/**
* @dataProvider cases
*/
public function test($value)
{
$passed = null;
$result = Dash\thru($value, function ($passed) use ($value) {
$this->assertSame($value, $passed);
$passed = 'changed';
return $passed;
});
$this->assertSame('changed', $result);
}
/**
* @dataProvider cases
*/
public function testCurried($value)
{
$passed = null;
$thru = Dash\Curry\thru(function ($passed) use ($value) {
$this->assertSame($value, $passed);
$passed = 'changed';
return $passed;
});
$this->assertSame('changed', $thru($value));
}
public function cases()
{
return [
'With null' => [
'value' => null,
],
'With a number' => [
'value' => 3.14,
],
'With a string' => [
'value' => 'hello',
],
'With a DateTime' => [
'value' => new DateTime(),
],
'With an empty array' => [
'value' => [],
],
'With an indexed array' => [
'value' => [1, 2, 3],
],
'With an associative array' => [
'value' => ['a' => 1, 'b' => 2, 'c' => 3],
],
'With an stdClass' => [
'value' => (object) [1, 2, 3],
],
'With an ArrayObject' => [
'value' => new ArrayObject([1, 2, 3]),
],
];
}
public function testExamples()
{
$result = Dash\chain([1, 3, 4])
->filter('Dash\isOdd')
->thru(function ($value) {
// $value === [1, 3]
$value[] = $value[0];
return $value;
})
->value();
$this->assertSame([1, 3, 1], $result);
}
}