1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/test/ProducerTraitTest.php

240 lines
6.0 KiB
PHP
Raw Normal View History

2017-01-04 02:10:27 +01:00
<?php
namespace Amp\Test;
2017-03-11 14:43:57 +01:00
use Amp\Deferred;
use Amp\Failure;
use Amp\Loop;
use Amp\PHPUnit\TestException;
use Amp\Promise;
2017-04-23 14:39:19 +02:00
use Amp\Success;
use React\Promise\FulfilledPromise as FulfilledReactPromise;
2017-01-04 02:10:27 +01:00
2018-06-18 20:00:01 +02:00
class Producer
{
2017-01-04 02:10:27 +01:00
use \Amp\Internal\Producer {
emit as public;
2017-04-27 17:32:53 +02:00
complete as public;
2017-01-04 02:10:27 +01:00
fail as public;
}
}
class ProducerTraitTest extends BaseTest
2018-06-18 20:00:01 +02:00
{
2017-01-04 02:10:27 +01:00
/** @var \Amp\Test\Producer */
private $producer;
2018-06-18 20:00:01 +02:00
public function setUp()
{
2017-01-04 02:10:27 +01:00
$this->producer = new Producer;
}
2018-06-18 20:00:01 +02:00
public function testEmit()
{
2017-04-27 17:32:53 +02:00
Loop::run(function () {
$value = 1;
2017-04-27 17:32:53 +02:00
$promise = $this->producer->emit($value);
2017-04-27 17:32:53 +02:00
$this->assertTrue(yield $this->producer->advance());
$this->assertSame($value, $this->producer->getCurrent());
2017-04-27 17:32:53 +02:00
$this->assertInstanceOf(Promise::class, $promise);
});
2017-01-04 02:10:27 +01:00
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
*/
2018-06-18 20:00:01 +02:00
public function testEmitSuccessfulPromise()
{
2017-04-27 17:32:53 +02:00
Loop::run(function () {
$value = 1;
$promise = new Success($value);
2017-04-27 17:32:53 +02:00
$promise = $this->producer->emit($promise);
2017-04-27 17:32:53 +02:00
$this->assertTrue(yield $this->producer->advance());
$this->assertSame($value, $this->producer->getCurrent());
2017-04-27 17:32:53 +02:00
$this->assertInstanceOf(Promise::class, $promise);
});
2017-01-04 02:10:27 +01:00
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
*/
2018-06-18 20:00:01 +02:00
public function testEmitFailedPromise()
{
2017-04-27 17:32:53 +02:00
Loop::run(function () {
$exception = new TestException;
2017-04-27 17:32:53 +02:00
$promise = new Failure($exception);
2017-04-27 17:32:53 +02:00
$promise = $this->producer->emit($promise);
2017-04-27 17:32:53 +02:00
try {
$this->assertTrue(yield $this->producer->advance());
$this->fail("The exception used to fail the iterator should be thrown from advance()");
} catch (TestException $reason) {
2017-04-27 17:32:53 +02:00
$this->assertSame($reason, $exception);
}
2017-04-27 17:32:53 +02:00
$this->assertInstanceOf(Promise::class, $promise);
2017-01-04 02:10:27 +01:00
});
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
*/
2018-06-18 20:00:01 +02:00
public function testEmitPendingPromise()
{
2017-04-27 17:32:53 +02:00
Loop::run(function () {
$value = 1;
$deferred = new Deferred;
2017-04-27 17:32:53 +02:00
$this->producer->emit($deferred->promise());
2017-04-27 17:32:53 +02:00
$deferred->resolve($value);
2017-04-27 17:32:53 +02:00
$this->assertTrue(yield $this->producer->advance());
$this->assertSame($value, $this->producer->getCurrent());
});
2017-01-04 02:10:27 +01:00
}
/**
* @depends testEmit
*/
2018-06-18 20:00:01 +02:00
public function testEmitSuccessfulReactPromise()
{
2017-04-27 17:32:53 +02:00
Loop::run(function () {
$value = 1;
$promise = new FulfilledReactPromise($value);
2017-04-27 17:32:53 +02:00
$this->producer->emit($promise);
2017-04-27 17:32:53 +02:00
$this->assertTrue(yield $this->producer->advance());
$this->assertSame($value, $this->producer->getCurrent());
});
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
*/
2018-06-18 20:00:01 +02:00
public function testEmitPendingPromiseThenNonPromise()
{
2017-04-27 17:32:53 +02:00
Loop::run(function () {
$deferred = new Deferred;
2017-04-27 17:32:53 +02:00
$this->producer->emit($deferred->promise());
2017-04-27 17:32:53 +02:00
$this->producer->emit(2);
2017-04-27 17:32:53 +02:00
$this->assertTrue(yield $this->producer->advance());
$this->assertSame(2, $this->producer->getCurrent());
2017-04-27 17:32:53 +02:00
$deferred->resolve(1);
2017-04-27 17:32:53 +02:00
$this->assertTrue(yield $this->producer->advance());
$this->assertSame(1, $this->producer->getCurrent());
});
2017-01-04 02:10:27 +01:00
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
* @expectedException \Error
2017-04-28 07:51:59 +02:00
* @expectedExceptionMessage Iterators cannot emit values after calling complete
2017-01-04 02:10:27 +01:00
*/
2018-06-18 20:00:01 +02:00
public function testEmitAfterComplete()
{
2017-04-27 17:32:53 +02:00
$this->producer->complete();
2017-01-04 02:10:27 +01:00
$this->producer->emit(1);
}
/**
* @expectedException \Error
* @expectedExceptionMessage The iterator has completed
*/
2018-06-18 20:00:01 +02:00
public function testGetCurrentAfterComplete()
{
$this->producer->complete();
$this->producer->getCurrent();
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
* @expectedException \Error
2017-04-28 07:51:59 +02:00
* @expectedExceptionMessage The iterator was completed before the promise result could be emitted
2017-01-04 02:10:27 +01:00
*/
2018-06-18 20:00:01 +02:00
public function testEmitPendingPromiseThenComplete()
{
2017-01-04 02:10:27 +01:00
$invoked = false;
$deferred = new Deferred;
2017-01-04 02:10:27 +01:00
$promise = $this->producer->emit($deferred->promise());
2017-04-27 17:32:53 +02:00
$this->producer->complete();
2017-01-04 02:10:27 +01:00
$deferred->resolve();
$promise->onResolve(function ($exception) use (&$invoked, &$reason) {
2017-01-04 02:10:27 +01:00
$invoked = true;
$reason = $exception;
});
2017-01-04 02:10:27 +01:00
$this->assertTrue($invoked);
throw $reason;
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
* @expectedException \Error
2017-04-28 07:51:59 +02:00
* @expectedExceptionMessage The iterator was completed before the promise result could be emitted
2017-01-04 02:10:27 +01:00
*/
2018-06-18 20:00:01 +02:00
public function testEmitPendingPromiseThenFail()
{
2017-01-04 02:10:27 +01:00
$invoked = false;
$deferred = new Deferred;
2017-01-04 02:10:27 +01:00
$promise = $this->producer->emit($deferred->promise());
2017-04-27 17:32:53 +02:00
$this->producer->complete();
2017-01-04 02:10:27 +01:00
$deferred->fail(new \Exception);
$promise->onResolve(function ($exception) use (&$invoked, &$reason) {
2017-01-04 02:10:27 +01:00
$invoked = true;
$reason = $exception;
});
2017-01-04 02:10:27 +01:00
$this->assertTrue($invoked);
throw $reason;
}
/**
* @expectedException \Error
* @expectedExceptionMessage The prior promise returned must resolve before invoking this method again
*/
2018-06-18 20:00:01 +02:00
public function testDoubleAdvance()
{
$this->producer->advance();
$this->producer->advance();
}
/**
* @expectedException \Error
* @expectedExceptionMessage Promise returned from advance() must resolve before calling this method
*/
2018-06-18 20:00:01 +02:00
public function testGetCurrentBeforeAdvance()
{
$this->producer->getCurrent();
}
/**
* @expectedException \Error
* @expectedExceptionMessage Iterator has already been completed
*/
2018-06-18 20:00:01 +02:00
public function testDoubleComplete()
{
$this->producer->complete();
$this->producer->complete();
}
2017-01-04 02:10:27 +01:00
}