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

188 lines
4.8 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\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 \Exception;
$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());
} catch (\Exception $reason) {
$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-27 17:32:53 +02:00
* @expectedExceptionMessage Streams 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);
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
* @expectedException \Error
2017-04-27 17:32:53 +02:00
* @expectedExceptionMessage The stream 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-27 17:32:53 +02:00
* @expectedExceptionMessage The stream 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;
}
}