Skip to content
This repository was archived by the owner on Dec 28, 2023. It is now read-only.

Commit 363e30f

Browse files
committed
Merge pull request #1 from emonkak/collection-combine
Add Collection::combine()
2 parents 635d077 + 9c5dbed commit 363e30f

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ cache:
1212
- $HOME/.composer/cache
1313

1414
before_script:
15-
- composer install
15+
- composer install --prefer-source
1616

1717
script:
1818
- mkdir -p build/logs
1919
- phpunit --coverage-clover build/logs/clover.xml
2020

2121
after_script:
2222
- php vendor/bin/coveralls
23+
24+
sudo: false

src/Collection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ public static function from($source)
2828
return new Collection($source, self::$defaultProvider);
2929
}
3030

31+
public static function combine($sources)
32+
{
33+
if (!Iterators::isTraversable($sources)) {
34+
$type = gettype($sources);
35+
throw new \InvalidArgumentException("'$type' can not be traversable.");
36+
}
37+
38+
return new Collection(
39+
self::$defaultProvider->concat($sources),
40+
self::$defaultProvider
41+
);
42+
}
43+
3144
public static function range($start, $stop = null, $step = 1)
3245
{
3346
if ($stop === null) {

tests/AbstractCollectionTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,38 @@ public function testFrom()
2828
/**
2929
* @expectedException \InvalidArgumentException
3030
*/
31-
public function testFromNotTraverableSource()
31+
public function testFromThrowsInvalidArgumentException()
3232
{
3333
Collection::from(0);
3434
}
3535

36+
/**
37+
* @dataProvider provideCombine
38+
*/
39+
public function testCombine($sources, $expected)
40+
{
41+
$collection = Collection::combine($sources);
42+
43+
$this->assertSame($expected, $collection->toList());
44+
$this->assertSame(Collection::getDefaultProvider(), $collection->getProvider());
45+
}
46+
47+
public function provideCombine()
48+
{
49+
return [
50+
[[], []],
51+
[[new \ArrayIterator([1, 2]), [3, 4, 5]], [1, 2, 3, 4, 5]]
52+
];
53+
}
54+
55+
/**
56+
* @expectedException \InvalidArgumentException
57+
*/
58+
public function testCombineThrowsInvalidArgumentException()
59+
{
60+
Collection::combine(1);
61+
}
62+
3663
public function tearDown()
3764
{
3865
Collection::setDefaultProvider($this->defaultProvider);
@@ -164,7 +191,7 @@ public function testParMap($factory)
164191
* @dataProvider provideCollectionFactory
165192
* @expectedException \InvalidArgumentException
166193
*/
167-
public function testParMapWhenWorkersAreZeroThrowException($factory)
194+
public function testParMapThrowsInvalidArgumentException($factory)
168195
{
169196
$factory([])->parMap(function($x) { }, 0);
170197
}

tests/ArrayCollectionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ class ArrayCollectionTest extends AbstractCollectionTest
1010
/**
1111
* @expectedException \OverflowException
1212
*/
13-
public function testRepeatOfInfiniteStream()
13+
public function testRepeatThrowsOverflowException()
1414
{
1515
Collection::repeat('foo');
1616
}
1717

1818
/**
1919
* @expectedException \OverflowException
2020
*/
21-
public function testIterate()
21+
public function testIterateThrowsOverflowException()
2222
{
2323
Collection::iterate(2, function($x) { return $x * $x; });
2424
}
@@ -27,7 +27,7 @@ public function testIterate()
2727
* @dataProvider provideCollectionFactory
2828
* @expectedException \OverflowException
2929
*/
30-
public function testCycleOfInfiniteStream($factory)
30+
public function testCycleThrowsOverflowException($factory)
3131
{
3232
$factory([1, 2])->cycle();
3333
}

tests/Selector/PropertySelectorParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function provideParse()
4646
* @dataProvider provideParseInvalidExpr
4747
* @expectedException \InvalidArgumentException
4848
*/
49-
public function testParseInvalidExpr($expr)
49+
public function testParseThrowsInvalidArgumentException($expr)
5050
{
5151
PropertySelectorParser::parse($expr);
5252
}

0 commit comments

Comments
 (0)