advance()) { $this->assertSame(\array_shift($expected), $iterator->getCurrent()); } } /** * @depends testMerge */ public function testMergeWithDelayedEmits(): \Generator { $iterators = []; $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]; $iterators[] = new Producer(function (callable $emit) use ($values1) { foreach ($values1 as $value) { yield $emit($value); } }); $iterators[] = new Producer(function (callable $emit) use ($values2) { foreach ($values2 as $value) { yield $emit($value); } }); $iterator = Iterator\merge($iterators); while (yield $iterator->advance()) { $this->assertSame(\array_shift($expected), $iterator->getCurrent()); } } /** * @depends testMerge */ public function testMergeWithFailedIterator(): \Generator { $exception = new TestException; $producer = new Producer(function (callable $emit) use ($exception) { yield $emit(1); // Emit once before failing. throw $exception; }); $iterator = Iterator\merge([$producer, Iterator\fromIterable(\range(1, 5))]); try { while (yield $iterator->advance()) ; $this->fail("The exception used to fail the iterator should be thrown from advance()"); } catch (TestException $reason) { $this->assertSame($exception, $reason); } } public function testNonIterator() { $this->expectException(\TypeError::class); Iterator\merge([1]); } }