1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 13:21:16 +01:00
amp/test/ConcatTest.php
2016-12-15 17:31:57 -06:00

72 lines
2.0 KiB
PHP

<?php declare(strict_types = 1);
namespace Amp\Test;
use Amp;
use Amp\Emitter;
use Interop\Async\Loop;
class ConcatTest extends \PHPUnit_Framework_TestCase {
public function getObservables() {
return [
[[Amp\range(1, 3), Amp\range(4, 6)], \range(1, 6)],
[[Amp\range(1, 5), Amp\range(6, 8)], \range(1, 8)],
[[Amp\range(1, 4), Amp\range(5, 10)], \range(1, 10)],
];
}
/**
* @dataProvider getObservables
*
* @param array $observables
* @param array $expected
*/
public function testConcat(array $observables, array $expected) {
Loop::execute(function () use ($observables, $expected) {
$observable = Amp\concat($observables);
Amp\each($observable, function ($value) use ($expected) {
static $i = 0;
$this->assertSame($expected[$i++], $value);
});
});
}
/**
* @depends testConcat
*/
public function testConcatWithFailedObservable() {
$exception = new \Exception;
$results = [];
Loop::execute(function () use (&$results, &$reason, $exception) {
$emitter = new Emitter(function (callable $emit) use ($exception) {
yield $emit(6); // Emit once before failing.
throw $exception;
});
$observable = Amp\concat([Amp\range(1, 5), $emitter, Amp\range(7, 10)]);
$observable->subscribe(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$observable->when($callback);
});
$this->assertSame(\range(1, 6), $results);
$this->assertSame($exception, $reason);
}
/**
* @expectedException \Error
* @expectedExceptionMessage Non-observable provided
*/
public function testNonObservable() {
Amp\concat([1]);
}
}