2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp;
|
2017-01-04 02:10:27 +01:00
|
|
|
use Amp\Producer;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
|
|
|
class ConcatTest extends \PHPUnit_Framework_TestCase {
|
2017-01-04 02:10:27 +01:00
|
|
|
public function getStreams() {
|
2016-12-16 00:28:22 +01:00
|
|
|
return [
|
2017-01-04 02:10:27 +01:00
|
|
|
[[Amp\stream(\range(1, 3)), Amp\stream(\range(4, 6))], \range(1, 6)],
|
|
|
|
[[Amp\stream(\range(1, 5)), Amp\stream(\range(6, 8))], \range(1, 8)],
|
|
|
|
[[Amp\stream(\range(1, 4)), Amp\stream(\range(5, 10))], \range(1, 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-01-04 02:10:27 +01:00
|
|
|
* @dataProvider getStreams
|
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 testConcat(array $streams, array $expected) {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use ($streams, $expected) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$stream = Amp\concat($streams);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
Amp\each($stream, function ($value) use ($expected) {
|
2016-12-16 00:28:22 +01:00
|
|
|
static $i = 0;
|
|
|
|
$this->assertSame($expected[$i++], $value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testConcat
|
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testConcatWithFailedStream() {
|
2016-12-16 00:28:22 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results, &$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(6); // Emit once before failing.
|
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$stream = Amp\concat([Amp\stream(\range(1, 5)), $producer, Amp\stream(\range(7, 10))]);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$stream->listen(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
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-01-04 02:10:27 +01:00
|
|
|
$stream->when($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(\range(1, 6), $results);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Error
|
2017-01-04 02:10:27 +01:00
|
|
|
* @expectedExceptionMessage Non-stream provided
|
2016-12-16 00:28:22 +01:00
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testNonStream() {
|
2016-12-16 00:28:22 +01:00
|
|
|
Amp\concat([1]);
|
|
|
|
}
|
|
|
|
}
|