1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 18:38:17 +01:00
amp/test/Stream/ConcatTest.php

79 lines
2.0 KiB
PHP
Raw Normal View History

<?php
2020-05-13 17:15:21 +02:00
namespace Amp\Test\Stream;
2020-05-13 17:15:21 +02:00
use Amp\AsyncGenerator;
2020-05-17 21:41:42 +02:00
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
2020-05-13 17:15:21 +02:00
use Amp\Stream;
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
{
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)],
];
}
/**
2017-03-14 00:52:57 +01:00
* @dataProvider getArrays
*
* @param array $iterators
* @param array $expected
*/
2018-06-18 20:00:01 +02:00
public function testConcat(array $iterators, array $expected)
{
2020-05-17 21:41:42 +02:00
$iterators = \array_map(static function (array $iterator): Stream {
return Stream\fromIterable($iterator);
}, $iterators);
2020-05-17 21:41:42 +02:00
$stream = Stream\concat($iterators);
2017-03-14 00:52:57 +01:00
2020-05-21 17:11:22 +02:00
while (null !== $value = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
2020-05-17 21:41:42 +02:00
}
}
/**
* @depends testConcat
*/
2020-05-13 17:15:21 +02:00
public function testConcatWithFailedStream()
2018-06-18 20:00:01 +02:00
{
2020-05-17 21:41:42 +02:00
$exception = new TestException;
$expected = \range(1, 6);
$generator = new AsyncGenerator(static function (callable $yield) use ($exception) {
yield $yield(6); // Emit once before failing.
throw $exception;
});
2020-05-17 21:41:42 +02:00
$stream = Stream\concat([
Stream\fromIterable(\range(1, 5)),
$generator,
Stream\fromIterable(\range(7, 10)),
]);
2020-05-17 21:41:42 +02:00
try {
2020-05-21 17:11:22 +02:00
while (null !== $value = yield $stream->continue()) {
$this->assertSame(\array_shift($expected), $value);
2017-04-27 17:32:53 +02:00
}
2020-05-17 21:41:42 +02:00
$this->fail("The exception used to fail the stream should be thrown from continue()");
} catch (TestException $reason) {
$this->assertSame($exception, $reason);
}
$this->assertEmpty($expected);
}
2020-05-13 17:15:21 +02:00
public function testNonStream()
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-05-13 17:15:21 +02:00
Stream\concat([1]);
}
}