1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00
amp/test/ProducerTraitTest.php

225 lines
5.9 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 PHPUnit\Framework\TestCase;
use React\Promise\FulfilledPromise as FulfilledReactPromise;
2017-01-04 02:10:27 +01:00
class Producer {
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 TestCase {
2017-01-04 02:10:27 +01:00
/** @var \Amp\Test\Producer */
private $producer;
public function setUp() {
$this->producer = new Producer;
}
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
2017-04-27 17:32:53 +02:00
public function testEmitAfterComplete() {
$this->producer->complete();
2017-01-04 02:10:27 +01:00
$this->producer->emit(1);
}
/**
* @expectedException \Error
* @expectedExceptionMessage The iterator has completed
*/
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
*/
2017-04-27 17:32:53 +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
*/
public function testEmitPendingPromiseThenFail() {
$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
*/
public function testDoubleAdvance() {
$this->producer->advance();
$this->producer->advance();
}
/**
* @expectedException \Error
* @expectedExceptionMessage Promise returned from advance() must resolve before calling this method
*/
public function testGetCurrentBeforeAdvance() {
$this->producer->getCurrent();
}
/**
* @expectedException \Error
* @expectedExceptionMessage Iterator has already been completed
*/
public function testDoubleComplete() {
$this->producer->complete();
$this->producer->complete();
}
2017-01-04 02:10:27 +01:00
}