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\Emitter;
|
|
|
|
use Amp\Loop;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Producer;
|
|
|
|
use Amp\Stream;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2017-03-11 14:57:03 +01:00
|
|
|
class FilterTest extends \PHPUnit\Framework\TestCase {
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testNoValuesEmitted() {
|
|
|
|
$invoked = false;
|
2017-04-24 15:39:08 +02: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\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-04-27 17:32:53 +02:00
|
|
|
$emitter->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
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
public function testValuesEmitted() {
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$count = 0;
|
|
|
|
$values = [1, 2, 3];
|
|
|
|
$expected = [1, 3];
|
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\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-04-27 17:32:53 +02:00
|
|
|
while (yield $stream->advance()) {
|
|
|
|
$this->assertSame(\array_shift($expected), $stream->getCurrent());
|
|
|
|
}
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function testCallbackThrows() {
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$values = [1, 2, 3];
|
|
|
|
$exception = new \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\filter($producer, 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 {
|
|
|
|
while (yield $stream->advance()) {
|
|
|
|
$stream->getCurrent();
|
|
|
|
}
|
|
|
|
} catch (\Exception $reason) {
|
|
|
|
$this->assertSame($reason, $exception);
|
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testStreamFails() {
|
2017-04-27 17:32:53 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$invoked = false;
|
|
|
|
$exception = new \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\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
|
|
|
|
2017-04-27 17:32:53 +02:00
|
|
|
try {
|
|
|
|
while (yield $stream->advance()) {
|
|
|
|
$stream->getCurrent();
|
|
|
|
}
|
|
|
|
} catch (\Exception $reason) {
|
|
|
|
$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
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
}
|