1
0
mirror of https://github.com/danog/amp.git synced 2024-12-12 09:29:45 +01:00
amp/test/Iterator/FromIterableTest.php

134 lines
3.5 KiB
PHP
Raw Normal View History

<?php
2020-05-13 17:15:21 +02:00
namespace Amp\Test\Iterator;
use Amp\Delayed;
use Amp\Failure;
2017-04-27 17:51:06 +02:00
use Amp\Iterator;
2020-09-28 05:19:52 +02:00
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
use Amp\Success;
2020-09-28 05:19:52 +02:00
class FromIterableTest extends AsyncTestCase
2018-06-18 20:00:01 +02:00
{
const TIMEOUT = 10;
2020-09-28 05:19:52 +02:00
public function testSuccessfulPromises(): \Generator
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$expected = \range(1, 3);
$iterator = Iterator\fromIterable([new Success(1), new Success(2), new Success(3)]);
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
while (yield $iterator->advance()) {
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
}
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
public function testFailedPromises(): \Generator
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$exception = new \Exception;
$iterator = Iterator\fromIterable([new Failure($exception), new Failure($exception)]);
try {
yield $iterator->advance();
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
}
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
public function testMixedPromises(): \Generator
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$exception = new TestException;
$expected = \range(1, 2);
$iterator = Iterator\fromIterable([new Success(1), new Success(2), new Failure($exception), new Success(4)]);
try {
while (yield $iterator->advance()) {
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
2017-04-27 17:32:53 +02:00
}
2020-09-28 05:19:52 +02:00
$this->fail("A failed promise in the iterable should fail the iterator and be thrown from advance()");
} catch (TestException $reason) {
$this->assertSame($exception, $reason);
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
$this->assertEmpty($expected);
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
public function testPendingPromises(): \Generator
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$expected = \range(1, 4);
$iterator = Iterator\fromIterable([new Delayed(30, 1), new Delayed(10, 2), new Delayed(20, 3), new Success(4)]);
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
while (yield $iterator->advance()) {
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
}
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
public function testTraversable(): \Generator
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$expected = \range(1, 4);
$generator = (function () {
foreach (\range(1, 4) as $value) {
yield $value;
}
})();
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
$iterator = Iterator\fromIterable($generator);
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
while (yield $iterator->advance()) {
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
}
2017-01-06 23:16:06 +01:00
2020-09-28 05:19:52 +02:00
$this->assertEmpty($expected);
2017-01-04 02:10:27 +01:00
}
2017-01-06 23:16:06 +01:00
/**
* @dataProvider provideInvalidIteratorArguments
2017-01-06 23:16:06 +01:00
*/
2020-09-28 05:19:52 +02:00
public function testInvalid($arg): void
2018-06-18 20:00:01 +02:00
{
2020-05-13 17:15:21 +02:00
$this->expectException(\TypeError::class);
2017-04-27 17:51:06 +02:00
Iterator\fromIterable($arg);
2017-01-06 23:16:06 +01:00
}
2020-09-28 05:19:52 +02:00
public function provideInvalidIteratorArguments(): array
2018-06-18 20:00:01 +02:00
{
2017-01-06 23:16:06 +01:00
return [
[null],
[new \stdClass],
[32],
[false],
[true],
["string"],
];
}
2020-09-28 05:19:52 +02:00
public function testInterval(): \Generator
2018-06-18 20:00:01 +02:00
{
2020-09-28 05:19:52 +02:00
$count = 3;
$iterator = Iterator\fromIterable(\range(1, $count), self::TIMEOUT);
2020-09-28 05:19:52 +02:00
$i = 0;
while (yield $iterator->advance()) {
$this->assertSame(++$i, $iterator->getCurrent());
}
2020-09-28 05:19:52 +02:00
$this->assertSame($count, $i);
}
/**
* @depends testInterval
*/
2020-09-28 05:19:52 +02:00
public function testSlowConsumer(): \Generator
2018-06-18 20:00:01 +02:00
{
$count = 5;
2020-09-28 05:19:52 +02:00
$iterator = Iterator\fromIterable(\range(1, $count), self::TIMEOUT);
2020-09-28 05:19:52 +02:00
for ($i = 0; yield $iterator->advance(); ++$i) {
yield new Delayed(self::TIMEOUT * 2);
}
2020-09-28 05:19:52 +02:00
$this->assertSame($count, $i);
}
}