2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
namespace Amp\Test\Stream;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
use Amp\AsyncGenerator;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2017-04-28 06:53:04 +02:00
|
|
|
use Amp\PHPUnit\TestException;
|
2020-05-13 17:15:21 +02:00
|
|
|
use Amp\Stream;
|
|
|
|
use Amp\StreamSource;
|
|
|
|
use Amp\Test\BaseTest;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
class MapTest extends BaseTest
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
|
|
|
public function testNoValuesEmitted()
|
|
|
|
{
|
2016-12-16 00:28:22 +01:00
|
|
|
$invoked = false;
|
2017-04-24 15:39:08 +02:00
|
|
|
Loop::run(function () use (&$invoked) {
|
2020-05-13 17:15:21 +02:00
|
|
|
$source = new StreamSource;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
$iterator = Stream\map($source->stream(), function ($value) use (&$invoked) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$invoked = true;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
$this->assertInstanceOf(Stream::class, $iterator);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
$source->complete();
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertFalse($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testValuesEmitted()
|
|
|
|
{
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$count = 0;
|
|
|
|
$values = [1, 2, 3];
|
2020-05-13 17:15:21 +02:00
|
|
|
$generator = new AsyncGenerator(function (callable $yield) use ($values) {
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($values as $value) {
|
2020-05-13 17:15:21 +02:00
|
|
|
yield $yield($value);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
$iterator = Stream\map($generator, function ($value) use (&$count) {
|
2016-12-16 00:28:22 +01:00
|
|
|
++$count;
|
|
|
|
return $value + 1;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
while (list($value) = yield $iterator->continue()) {
|
|
|
|
$this->assertSame(\array_shift($values) + 1, $value);
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertSame(3, $count);
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testValuesEmitted
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnNextCallbackThrows()
|
|
|
|
{
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$values = [1, 2, 3];
|
2017-04-28 06:53:04 +02:00
|
|
|
$exception = new TestException;
|
2017-04-27 17:32:53 +02:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
$generator = new AsyncGenerator(function (callable $yield) use ($values) {
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($values as $value) {
|
2020-05-13 17:15:21 +02:00
|
|
|
yield $yield($value);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
$iterator = Stream\map($generator, function () use ($exception) {
|
2016-12-16 00:28:22 +01:00
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
try {
|
2020-05-13 17:15:21 +02:00
|
|
|
yield $iterator->continue();
|
2017-04-28 06:53:04 +02:00
|
|
|
$this->fail("The exception thrown from the map callback should be thrown from advance()");
|
|
|
|
} catch (TestException $reason) {
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertSame($reason, $exception);
|
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
public function testStreamFails()
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$invoked = false;
|
2017-04-28 06:53:04 +02:00
|
|
|
$exception = new TestException;
|
2020-05-13 17:15:21 +02:00
|
|
|
$source = new StreamSource;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
$iterator = Stream\map($source->stream(), function ($value) use (&$invoked) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$invoked = true;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
$source->fail($exception);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
try {
|
2020-05-13 17:15:21 +02:00
|
|
|
yield $iterator->continue();
|
2017-04-28 06:53:04 +02:00
|
|
|
$this->fail("The exception used to fail the iterator should be thrown from advance()");
|
|
|
|
} catch (TestException $reason) {
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertSame($reason, $exception);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
$this->assertFalse($invoked);
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|