|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Novactive Collection. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Luke Visinoni <[email protected], [email protected]> |
|
6
|
|
|
* @author Sébastien Morel <[email protected], [email protected]> |
|
7
|
|
|
* @copyright 2017 Novactive |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Novactive\Tests; |
|
12
|
|
|
|
|
13
|
|
|
use Novactive\Collection\Factory; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class CombineKeysCollectionTest. |
|
17
|
|
|
*/ |
|
18
|
|
|
class CombineKeysCollectionTest extends UnitTestCase |
|
19
|
|
|
{ |
|
20
|
|
View Code Duplication |
public function testCombineKeysUsesIncomingTraversableAsKeysForCollectionsValues() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$coll = Factory::create($this->fixtures['names']); |
|
23
|
|
|
$orig = $coll->toArray(); |
|
24
|
|
|
$newkeys = $coll->combineKeys($this->fixtures['emails']); |
|
25
|
|
|
$this->assertEquals( |
|
26
|
|
|
[ |
|
27
|
|
|
'[email protected]' => 'Chelsea', |
|
28
|
|
|
'[email protected]' => 'Adella', |
|
29
|
|
|
'[email protected]' => 'Monte', |
|
30
|
|
|
'[email protected]' => 'Maye', |
|
31
|
|
|
'[email protected]' => 'Lottie', |
|
32
|
|
|
'[email protected]' => 'Don', |
|
33
|
|
|
'[email protected]' => 'Dayton', |
|
34
|
|
|
'[email protected]' => 'Kirk', |
|
35
|
|
|
'[email protected]' => 'Troy', |
|
36
|
|
|
'[email protected]' => 'Nakia', |
|
37
|
|
|
], |
|
38
|
|
|
$newkeys->toArray() |
|
39
|
|
|
); |
|
40
|
|
|
$this->assertNotSame($newkeys->toArray(), $orig); |
|
41
|
|
|
$this->assertSame( |
|
42
|
|
|
$coll->toArray(), |
|
43
|
|
|
$orig, |
|
44
|
|
|
'The original collection should not be affected by Collection::combineKeys().' |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @expectedException \InvalidArgumentException |
|
50
|
|
|
* @expectedExceptionMessage Invalid input type for combineKeys. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testCombineKeysThrowsExceptionIfPassedNonTraversableNonArray() |
|
53
|
|
|
{ |
|
54
|
|
|
$coll = Factory::create($this->fixtures['emails']); |
|
55
|
|
|
$coll->combineKeys('this is not traversable'); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @expectedException \InvalidArgumentException |
|
60
|
|
|
* @expectedExceptionMessage Invalid input for combineKeys, number of items does not match. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testCombineKeysThrowsExceptionIfPassedTraversableWithBadCount() |
|
63
|
|
|
{ |
|
64
|
|
|
$coll = Factory::create($this->fixtures['emails']); |
|
65
|
|
|
$coll->combineKeys([1, 2, 3]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
public function testCombineKeysAcceptsTraversable() |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$coll = Factory::create($this->fixtures['names']); |
|
71
|
|
|
$orig = $coll->toArray(); |
|
72
|
|
|
$iter = $this->getIteratorForArray($this->fixtures['emails']); |
|
73
|
|
|
$keycombined = $coll->combineKeys($iter); |
|
74
|
|
|
$this->assertSame( |
|
75
|
|
|
[ |
|
76
|
|
|
'[email protected]' => 'Chelsea', |
|
77
|
|
|
'[email protected]' => 'Adella', |
|
78
|
|
|
'[email protected]' => 'Monte', |
|
79
|
|
|
'[email protected]' => 'Maye', |
|
80
|
|
|
'[email protected]' => 'Lottie', |
|
81
|
|
|
'[email protected]' => 'Don', |
|
82
|
|
|
'[email protected]' => 'Dayton', |
|
83
|
|
|
'[email protected]' => 'Kirk', |
|
84
|
|
|
'[email protected]' => 'Troy', |
|
85
|
|
|
'[email protected]' => 'Nakia', |
|
86
|
|
|
], |
|
87
|
|
|
$keycombined->toArray() |
|
88
|
|
|
); |
|
89
|
|
|
$this->assertSame( |
|
90
|
|
|
$orig, |
|
91
|
|
|
$coll->toArray(), |
|
92
|
|
|
'Ensure that Collection::combineKeys does not change the original collection.' |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.