1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00
amp/test/EachTest.php

155 lines
4.5 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
use Amp;
use Amp\Producer;
use Amp\Stream;
use Amp\Emitter;
use Amp\Loop;
class EachTest extends \PHPUnit\Framework\TestCase {
public function testNoValuesEmitted() {
$invoked = false;
Loop::run(function () use (&$invoked){
2017-01-04 02:10:27 +01:00
$emitter = new Emitter;
2017-01-04 02:10:27 +01:00
$stream = Amp\each($emitter->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
2017-01-04 02:10:27 +01:00
$this->assertInstanceOf(Stream::class, $stream);
2017-01-04 02:10:27 +01:00
$emitter->resolve();
});
$this->assertFalse($invoked);
}
public function testValuesEmitted() {
$count = 0;
$values = [1, 2, 3];
$final = 4;
$results = [];
Loop::run(function () use (&$results, &$result, &$count, $values, $final) {
2017-01-04 02:10:27 +01:00
$producer = new Producer(function (callable $emit) use ($values, $final) {
foreach ($values as $value) {
yield $emit($value);
}
return $final;
});
2017-01-04 02:10:27 +01:00
$stream = Amp\each($producer, function ($value) use (&$count) {
++$count;
return $value + 1;
}, function ($value) use (&$invoked) {
return $value + 1;
});
2017-01-04 02:10:27 +01:00
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
2017-01-04 02:10:27 +01:00
$stream->when(function ($exception, $value) use (&$result) {
$result = $value;
});
});
$this->assertSame(\count($values), $count);
$this->assertSame(\array_map(function ($value) { return $value + 1; }, $values), $results);
$this->assertSame($final + 1, $result);
}
/**
* @depends testValuesEmitted
*/
public function testOnNextCallbackThrows() {
$values = [1, 2, 3];
$exception = new \Exception;
Loop::run(function () use (&$reason, $values, $exception) {
2017-01-04 02:10:27 +01:00
$producer = new Producer(function (callable $emit) use ($values) {
foreach ($values as $value) {
yield $emit($value);
}
});
2017-01-04 02:10:27 +01:00
$stream = Amp\each($producer, function () use ($exception) {
throw $exception;
});
2017-01-04 02:10:27 +01:00
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2017-01-04 02:10:27 +01:00
$stream->when($callback);
});
$this->assertSame($exception, $reason);
}
/**
* @depends testValuesEmitted
*/
public function testOnCompleteCallbackThrows() {
$count = 0;
$values = [1, 2, 3];
$results = [];
$exception = new \Exception;
Loop::run(function () use (&$reason, &$results, &$count, $values, $exception) {
2017-01-04 02:10:27 +01:00
$producer = new Producer(function (callable $emit) use ($values) {
foreach ($values as $value) {
yield $emit($value);
}
});
2017-01-04 02:10:27 +01:00
$stream = Amp\each($producer, function ($value) use (&$count) {
++$count;
return $value + 1;
}, function ($value) use ($exception) {
throw $exception;
});
2017-01-04 02:10:27 +01:00
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2017-01-04 02:10:27 +01:00
$stream->when($callback);
});
$this->assertSame(\count($values), $count);
$this->assertSame(\array_map(function ($value) { return $value + 1; }, $values), $results);
$this->assertSame($exception, $reason);
}
2017-01-04 02:10:27 +01:00
public function testStreamFails() {
$invoked = false;
$exception = new \Exception;
Loop::run(function () use (&$invoked, &$reason, &$exception){
2017-01-04 02:10:27 +01:00
$emitter = new Emitter;
2017-01-04 02:10:27 +01:00
$stream = Amp\each($emitter->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
2017-01-04 02:10:27 +01:00
$emitter->fail($exception);
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2017-01-04 02:10:27 +01:00
$stream->when($callback);
});
$this->assertFalse($invoked);
$this->assertSame($exception, $reason);
}
}