continue()) { $this->assertSame(\array_shift($expected), $value); } } /** * @depends testMerge */ public function testMergeWithDelayedYields() { $streams = []; $values1 = [new Delayed(10, 1), new Delayed(50, 2), new Delayed(70, 3)]; $values2 = [new Delayed(20, 4), new Delayed(40, 5), new Delayed(60, 6)]; $expected = [1, 4, 5, 2, 6, 3]; $streams[] = new AsyncGenerator(function (callable $yield) use ($values1) { foreach ($values1 as $value) { yield $yield(yield $value); } }); $streams[] = new AsyncGenerator(function (callable $yield) use ($values2) { foreach ($values2 as $value) { yield $yield(yield $value); } }); $stream = Stream\merge($streams); while (null !== $value = yield $stream->continue()) { $this->assertSame(\array_shift($expected), $value); } } /** * @depends testMerge */ public function testMergeWithFailedStream() { $exception = new TestException; $generator = new AsyncGenerator(static function (callable $yield) use ($exception) { yield $yield(1); // Emit once before failing. throw $exception; }); $stream = Stream\merge([$generator, Stream\fromIterable(\range(1, 5))]); try { /** @noinspection PhpStatementHasEmptyBodyInspection */ while (yield $stream->continue()) { ; } $this->fail("The exception used to fail the stream should be thrown from continue()"); } catch (TestException $reason) { $this->assertSame($exception, $reason); } } public function testNonStream() { $this->expectException(\TypeError::class); /** @noinspection PhpParamsInspection */ Stream\merge([1]); } }