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-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-03-15 17:12:49 +01:00
|
|
|
use Amp\Promise;
|
2017-03-13 05:27:43 +01:00
|
|
|
use function React\Promise\reject;
|
2020-10-07 06:40:14 +02:00
|
|
|
use function Amp\delay;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class RethrowTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testRethrow(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$promise = new Failure($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
Promise\rethrow($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$invoked = false;
|
|
|
|
Loop::setErrorHandler(function (\Throwable $exception) use (&$invoked, &$reason): void {
|
|
|
|
$invoked = true;
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick the event loop.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($reason, $exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-03-13 05:27:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testRethrow
|
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testReactPromise(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-03-13 05:27:43 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$promise = reject($exception);
|
2017-03-13 05:27:43 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
Promise\rethrow($promise);
|
2017-03-13 05:27:43 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$invoked = false;
|
|
|
|
Loop::setErrorHandler(function (\Throwable $exception) use (&$invoked, &$reason): void {
|
|
|
|
$invoked = true;
|
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-03-14 22:15:36 +01:00
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick the event loop.
|
2020-09-28 05:19:52 +02:00
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($reason, $exception);
|
2017-03-14 22:15:36 +01:00
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|