2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Producer;
|
|
|
|
use Amp\Stream;
|
|
|
|
use Amp\Emitter;
|
|
|
|
use Amp\Loop;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
class StreamMapTest extends \PHPUnit\Framework\TestCase {
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testNoValuesEmitted() {
|
|
|
|
$invoked = false;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked){
|
2017-01-04 02:10:27 +01:00
|
|
|
$emitter = new Emitter;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\map($emitter->stream(), function ($value) use (&$invoked) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$invoked = true;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertInstanceOf(Stream::class, $stream);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$emitter->resolve();
|
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
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testValuesEmitted() {
|
|
|
|
$count = 0;
|
|
|
|
$values = [1, 2, 3];
|
|
|
|
$final = 4;
|
|
|
|
$results = [];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results, &$result, &$count, $values, $final) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($values, $final) {
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($values as $value) {
|
|
|
|
yield $emit($value);
|
|
|
|
}
|
|
|
|
return $final;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\map($producer, function ($value) use (&$count) {
|
2016-12-16 00:28:22 +01:00
|
|
|
++$count;
|
|
|
|
return $value + 1;
|
|
|
|
}, function ($value) use (&$invoked) {
|
|
|
|
return $value + 1;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$stream->listen(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve(function ($exception, $value) use (&$result) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$this->assertSame(\count($values), $count);
|
|
|
|
$this->assertSame(\array_map(function ($value) { return $value + 1; }, $values), $results);
|
|
|
|
$this->assertSame($final + 1, $result);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testValuesEmitted
|
|
|
|
*/
|
|
|
|
public function testOnNextCallbackThrows() {
|
|
|
|
$values = [1, 2, 3];
|
|
|
|
$exception = new \Exception;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$reason, $values, $exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($values) {
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($values as $value) {
|
|
|
|
yield $emit($value);
|
|
|
|
}
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\map($producer, function () use ($exception) {
|
2016-12-16 00:28:22 +01:00
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$stream->listen(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve($callback);
|
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->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testValuesEmitted
|
|
|
|
*/
|
|
|
|
public function testOnCompleteCallbackThrows() {
|
|
|
|
$count = 0;
|
|
|
|
$values = [1, 2, 3];
|
|
|
|
$results = [];
|
|
|
|
$exception = new \Exception;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$reason, &$results, &$count, $values, $exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($values) {
|
2016-12-16 00:28:22 +01:00
|
|
|
foreach ($values as $value) {
|
|
|
|
yield $emit($value);
|
|
|
|
}
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\map($producer, function ($value) use (&$count) {
|
2016-12-16 00:28:22 +01:00
|
|
|
++$count;
|
|
|
|
return $value + 1;
|
|
|
|
}, function ($value) use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$stream->listen(function ($value) use (&$results) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$results[] = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve($callback);
|
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->assertSame(\count($values), $count);
|
|
|
|
$this->assertSame(\array_map(function ($value) { return $value + 1; }, $values), $results);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testStreamFails() {
|
2016-12-16 00:28:22 +01:00
|
|
|
$invoked = false;
|
|
|
|
$exception = new \Exception;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked, &$reason, &$exception){
|
2017-01-04 02:10:27 +01:00
|
|
|
$emitter = new Emitter;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$stream = Stream\map($emitter->stream(), function ($value) use (&$invoked) {
|
2016-12-16 00:28:22 +01:00
|
|
|
$invoked = true;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$emitter->fail($exception);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
$callback = function ($exception, $value) use (&$reason) {
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve($callback);
|
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);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
}
|