2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\Pause;
|
2017-03-15 17:12:49 +01:00
|
|
|
use Amp\Stream;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Success;
|
|
|
|
use Amp\Loop;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
class StreamFromIterableTest extends \PHPUnit\Framework\TestCase {
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testSuccessfulPromises() {
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results) {
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\fromIterable([new Success(1), new Success(2), new Success(3)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$stream->onEmit(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame([1, 2, 3], $results);
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testFailedPromises() {
|
|
|
|
$exception = new \Exception;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$reason, $exception) {
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\fromIterable([new Failure($exception), new Failure($exception)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve($callback);
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testMixedPromises() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results, &$reason, $exception) {
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\fromIterable([new Success(1), new Success(2), new Failure($exception), new Success(4)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$stream->onEmit(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve($callback);
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertSame(\range(1, 2), $results);
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testPendingPromises() {
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results) {
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\fromIterable([new Pause(30, 1), new Pause(10, 2), new Pause(20, 3), new Success(4)]);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$stream->onEmit(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertSame(\range(1, 4), $results);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testTraversable() {
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$generator = (function () {
|
|
|
|
foreach (\range(1, 4) as $value) {
|
|
|
|
yield $value;
|
|
|
|
}
|
|
|
|
})();
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\fromIterable($generator);
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$stream->onEmit(function ($value) use (&$results) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
|
|
|
});
|
2017-01-06 23:16:06 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertSame(\range(1, 4), $results);
|
|
|
|
}
|
2017-01-06 23:16:06 +01:00
|
|
|
|
|
|
|
/**
|
2017-03-14 18:39:53 +01:00
|
|
|
* @expectedException \Amp\UnionTypeError
|
2017-01-06 23:16:06 +01:00
|
|
|
* @dataProvider provideInvalidStreamArguments
|
|
|
|
*/
|
|
|
|
public function testInvalid($arg) {
|
2017-03-15 17:12:49 +01:00
|
|
|
Stream\fromIterable($arg);
|
2017-01-06 23:16:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideInvalidStreamArguments() {
|
|
|
|
return [
|
|
|
|
[null],
|
|
|
|
[new \stdClass],
|
|
|
|
[32],
|
|
|
|
[false],
|
|
|
|
[true],
|
|
|
|
["string"],
|
|
|
|
];
|
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|