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