1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 18:38:17 +01:00
amp/test/Stream/FilterTest.php

107 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2020-05-13 17:15:21 +02:00
namespace Amp\Test\Stream;
2020-05-13 17:15:21 +02:00
use Amp\AsyncGenerator;
use Amp\Loop;
use Amp\PHPUnit\TestException;
2020-05-13 17:15:21 +02:00
use Amp\Stream;
use Amp\StreamSource;
use Amp\Test\BaseTest;
class FilterTest extends BaseTest
2018-06-18 20:00:01 +02:00
{
public function testNoValuesEmitted()
{
$invoked = false;
Loop::run(function () use (&$invoked) {
2020-05-13 17:15:21 +02:00
$source = new StreamSource;
2020-05-13 17:15:21 +02:00
$iterator = Stream\filter($source->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
2020-05-13 17:15:21 +02:00
$this->assertInstanceOf(Stream::class, $iterator);
2020-05-13 17:15:21 +02:00
$source->complete();
});
$this->assertFalse($invoked);
}
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];
$expected = [1, 3];
2020-05-13 17:15:21 +02:00
$generator = new AsyncGenerator(function (callable $yield) use ($values) {
foreach ($values as $value) {
2020-05-13 17:15:21 +02:00
yield $yield($value);
}
});
2020-05-13 17:15:21 +02:00
$iterator = Stream\filter($generator, function ($value) use (&$count) {
++$count;
return $value & 1;
});
2020-05-13 17:15:21 +02:00
while (list($value) = yield $iterator->continue()) {
$this->assertSame(\array_shift($expected), $value);
2017-04-27 17:32:53 +02:00
}
$this->assertSame(3, $count);
});
}
/**
* @depends testValuesEmitted
*/
2018-06-18 20:00:01 +02:00
public function testCallbackThrows()
{
2017-04-27 17:32:53 +02:00
Loop::run(function () {
$values = [1, 2, 3];
$exception = new TestException;
2020-05-13 17:15:21 +02:00
$generator = new AsyncGenerator(function (callable $yield) use ($values) {
foreach ($values as $value) {
2020-05-13 17:15:21 +02:00
yield $yield($value);
}
});
2020-05-13 17:15:21 +02:00
$iterator = Stream\filter($generator, function () use ($exception) {
throw $exception;
});
2017-04-27 17:32:53 +02:00
try {
2020-05-13 17:15:21 +02:00
yield $iterator->continue();
$this->fail("The exception thrown from the filter callback should be thrown from continue()");
} catch (TestException $reason) {
2017-04-27 17:32:53 +02:00
$this->assertSame($reason, $exception);
}
});
}
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;
$exception = new TestException;
2020-05-13 17:15:21 +02:00
$source = new StreamSource;
2020-05-13 17:15:21 +02:00
$stream = Stream\filter($source->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
2020-05-13 17:15:21 +02:00
$source->fail($exception);
2017-04-27 17:32:53 +02:00
try {
2020-05-13 17:15:21 +02:00
yield $stream->continue();
$this->fail("The exception used to fail the iterator should be thrown from continue()");
} catch (TestException $reason) {
2017-04-27 17:32:53 +02:00
$this->assertSame($reason, $exception);
}
2017-04-27 17:32:53 +02:00
$this->assertFalse($invoked);
});
}
}