2016-12-29 14:09:49 -06:00
|
|
|
<?php
|
2016-12-15 17:28:22 -06:00
|
|
|
|
2020-05-13 10:15:21 -05:00
|
|
|
namespace Amp\Test\Stream;
|
2016-12-15 17:28:22 -06:00
|
|
|
|
2020-05-13 10:15:21 -05:00
|
|
|
use Amp\AsyncGenerator;
|
2020-05-17 21:41:42 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-04-27 23:53:04 -05:00
|
|
|
use Amp\PHPUnit\TestException;
|
2020-05-13 10:15:21 -05:00
|
|
|
use Amp\Stream;
|
|
|
|
use Amp\StreamSource;
|
2016-12-15 17:28:22 -06:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
class MapTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
|
|
|
public function testNoValuesEmitted()
|
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$source = new StreamSource;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */
|
|
|
|
$stream = Stream\map($source->stream(), $this->createCallback(0));
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
$source->complete();
|
2016-12-15 17:28:22 -06:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testValuesEmitted()
|
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$count = 0;
|
|
|
|
$values = [1, 2, 3];
|
|
|
|
$generator = new AsyncGenerator(static function (callable $yield) use ($values) {
|
|
|
|
foreach ($values as $value) {
|
|
|
|
yield $yield($value);
|
2017-04-27 10:32:53 -05:00
|
|
|
}
|
2020-05-17 21:41:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$stream = Stream\map($generator, static function ($value) use (&$count) {
|
|
|
|
++$count;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
return $value + 1;
|
2016-12-15 17:28:22 -06:00
|
|
|
});
|
2020-05-17 21:41:42 +02:00
|
|
|
|
|
|
|
while (list($value) = yield $stream->continue()) {
|
|
|
|
$this->assertSame(\array_shift($values) + 1, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame(3, $count);
|
2016-12-15 17:28:22 -06:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-15 17:28:22 -06:00
|
|
|
/**
|
|
|
|
* @depends testValuesEmitted
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnNextCallbackThrows()
|
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$values = [1, 2, 3];
|
|
|
|
$exception = new TestException;
|
|
|
|
|
|
|
|
$generator = new AsyncGenerator(static function (callable $yield) use ($values) {
|
|
|
|
foreach ($values as $value) {
|
|
|
|
yield $yield($value);
|
2017-04-27 10:32:53 -05:00
|
|
|
}
|
2016-12-15 17:28:22 -06:00
|
|
|
});
|
2020-05-17 21:41:42 +02:00
|
|
|
|
|
|
|
$stream = Stream\map($generator, static function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->expectExceptionObject($exception);
|
|
|
|
|
|
|
|
yield $stream->continue();
|
2016-12-15 17:28:22 -06:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 10:15:21 -05:00
|
|
|
public function testStreamFails()
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$exception = new TestException;
|
|
|
|
$source = new StreamSource;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
$iterator = Stream\map($source->stream(), $this->createCallback(0));
|
|
|
|
|
|
|
|
$source->fail($exception);
|
|
|
|
|
|
|
|
$this->expectExceptionObject($exception);
|
|
|
|
|
|
|
|
yield $iterator->continue();
|
2016-12-15 17:28:22 -06:00
|
|
|
}
|
|
|
|
}
|