2017-01-04 02:10:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\{
|
|
|
|
Deferred, Failure, Loop, Success
|
|
|
|
};
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-01-04 02:10:27 +01:00
|
|
|
|
|
|
|
class Producer {
|
|
|
|
use \Amp\Internal\Producer {
|
|
|
|
emit as public;
|
|
|
|
resolve as public;
|
|
|
|
fail as public;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
/** @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-01-04 02:10:27 +01:00
|
|
|
$this->producer->listen($callback);
|
|
|
|
$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-01-04 02:10:27 +01:00
|
|
|
$this->producer->listen($callback);
|
|
|
|
$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-01-04 02:10:27 +01:00
|
|
|
$this->producer->listen($callback);
|
|
|
|
$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-01-04 02:10:27 +01:00
|
|
|
$this->producer->when(function ($exception) use (&$invoked, &$reason) {
|
|
|
|
$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-01-04 02:10:27 +01:00
|
|
|
$this->producer->listen($callback);
|
|
|
|
$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-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-01-04 02:10:27 +01:00
|
|
|
$this->producer->listen($callback);
|
|
|
|
$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-01-04 02:10:27 +01:00
|
|
|
$promise->when(function ($exception) use (&$invoked, &$reason) {
|
|
|
|
$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-01-04 02:10:27 +01:00
|
|
|
$promise->when(function ($exception) use (&$invoked, &$reason) {
|
|
|
|
$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-01-04 02:10:27 +01:00
|
|
|
$this->producer->listen(function () use ($exception) {
|
|
|
|
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() {
|
|
|
|
$invoked = true;
|
|
|
|
$value = 1;
|
|
|
|
$promise = new Success($value);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->producer->listen(function () use ($promise) {
|
|
|
|
return $promise;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$promise = $this->producer->emit(1);
|
|
|
|
$promise->when(function () use (&$invoked) {
|
|
|
|
$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-01-04 02:10:27 +01:00
|
|
|
$this->producer->listen(function () use ($promise) {
|
|
|
|
return $promise;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$promise = $this->producer->emit(1);
|
|
|
|
$promise->when(function () use (&$invoked) {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|