2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 23:39:25 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Success;
|
2020-09-28 05:19:52 +02:00
|
|
|
use function Amp\await;
|
|
|
|
use function Amp\sleep;
|
|
|
|
use function React\Promise\reject;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class SuccessTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testConstructWithPromise(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->expectException(\Error::class);
|
|
|
|
|
|
|
|
$success = new Success($this->getMockBuilder(Promise::class)->getMock());
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testOnResolve(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$success = new Success($value);
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertSame($value, await($success));
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolve
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testOnResolveThrowingForwardsToLoopHandlerOnSuccess(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$invoked = 0;
|
|
|
|
$expected = new \Exception;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
Loop::setErrorHandler(function ($exception) use (&$invoked, $expected) {
|
|
|
|
++$invoked;
|
|
|
|
$this->assertSame($expected, $exception);
|
|
|
|
});
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$callback = function () use ($expected) {
|
|
|
|
throw $expected;
|
|
|
|
};
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$success = new Success;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$success->onResolve($callback);
|
|
|
|
|
|
|
|
sleep(0); // Tick event loop to execute onResolve callback.
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
2017-03-28 01:37:55 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testOnResolveWithReactPromise(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$invoked = 0;
|
|
|
|
$success = new Success;
|
|
|
|
|
|
|
|
Loop::setErrorHandler(function (\Throwable $exception) use (&$invoked, &$reason): void {
|
|
|
|
++$invoked;
|
|
|
|
$reason = $exception;
|
2017-03-28 01:37:55 +02:00
|
|
|
});
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
$success->onResolve(fn () => reject(new \Exception("Success")));
|
|
|
|
|
|
|
|
sleep(0); // Tick event loop to execute onResolve callback.
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
2017-03-28 01:37:55 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testOnResolveWithGenerator(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-03-28 01:37:55 +02:00
|
|
|
$value = 1;
|
|
|
|
$success = new Success($value);
|
|
|
|
$invoked = false;
|
|
|
|
$success->onResolve(function ($exception, $value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
return $value;
|
|
|
|
yield; // Unreachable, but makes function a generator.
|
|
|
|
});
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertSame($value, await($success));
|
|
|
|
sleep(0); // Tick event loop to execute coroutine
|
2017-03-28 01:37:55 +02:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|