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
|
|
|
|
|
|
|
class FilterTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
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-01-04 02:10:27 +01:00
|
|
|
$stream = Amp\filter($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];
|
|
|
|
$results = [];
|
|
|
|
$expected = [1, 3];
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$results, &$result, &$count, $values) {
|
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-01-04 02:10:27 +01:00
|
|
|
$stream = Amp\filter($producer, function ($value) use (&$count) {
|
2016-12-16 00:28:22 +01:00
|
|
|
++$count;
|
|
|
|
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-01-04 02:10:27 +01:00
|
|
|
$stream->when(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($expected, $results);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
/**
|
|
|
|
* @depends testValuesEmitted
|
|
|
|
*/
|
|
|
|
public function testCallbackThrows() {
|
|
|
|
$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-01-04 02:10:27 +01:00
|
|
|
$stream = Amp\filter($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-01-04 02:10:27 +01:00
|
|
|
$stream->when($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
|
|
|
|
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-01-04 02:10:27 +01:00
|
|
|
$stream = Amp\filter($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-01-04 02:10:27 +01:00
|
|
|
$stream->when($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);
|
|
|
|
}
|
|
|
|
}
|