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 CombineCollectionTest. |
17
|
|
|
*/ |
18
|
|
|
class CombineCollectionTest extends UnitTestCase |
19
|
|
|
{ |
20
|
|
|
public function testCombineReturnsCollectionWithExistingKeysAndIncomingValues() |
21
|
|
|
{ |
22
|
|
|
$coll = Factory::create($this->fixtures['names']); |
23
|
|
|
|
24
|
|
|
$this->assertSame( |
25
|
|
|
[ |
26
|
|
|
'Chelsea', |
27
|
|
|
'Adella', |
28
|
|
|
'Monte', |
29
|
|
|
'Maye', |
30
|
|
|
'Lottie', |
31
|
|
|
'Don', |
32
|
|
|
'Dayton', |
33
|
|
|
'Kirk', |
34
|
|
|
'Troy', |
35
|
|
|
'Nakia', |
36
|
|
|
], |
37
|
|
|
$orig = $coll->toArray() |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$newcoll = $coll->combine($this->fixtures['emails']); |
41
|
|
|
$this->assertNotSame($coll, $newcoll); |
42
|
|
|
$this->assertSame( |
43
|
|
|
[ |
44
|
|
|
'Chelsea' => '[email protected]', |
45
|
|
|
'Adella' => '[email protected]', |
46
|
|
|
'Monte' => '[email protected]', |
47
|
|
|
'Maye' => '[email protected]', |
48
|
|
|
'Lottie' => '[email protected]', |
49
|
|
|
'Don' => '[email protected]', |
50
|
|
|
'Dayton' => '[email protected]', |
51
|
|
|
'Kirk' => '[email protected]', |
52
|
|
|
'Troy' => '[email protected]', |
53
|
|
|
'Nakia' => '[email protected]', |
54
|
|
|
], |
55
|
|
|
$newcoll->toArray() |
56
|
|
|
); |
57
|
|
|
$this->assertSame( |
58
|
|
|
$orig, |
59
|
|
|
$coll->toArray(), |
60
|
|
|
'Ensure that Collection::combine() has not changed the original collection.' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testCombineAcceptsTraversable() |
65
|
|
|
{ |
66
|
|
|
$stub = $this->getIteratorForArray($this->fixtures['emails']); |
67
|
|
|
$coll = Factory::create($this->fixtures['names']); |
68
|
|
|
$this->assertSame( |
69
|
|
|
[ |
70
|
|
|
'Chelsea', |
71
|
|
|
'Adella', |
72
|
|
|
'Monte', |
73
|
|
|
'Maye', |
74
|
|
|
'Lottie', |
75
|
|
|
'Don', |
76
|
|
|
'Dayton', |
77
|
|
|
'Kirk', |
78
|
|
|
'Troy', |
79
|
|
|
'Nakia', |
80
|
|
|
], |
81
|
|
|
$coll->toArray() |
82
|
|
|
); |
83
|
|
|
$this->assertSame( |
84
|
|
|
[ |
85
|
|
|
'[email protected]', |
86
|
|
|
'[email protected]', |
87
|
|
|
'[email protected]', |
88
|
|
|
'[email protected]', |
89
|
|
|
'[email protected]', |
90
|
|
|
'[email protected]', |
91
|
|
|
'[email protected]', |
92
|
|
|
'[email protected]', |
93
|
|
|
'[email protected]', |
94
|
|
|
'[email protected]', |
95
|
|
|
], |
96
|
|
|
Factory::getArrayForItems($stub) |
97
|
|
|
); |
98
|
|
|
$orig = $coll->toArray(); |
99
|
|
|
$return = $coll->combine($stub); |
100
|
|
|
$this->assertEquals( |
101
|
|
|
[ |
102
|
|
|
'Chelsea' => '[email protected]', |
103
|
|
|
'Adella' => '[email protected]', |
104
|
|
|
'Monte' => '[email protected]', |
105
|
|
|
'Maye' => '[email protected]', |
106
|
|
|
'Lottie' => '[email protected]', |
107
|
|
|
'Don' => '[email protected]', |
108
|
|
|
'Dayton' => '[email protected]', |
109
|
|
|
'Kirk' => '[email protected]', |
110
|
|
|
'Troy' => '[email protected]', |
111
|
|
|
'Nakia' => '[email protected]', |
112
|
|
|
], |
113
|
|
|
$return->toArray() |
114
|
|
|
); |
115
|
|
|
$this->assertSame( |
116
|
|
|
$orig, |
117
|
|
|
$coll->toArray(), |
118
|
|
|
'Ensure that Collection::combine() has not changed the original collection.' |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @expectedException \InvalidArgumentException |
124
|
|
|
* @expectedExceptionMessage Invalid input type for combine. |
125
|
|
|
*/ |
126
|
|
|
public function testCombineThrowsExceptionIfInvalidInput() |
127
|
|
|
{ |
128
|
|
|
$coll = Factory::create($this->fixtures['names']); |
129
|
|
|
$coll->combine('not an array'); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @expectedException \InvalidArgumentException |
134
|
|
|
* @expectedExceptionMessage Invalid input for combine, number of items does not match. |
135
|
|
|
*/ |
136
|
|
|
public function testCombineThrowsExceptionIfIncomingTraversableCountIsNotSameAsCollection() |
137
|
|
|
{ |
138
|
|
|
$coll = Factory::create($this->fixtures['names']); |
139
|
|
|
$coll->combine([1, 2, 3]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: