File tree Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -186,22 +186,45 @@ public function testGetThrowsNotFound(): void
186
186
$ collection ->get ('invalid ' );
187
187
}
188
188
189
- public function testReduce ()
189
+ public function testReduce (): void
190
190
{
191
191
$ collection = StringCollection::create (['foo ' , 'bar ' ]);
192
192
193
193
$ this ->assertEquals ('foobar ' , $ collection ->reduce (function (?string $ total , $ value ) {
194
194
return $ total . $ value ;
195
195
}));
196
196
}
197
+
198
+ public function testMergeIsTypeSafe (): void
199
+ {
200
+ $ this ->assertInstanceOf (
201
+ StringCollection::class,
202
+ StringCollection::create (['foo ' ])->merge (StringCollection::create (['bar ' ]))
203
+ );
204
+
205
+ $ this ->expectException (InvalidArgumentException::class);
206
+ StringCollection::create (['foo ' ])->merge (IntegerCollection::create ([1 ]));
207
+ }
197
208
}
198
209
199
210
final class StringCollection extends ImmutableCollection
200
211
{
201
212
protected function validateItems (array $ elements ): void
202
213
{
203
214
foreach ($ elements as $ item ) {
204
- if (!is_string ($ item )) {
215
+ if (!\is_string ($ item )) {
216
+ throw new InvalidArgumentException ('Invalid value ' );
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ final class IntegerCollection extends ImmutableCollection
223
+ {
224
+ protected function validateItems (array $ elements ): void
225
+ {
226
+ foreach ($ elements as $ item ) {
227
+ if (!\is_int ($ item )) {
205
228
throw new InvalidArgumentException ('Invalid value ' );
206
229
}
207
230
}
You can’t perform that action at this time.
0 commit comments