mirror of
https://github.com/danog/amp.git
synced 2024-12-12 17:37:34 +01:00
84 lines
1.9 KiB
PHP
84 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Amp\Test\Stream;
|
|
|
|
use Amp\AsyncGenerator;
|
|
use Amp\PHPUnit\TestException;
|
|
use Amp\Stream;
|
|
use Amp\StreamSource;
|
|
use Amp\Test\BaseTest;
|
|
|
|
class FilterTest extends BaseTest
|
|
{
|
|
public function testNoValuesEmitted()
|
|
{
|
|
$source = new StreamSource;
|
|
|
|
$stream = Stream\filter($source->stream(), $this->createCallback(0));
|
|
|
|
$source->complete();
|
|
|
|
yield Stream\discard($stream);
|
|
}
|
|
|
|
public function testValuesEmitted()
|
|
{
|
|
$count = 0;
|
|
$values = [1, 2, 3];
|
|
$expected = [1, 3];
|
|
$generator = new AsyncGenerator(static function (callable $yield) use ($values) {
|
|
foreach ($values as $value) {
|
|
yield $yield($value);
|
|
}
|
|
});
|
|
|
|
$iterator = Stream\filter($generator, static function ($value) use (&$count) {
|
|
++$count;
|
|
|
|
return $value & 1;
|
|
});
|
|
|
|
while (list($value) = yield $iterator->continue()) {
|
|
$this->assertSame(\array_shift($expected), $value);
|
|
}
|
|
|
|
$this->assertSame(3, $count);
|
|
}
|
|
|
|
/**
|
|
* @depends testValuesEmitted
|
|
*/
|
|
public function testCallbackThrows()
|
|
{
|
|
$values = [1, 2, 3];
|
|
$exception = new TestException;
|
|
$generator = new AsyncGenerator(static function (callable $yield) use ($values) {
|
|
foreach ($values as $value) {
|
|
yield $yield($value);
|
|
}
|
|
});
|
|
|
|
$stream = Stream\filter($generator, static function () use ($exception) {
|
|
throw $exception;
|
|
});
|
|
|
|
$this->expectExceptionObject($exception);
|
|
|
|
yield $stream->continue();
|
|
}
|
|
|
|
public function testStreamFails()
|
|
{
|
|
$exception = new TestException;
|
|
$source = new StreamSource;
|
|
|
|
$stream = Stream\filter($source->stream(), $this->createCallback(0));
|
|
|
|
$source->fail($exception);
|
|
|
|
$this->expectExceptionObject($exception);
|
|
|
|
yield $stream->continue();
|
|
}
|
|
}
|