|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use function Jasny\iterable_map; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @covers \Jasny\iterable_map |
|
10
|
|
|
*/ |
|
11
|
|
|
class IterableMapTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
use ProvideIterablesTrait; |
|
14
|
|
|
use LazyExecutionIteratorTrait; |
|
15
|
|
|
|
|
16
|
|
|
public function provider() |
|
17
|
|
|
{ |
|
18
|
|
|
return $this->provideIterables(range(1, 4)); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @dataProvider provider |
|
23
|
|
|
*/ |
|
24
|
|
|
public function test($values) |
|
25
|
|
|
{ |
|
26
|
|
|
$iterator = iterable_map($values, function($value) { |
|
27
|
|
|
return str_repeat('*', $value); |
|
28
|
|
|
}); |
|
29
|
|
|
|
|
30
|
|
|
$result = iterator_to_array($iterator); |
|
31
|
|
|
|
|
32
|
|
|
$expected = [ |
|
33
|
|
|
'*', |
|
34
|
|
|
'**', |
|
35
|
|
|
'***', |
|
36
|
|
|
'****' |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
$this->assertSame($expected, $result); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function keyValueProvider() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->provideIterables(['one' => 'foo', 'two' => 'bar', 'three' => 'qux'], false, false); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @dataProvider keyValueProvider |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testKeyValue($values) |
|
51
|
|
|
{ |
|
52
|
|
|
$iterator = iterable_map($values, function($value, $key) { |
|
53
|
|
|
return "$key-$value"; |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
$result = iterator_to_array($iterator); |
|
57
|
|
|
|
|
58
|
|
|
$expected = [ |
|
59
|
|
|
'one' => 'one-foo', |
|
60
|
|
|
'two' => 'two-bar', |
|
61
|
|
|
'three' => 'three-qux' |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
$this->assertSame($expected, $result); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testEmpty() |
|
68
|
|
|
{ |
|
69
|
|
|
$iterator = iterable_map(new \EmptyIterator(), function() {}); |
|
70
|
|
|
|
|
71
|
|
|
$result = iterator_to_array($iterator); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertEquals([], $result); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Test that nothing happens when not iterating |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testLazyExecution() |
|
80
|
|
|
{ |
|
81
|
|
|
$iterator = $this->createLazyExecutionIterator(); |
|
82
|
|
|
|
|
83
|
|
|
iterable_map($iterator, function() {}); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertTrue(true, "No warning"); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|