1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00
amp/test/ConcatTest.php

73 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
2017-04-27 17:51:06 +02:00
use Amp\Iterator;
use Amp\Loop;
use Amp\PHPUnit\TestException;
2017-03-14 00:52:57 +01:00
use Amp\Producer;
class ConcatTest extends \PHPUnit\Framework\TestCase {
2017-03-14 00:52:57 +01:00
public function getArrays() {
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
*/
public function testConcat(array $iterators, array $expected) {
Loop::run(function () use ($iterators, $expected) {
$iterators = \array_map(function (array $iterator): Iterator {
return Iterator\fromIterable($iterator);
}, $iterators);
$iterator = Iterator\concat($iterators);
2017-03-14 00:52:57 +01:00
while (yield $iterator->advance()) {
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
2017-04-27 17:32:53 +02:00
}
});
}
/**
* @depends testConcat
*/
public function testConcatWithFailedIterator() {
2017-04-27 17:32:53 +02:00
Loop::run(function () {
$exception = new TestException;
2017-04-27 17:32:53 +02:00
$expected = \range(1, 6);
2017-01-04 02:10:27 +01:00
$producer = new Producer(function (callable $emit) use ($exception) {
yield $emit(6); // Emit once before failing.
throw $exception;
});
$iterator = Iterator\concat([Iterator\fromIterable(\range(1, 5)), $producer, Iterator\fromIterable(\range(7, 10))]);
2017-04-27 17:32:53 +02:00
try {
while (yield $iterator->advance()) {
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
2017-04-27 17:32:53 +02:00
}
$this->fail("The exception used to fail the iterator should be thrown from advance()");
} catch (TestException $reason) {
2017-04-27 17:32:53 +02:00
$this->assertSame($exception, $reason);
}
2017-04-27 17:32:53 +02:00
$this->assertEmpty($expected);
});
}
/**
2017-04-23 19:08:40 +02:00
* @expectedException \TypeError
*/
public function testNonIterator() {
2017-04-27 17:51:06 +02:00
Iterator\concat([1]);
}
}