2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
namespace Amp\Test\Pipeline;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
use Amp\AsyncGenerator;
|
2020-05-18 20:49:56 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-04-28 06:53:04 +02:00
|
|
|
use Amp\PHPUnit\TestException;
|
2020-08-23 16:18:28 +02:00
|
|
|
use Amp\Pipeline;
|
|
|
|
use Amp\PipelineSource;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-05-18 20:49:56 +02:00
|
|
|
class FilterTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testNoValuesEmitted(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-08-23 16:18:28 +02:00
|
|
|
$source = new PipelineSource;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = Pipeline\filter($source->pipe(), $this->createCallback(0));
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-05-17 21:41:42 +02:00
|
|
|
$source->complete();
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2021-04-04 20:10:23 +02:00
|
|
|
Pipeline\discard($pipeline)->join();
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testValuesEmitted(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$count = 0;
|
|
|
|
$values = [1, 2, 3];
|
|
|
|
$expected = [1, 3];
|
2020-09-28 05:19:52 +02:00
|
|
|
$generator = new AsyncGenerator(static function () use ($values) {
|
2020-05-17 21:41:42 +02:00
|
|
|
foreach ($values as $value) {
|
2020-09-28 05:19:52 +02:00
|
|
|
yield $value;
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2020-05-17 21:41:42 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = Pipeline\filter($generator, static function ($value) use (&$count) {
|
2020-05-17 21:41:42 +02:00
|
|
|
++$count;
|
|
|
|
|
|
|
|
return $value & 1;
|
|
|
|
});
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
while (null !== $value = $pipeline->continue()) {
|
2021-03-26 22:34:32 +01:00
|
|
|
self::assertSame(\array_shift($expected), $value);
|
2020-05-17 21:41:42 +02:00
|
|
|
}
|
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
self::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
|
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testCallbackThrows(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$values = [1, 2, 3];
|
|
|
|
$exception = new TestException;
|
2020-09-28 05:19:52 +02:00
|
|
|
$generator = new AsyncGenerator(static function () use ($values) {
|
2020-05-17 21:41:42 +02:00
|
|
|
foreach ($values as $value) {
|
2020-09-28 05:19:52 +02:00
|
|
|
yield $value;
|
2017-04-27 17:32:53 +02:00
|
|
|
}
|
2016-12-16 00:28:22 +01:00
|
|
|
});
|
2020-05-17 21:41:42 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = Pipeline\filter($generator, static function () use ($exception) {
|
2020-05-17 21:41:42 +02:00
|
|
|
throw $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->expectExceptionObject($exception);
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$pipeline->continue();
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testPipelineFails(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-05-17 21:41:42 +02:00
|
|
|
$exception = new TestException;
|
2020-08-23 16:18:28 +02:00
|
|
|
$source = new PipelineSource;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = Pipeline\filter($source->pipe(), $this->createCallback(0));
|
2020-05-17 21:41:42 +02:00
|
|
|
|
|
|
|
$source->fail($exception);
|
|
|
|
|
|
|
|
$this->expectExceptionObject($exception);
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$pipeline->continue();
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
}
|