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