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

281 lines
7.1 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;
resolve as public;
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() {
$invoked = false;
$value = 1;
2017-01-04 02:10:27 +01:00
$callback = function ($emitted) use (&$invoked, $value) {
$invoked = true;
$this->assertSame($emitted, $value);
};
$this->producer->onEmit($callback);
2017-01-04 02:10:27 +01:00
$promise = $this->producer->emit($value);
2017-01-04 02:10:27 +01:00
$this->assertInstanceOf(Promise::class, $promise);
$this->assertTrue($invoked);
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
*/
public function testEmitSuccessfulPromise() {
$invoked = false;
$value = 1;
$promise = new Success($value);
2017-01-04 02:10:27 +01:00
$callback = function ($emitted) use (&$invoked, $value) {
$invoked = true;
$this->assertSame($emitted, $value);
};
$this->producer->onEmit($callback);
2017-01-04 02:10:27 +01:00
$this->producer->emit($promise);
2017-01-04 02:10:27 +01:00
$this->assertTrue($invoked);
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
*/
public function testEmitFailedPromise() {
$invoked = false;
$exception = new \Exception;
$promise = new Failure($exception);
2017-01-04 02:10:27 +01:00
$callback = function ($emitted) use (&$invoked) {
$invoked = true;
};
$this->producer->onEmit($callback);
2017-01-04 02:10:27 +01:00
$this->producer->emit($promise);
2017-01-04 02:10:27 +01:00
$this->assertFalse($invoked);
$this->producer->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);
$this->assertSame($exception, $reason);
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
*/
public function testEmitPendingPromise() {
$invoked = false;
$value = 1;
$deferred = new Deferred;
2017-01-04 02:10:27 +01:00
$callback = function ($emitted) use (&$invoked, $value) {
$invoked = true;
$this->assertSame($emitted, $value);
};
$this->producer->onEmit($callback);
2017-01-04 02:10:27 +01:00
$this->producer->emit($deferred->promise());
2017-01-04 02:10:27 +01:00
$this->assertFalse($invoked);
2017-01-04 02:10:27 +01:00
$deferred->resolve($value);
2017-01-04 02:10:27 +01:00
$this->assertTrue($invoked);
}
/**
* @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);
};
$this->producer->onEmit($callback);
$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-04 02:10:27 +01:00
$callback = function ($emitted) use (&$invoked, &$result) {
$invoked = true;
$result = $emitted;
};
$this->producer->onEmit($callback);
2017-01-04 02:10:27 +01:00
$this->producer->emit($deferred->promise());
2017-01-04 02:10:27 +01:00
$this->assertFalse($invoked);
2017-01-04 02:10:27 +01:00
$this->producer->emit(2);
$this->assertTrue($invoked);
$this->assertSame(2, $result);
2017-01-04 02:10:27 +01:00
$deferred->resolve(1);
$this->assertSame(1, $result);
}
2017-01-04 02:10:27 +01:00
/**
* @depends testEmit
* @expectedException \Error
* @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-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-04 02:10:27 +01:00
$promise = $this->producer->emit($deferred->promise());
2017-01-04 02:10:27 +01:00
$this->producer->resolve();
$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
* @expectedExceptionMessage The stream was resolved before the promise result could be emitted
*/
public function testEmitPendingPromiseThenFail() {
$invoked = false;
$deferred = new Deferred;
2017-01-04 02:10:27 +01:00
$promise = $this->producer->emit($deferred->promise());
2017-01-04 02:10:27 +01:00
$this->producer->resolve();
$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;
}
2017-01-04 02:10:27 +01:00
public function testSubscriberThrows() {
$exception = new \Exception;
2017-01-04 02:10:27 +01:00
try {
Loop::run(function () use ($exception) {
$this->producer->onEmit(function () use ($exception) {
2017-01-04 02:10:27 +01:00
throw $exception;
});
2017-01-04 02:10:27 +01:00
$this->producer->emit(1);
});
} catch (\Exception $caught) {
$this->assertSame($exception, $caught);
}
}
2017-01-04 02:10:27 +01:00
public function testSubscriberReturnsSuccessfulPromise() {
$invoked = false;
2017-01-04 02:10:27 +01:00
$value = 1;
$promise = new Success($value);
$this->producer->onEmit(function () use ($promise) {
2017-01-04 02:10:27 +01:00
return $promise;
});
2017-01-04 02:10:27 +01:00
$promise = $this->producer->emit(1);
$promise->onResolve(function () use (&$invoked) {
2017-01-04 02:10:27 +01:00
$invoked = true;
});
2017-01-04 02:10:27 +01:00
$this->assertTrue($invoked);
}
2017-01-04 02:10:27 +01:00
public function testSubscriberReturnsFailedPromise() {
$exception = new \Exception;
$promise = new Failure($exception);
2017-01-04 02:10:27 +01:00
try {
Loop::run(function () use ($exception, $promise) {
$this->producer->onEmit(function () use ($promise) {
2017-01-04 02:10:27 +01:00
return $promise;
});
2017-01-04 02:10:27 +01:00
$promise = $this->producer->emit(1);
$promise->onResolve(function () use (&$invoked) {
2017-01-04 02:10:27 +01:00
$invoked = true;
});
2017-01-04 02:10:27 +01:00
$this->assertTrue($invoked);
});
} catch (\Exception $caught) {
$this->assertSame($exception, $caught);
}
}
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
}