2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
namespace Amp\Test\Pipeline;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
use Amp\AsyncGenerator;
|
2020-05-17 21:41:42 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-04-28 06:53:04 +02:00
|
|
|
use Amp\PHPUnit\TestException;
|
2020-08-23 16:18:28 +02:00
|
|
|
use Amp\Pipeline;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
class ConcatTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
public function getArrays(): array
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 00:28:22 +01:00
|
|
|
return [
|
2017-03-14 00:52:57 +01: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-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
|
|
|
*
|
2020-09-28 05:19:52 +02:00
|
|
|
* @param array $array
|
2016-12-16 00:28:22 +01:00
|
|
|
* @param array $expected
|
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testConcat(array $array, array $expected): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$pipelines = \array_map(static function (iterable $iterable): Pipeline {
|
|
|
|
return Pipeline\fromIterable($iterable);
|
|
|
|
}, $array);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$pipeline = Pipeline\concat($pipelines);
|
2017-03-14 00:52:57 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
while (null !== $value = $pipeline->continue()) {
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertSame(\array_shift($expected), $value);
|
2020-05-17 21:41:42 +02:00
|
|
|
}
|
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 testConcat
|
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testConcatWithFailedPipeline(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$exception = new TestException;
|
|
|
|
$expected = \range(1, 6);
|
2020-09-28 05:19:52 +02:00
|
|
|
$generator = new AsyncGenerator(static function () use ($exception) {
|
|
|
|
yield 6; // Emit once before failing.
|
2020-05-17 21:41:42 +02:00
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = Pipeline\concat([
|
|
|
|
Pipeline\fromIterable(\range(1, 5)),
|
2020-05-17 21:41:42 +02:00
|
|
|
$generator,
|
2020-08-23 16:18:28 +02:00
|
|
|
Pipeline\fromIterable(\range(7, 10)),
|
2020-05-17 21:41:42 +02:00
|
|
|
]);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
try {
|
2020-09-28 05:19:52 +02:00
|
|
|
while (null !== $value = $pipeline->continue()) {
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertSame(\array_shift($expected), $value);
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->fail("The exception used to fail the pipeline should be thrown from continue()");
|
2020-05-17 21:41:42 +02:00
|
|
|
} catch (TestException $reason) {
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEmpty($expected);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testNonPipeline(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-13 17:15:21 +02:00
|
|
|
$this->expectException(\TypeError::class);
|
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
/** @noinspection PhpParamsInspection */
|
2020-08-23 16:18:28 +02:00
|
|
|
Pipeline\concat([1]);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
}
|