2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2017-03-14 00:52:57 +01:00
|
|
|
use Amp\Producer;
|
|
|
|
use Amp\Stream;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2017-03-11 14:57:03 +01:00
|
|
|
class MergeTest extends \PHPUnit\Framework\TestCase {
|
2017-03-14 00:52:57 +01:00
|
|
|
public function getArrays() {
|
2016-12-16 00:28:22 +01:00
|
|
|
return [
|
2017-03-14 00:52:57 +01:00
|
|
|
[[\range(1, 3), \range(4, 6)], [1, 4, 2, 5, 3, 6]],
|
|
|
|
[[\range(1, 5), \range(6, 8)], [1, 6, 2, 7, 3, 8, 4, 5]],
|
|
|
|
[[\range(1, 4), \range(5, 10)], [1, 5, 2, 6, 3, 7, 4, 8, 9, 10]],
|
2016-12-16 00:28:22 +01:00
|
|
|
];
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
2017-03-14 00:52:57 +01:00
|
|
|
* @dataProvider getArrays
|
2016-12-16 00:28:22 +01:00
|
|
|
*
|
2017-01-04 02:10:27 +01:00
|
|
|
* @param array $streams
|
2016-12-16 00:28:22 +01:00
|
|
|
* @param array $expected
|
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testMerge(array $streams, array $expected) {
|
2017-03-14 00:52:57 +01:00
|
|
|
$streams = \array_map(function (array $stream): Stream {
|
2017-03-15 17:12:49 +01:00
|
|
|
return Stream\fromIterable($stream);
|
2017-03-14 00:52:57 +01:00
|
|
|
}, $streams);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\merge($streams);
|
2017-03-14 00:52:57 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
Stream\map($stream, function ($value) use ($expected) {
|
2017-03-14 00:52:57 +01:00
|
|
|
static $i = 0;
|
|
|
|
$this->assertSame($expected[$i++], $value);
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2017-03-14 00:52:57 +01:00
|
|
|
|
|
|
|
Loop::run();
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testMerge
|
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testMergeWithFailedStream() {
|
2016-12-16 00:28:22 +01:00
|
|
|
$exception = new \Exception;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$reason, $exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($exception) {
|
2016-12-16 00:28:22 +01:00
|
|
|
yield $emit(1); // Emit once before failing.
|
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\merge([$producer, Stream\fromIterable(\range(1, 5))]);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve($callback);
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
2017-04-23 19:08:40 +02:00
|
|
|
* @expectedException \TypeError
|
2016-12-16 00:28:22 +01:00
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testNonStream() {
|
2017-03-15 17:12:49 +01:00
|
|
|
Stream\merge([1]);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
}
|