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;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Success;
|
2017-03-12 21:02:26 +01:00
|
|
|
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;
|
|
|
|
resolve as public;
|
|
|
|
fail as public;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 21:02:26 +01:00
|
|
|
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() {
|
|
|
|
$invoked = false;
|
|
|
|
$value = 1;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$callback = function ($emitted) use (&$invoked, $value) {
|
|
|
|
$invoked = true;
|
|
|
|
$this->assertSame($emitted, $value);
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->producer->onEmit($callback);
|
2017-01-04 02:10:27 +01:00
|
|
|
$promise = $this->producer->emit($value);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testEmitSuccessfulPromise() {
|
|
|
|
$invoked = false;
|
|
|
|
$value = 1;
|
|
|
|
$promise = new Success($value);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$callback = function ($emitted) use (&$invoked, $value) {
|
|
|
|
$invoked = true;
|
|
|
|
$this->assertSame($emitted, $value);
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->producer->onEmit($callback);
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->producer->emit($promise);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testEmitFailedPromise() {
|
|
|
|
$invoked = false;
|
|
|
|
$exception = new \Exception;
|
|
|
|
$promise = new Failure($exception);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$callback = function ($emitted) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->producer->onEmit($callback);
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->producer->emit($promise);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertFalse($invoked);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$this->producer->onResolve(function ($exception) use (&$invoked, &$reason) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$invoked = true;
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testEmitPendingPromise() {
|
|
|
|
$invoked = false;
|
|
|
|
$value = 1;
|
|
|
|
$deferred = new Deferred;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$callback = function ($emitted) use (&$invoked, $value) {
|
|
|
|
$invoked = true;
|
|
|
|
$this->assertSame($emitted, $value);
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->producer->onEmit($callback);
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->producer->emit($deferred->promise());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertFalse($invoked);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$deferred->resolve($value);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-12 21:02:26 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testEmitSuccessfulReactPromise() {
|
|
|
|
$invoked = false;
|
|
|
|
$value = 1;
|
|
|
|
$promise = new FulfilledReactPromise($value);
|
|
|
|
|
|
|
|
$callback = function ($emitted) use (&$invoked, $value) {
|
|
|
|
$invoked = true;
|
|
|
|
$this->assertSame($emitted, $value);
|
|
|
|
};
|
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->producer->onEmit($callback);
|
2017-03-12 21:02:26 +01:00
|
|
|
$this->producer->emit($promise);
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testEmitPendingPromiseThenNonPromise() {
|
|
|
|
$invoked = false;
|
|
|
|
$deferred = new Deferred;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$callback = function ($emitted) use (&$invoked, &$result) {
|
|
|
|
$invoked = true;
|
|
|
|
$result = $emitted;
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->producer->onEmit($callback);
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->producer->emit($deferred->promise());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertFalse($invoked);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->producer->emit(2);
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame(2, $result);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$deferred->resolve(1);
|
|
|
|
$this->assertSame(1, $result);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
* @expectedException \Error
|
2017-01-07 19:25:04 +01:00
|
|
|
* @expectedExceptionMessage Streams cannot emit values after calling resolve
|
2017-01-04 02:10:27 +01:00
|
|
|
*/
|
|
|
|
public function testEmitAfterResolve() {
|
|
|
|
$this->producer->resolve();
|
|
|
|
$this->producer->emit(1);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
* @expectedException \Error
|
|
|
|
* @expectedExceptionMessage The stream was resolved before the promise result could be emitted
|
|
|
|
*/
|
|
|
|
public function testEmitPendingPromiseThenResolve() {
|
|
|
|
$invoked = false;
|
|
|
|
$deferred = new Deferred;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$promise = $this->producer->emit($deferred->promise());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->producer->resolve();
|
|
|
|
$deferred->resolve();
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception) use (&$invoked, &$reason) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$invoked = true;
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
throw $reason;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
* @expectedException \Error
|
|
|
|
* @expectedExceptionMessage The stream was resolved before the promise result could be emitted
|
|
|
|
*/
|
|
|
|
public function testEmitPendingPromiseThenFail() {
|
|
|
|
$invoked = false;
|
|
|
|
$deferred = new Deferred;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$promise = $this->producer->emit($deferred->promise());
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->producer->resolve();
|
|
|
|
$deferred->fail(new \Exception);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception) use (&$invoked, &$reason) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$invoked = true;
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
throw $reason;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testSubscriberThrows() {
|
|
|
|
$exception = new \Exception;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
try {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use ($exception) {
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->producer->onEmit(function () use ($exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->producer->emit(1);
|
|
|
|
});
|
|
|
|
} catch (\Exception $caught) {
|
|
|
|
$this->assertSame($exception, $caught);
|
|
|
|
}
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testSubscriberReturnsSuccessfulPromise() {
|
2017-03-28 01:37:55 +02:00
|
|
|
$invoked = false;
|
2017-01-04 02:10:27 +01:00
|
|
|
$value = 1;
|
|
|
|
$promise = new Success($value);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->producer->onEmit(function () use ($promise) {
|
2017-01-04 02:10:27 +01:00
|
|
|
return $promise;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$promise = $this->producer->emit(1);
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function () use (&$invoked) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$invoked = true;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testSubscriberReturnsFailedPromise() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
$promise = new Failure($exception);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
try {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use ($exception, $promise) {
|
2017-03-21 18:24:06 +01:00
|
|
|
$this->producer->onEmit(function () use ($promise) {
|
2017-01-04 02:10:27 +01:00
|
|
|
return $promise;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$promise = $this->producer->emit(1);
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function () use (&$invoked) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$invoked = true;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
});
|
|
|
|
} catch (\Exception $caught) {
|
|
|
|
$this->assertSame($exception, $caught);
|
|
|
|
}
|
|
|
|
}
|
2017-03-28 01:37:55 +02:00
|
|
|
|
|
|
|
public function testSubscriberReturnsGenerator() {
|
|
|
|
$invoked = false;
|
|
|
|
$this->producer->onEmit(function ($value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
return $value;
|
|
|
|
yield; // Unreachable, but makes function a generator.
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->producer->emit(1);
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
}
|