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

72 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
use Amp;
2017-01-04 02:10:27 +01:00
use Amp\Producer;
use Amp\Loop;
class ConcatTest extends \PHPUnit_Framework_TestCase {
2017-01-04 02:10:27 +01:00
public function getStreams() {
return [
2017-01-04 02:10:27 +01:00
[[Amp\stream(\range(1, 3)), Amp\stream(\range(4, 6))], \range(1, 6)],
[[Amp\stream(\range(1, 5)), Amp\stream(\range(6, 8))], \range(1, 8)],
[[Amp\stream(\range(1, 4)), Amp\stream(\range(5, 10))], \range(1, 10)],
];
}
/**
2017-01-04 02:10:27 +01:00
* @dataProvider getStreams
*
2017-01-04 02:10:27 +01:00
* @param array $streams
* @param array $expected
*/
2017-01-04 02:10:27 +01:00
public function testConcat(array $streams, array $expected) {
Loop::run(function () use ($streams, $expected) {
2017-01-04 02:10:27 +01:00
$stream = Amp\concat($streams);
2017-01-04 02:10:27 +01:00
Amp\each($stream, function ($value) use ($expected) {
static $i = 0;
$this->assertSame($expected[$i++], $value);
});
});
}
/**
* @depends testConcat
*/
2017-01-04 02:10:27 +01:00
public function testConcatWithFailedStream() {
$exception = new \Exception;
$results = [];
Loop::run(function () use (&$results, &$reason, $exception) {
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;
});
2017-01-04 02:10:27 +01:00
$stream = Amp\concat([Amp\stream(\range(1, 5)), $producer, Amp\stream(\range(7, 10))]);
2017-01-04 02:10:27 +01:00
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2017-01-04 02:10:27 +01:00
$stream->when($callback);
});
$this->assertSame(\range(1, 6), $results);
$this->assertSame($exception, $reason);
}
/**
* @expectedException \Error
2017-01-04 02:10:27 +01:00
* @expectedExceptionMessage Non-stream provided
*/
2017-01-04 02:10:27 +01:00
public function testNonStream() {
Amp\concat([1]);
}
}