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;
|
|
|
|
|
|
|
|
use Amp\Failure;
|
2017-03-28 01:37:55 +02:00
|
|
|
use Amp\Loop;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
|
|
|
use Amp\PHPUnit\TestException;
|
2020-07-18 15:39:38 +02:00
|
|
|
use Amp\Promise;
|
2020-09-28 05:19:52 +02:00
|
|
|
use function Amp\await;
|
2020-10-07 06:40:14 +02:00
|
|
|
use function Amp\delay;
|
2020-09-28 05:19:52 +02:00
|
|
|
use function React\Promise\reject;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class FailureTest extends AsyncTestCase
|
2018-06-18 20:00:01 +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
|
|
|
$exception = new \Exception;
|
|
|
|
|
2017-03-28 01:37:55 +02:00
|
|
|
$failure = new Failure($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
try {
|
|
|
|
await($failure);
|
|
|
|
} catch (\Exception $reason) {
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
return;
|
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->fail("Promise was not failed");
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
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
|
|
|
$failure = new Failure(new \Exception);
|
|
|
|
$failure->onResolve(function () {
|
|
|
|
return reject(new \Exception("Success"));
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop::setErrorHandler(function (\Throwable $exception) use (&$reason): void {
|
|
|
|
$reason = $exception;
|
2017-03-28 01:37:55 +02:00
|
|
|
});
|
2020-09-28 05:19:52 +02:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to invoke error callback.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
$this->assertSame("Success", $reason->getMessage());
|
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
|
|
|
$exception = new \Exception;
|
|
|
|
$failure = new Failure($exception);
|
|
|
|
$invoked = false;
|
2020-09-28 05:19:52 +02:00
|
|
|
$failure->onResolve(function () use (&$invoked): \Generator {
|
|
|
|
if (false) {
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
|
2017-03-28 01:37:55 +02:00
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
try {
|
|
|
|
await($failure);
|
|
|
|
} catch (\Exception $reason) {
|
|
|
|
$this->assertSame($exception, $reason);
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to execute coroutine
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail("Promise was not failed");
|
2017-03-28 01:37:55 +02:00
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|