1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00
amp/test/MergeTest.php

70 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
use Amp;
use Amp\Loop;
2017-03-14 00:52:57 +01:00
use Amp\Producer;
use Amp\Stream;
class MergeTest 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)], [1, 4, 2, 5, 3, 6]],
[[\range(1, 5), \range(6, 8)], [1, 6, 2, 7, 3, 8, 4, 5]],
[[\range(1, 4), \range(5, 10)], [1, 5, 2, 6, 3, 7, 4, 8, 9, 10]],
];
}
/**
2017-03-14 00:52:57 +01:00
* @dataProvider getArrays
*
2017-01-04 02:10:27 +01:00
* @param array $streams
* @param array $expected
*/
2017-01-04 02:10:27 +01:00
public function testMerge(array $streams, array $expected) {
2017-03-14 00:52:57 +01:00
$streams = \array_map(function (array $stream): Stream {
return Amp\stream($stream);
}, $streams);
2017-03-14 00:52:57 +01:00
$stream = Amp\merge($streams);
Amp\each($stream, function ($value) use ($expected) {
static $i = 0;
$this->assertSame($expected[$i++], $value);
});
2017-03-14 00:52:57 +01:00
Loop::run();
}
/**
* @depends testMerge
*/
2017-01-04 02:10:27 +01:00
public function testMergeWithFailedStream() {
$exception = new \Exception;
Loop::run(function () use (&$reason, $exception) {
2017-01-04 02:10:27 +01:00
$producer = new Producer(function (callable $emit) use ($exception) {
yield $emit(1); // Emit once before failing.
throw $exception;
});
2017-01-04 02:10:27 +01:00
$stream = Amp\merge([$producer, Amp\stream(\range(1, 5))]);
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2017-01-04 02:10:27 +01:00
$stream->when($callback);
});
$this->assertSame($exception, $reason);
}
/**
2017-03-14 18:39:53 +01:00
* @expectedException \Amp\UnionTypeError
*/
2017-01-04 02:10:27 +01:00
public function testNonStream() {
Amp\merge([1]);
}
}