1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 13:21:16 +01:00
amp/test/ConcatTest.php

75 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
use Amp\Loop;
2017-03-13 18:52:57 -05:00
use Amp\Producer;
use Amp\Stream;
class ConcatTest extends \PHPUnit\Framework\TestCase {
2017-03-13 18:52:57 -05:00
public function getArrays() {
return [
2017-03-13 18:52:57 -05: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-13 18:52:57 -05:00
* @dataProvider getArrays
*
2017-01-03 19:10:27 -06:00
* @param array $streams
* @param array $expected
*/
2017-01-03 19:10:27 -06:00
public function testConcat(array $streams, array $expected) {
2017-03-13 18:52:57 -05:00
$streams = \array_map(function (array $stream): Stream {
return Stream\fromIterable($stream);
2017-03-13 18:52:57 -05:00
}, $streams);
$stream = Stream\concat($streams);
2017-03-13 18:52:57 -05:00
Stream\map($stream, function ($value) use ($expected) {
2017-03-13 18:52:57 -05:00
static $i = 0;
$this->assertSame($expected[$i++], $value);
});
2017-03-13 18:52:57 -05:00
Loop::run();
}
/**
* @depends testConcat
*/
2017-01-03 19:10:27 -06:00
public function testConcatWithFailedStream() {
$exception = new \Exception;
$results = [];
Loop::run(function () use (&$results, &$reason, $exception) {
2017-01-03 19:10:27 -06:00
$producer = new Producer(function (callable $emit) use ($exception) {
yield $emit(6); // Emit once before failing.
throw $exception;
});
$stream = Stream\concat([Stream\fromIterable(\range(1, 5)), $producer, Stream\fromIterable(\range(7, 10))]);
2017-01-03 19:10:27 -06:00
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2017-01-03 19:10:27 -06:00
$stream->when($callback);
});
$this->assertSame(\range(1, 6), $results);
$this->assertSame($exception, $reason);
}
/**
2017-03-14 12:39:53 -05:00
* @expectedException \Amp\UnionTypeError
*/
2017-01-03 19:10:27 -06:00
public function testNonStream() {
Stream\concat([1]);
}
}