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-07-18 15:39:38 +02:00
|
|
|
use Amp\Promise;
|
2017-03-28 01:37:55 +02:00
|
|
|
use React\Promise\RejectedPromise as RejectedReactPromise;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2019-05-14 21:37:08 +02:00
|
|
|
class FailureTest extends BaseTest
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
2016-08-11 21:35:58 +02:00
|
|
|
* @expectedException \TypeError
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testConstructWithNonException()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$failure = new Failure(1);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolve()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$reason) {
|
|
|
|
++$invoked;
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
|
|
|
|
2017-03-28 01:37:55 +02:00
|
|
|
$failure = new Failure($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-28 01:37:55 +02:00
|
|
|
$failure->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-03-28 01:37:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage Success
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveWithReactPromise()
|
|
|
|
{
|
2017-03-28 01:37:55 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$failure = new Failure(new \Exception);
|
|
|
|
$failure->onResolve(function ($exception, $value) {
|
|
|
|
return new RejectedReactPromise(new \Exception("Success"));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveWithGenerator()
|
|
|
|
{
|
2017-03-28 01:37:55 +02:00
|
|
|
$exception = new \Exception;
|
|
|
|
$failure = new Failure($exception);
|
|
|
|
$invoked = false;
|
|
|
|
$failure->onResolve(function ($exception, $value) use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
return $exception;
|
|
|
|
yield; // Unreachable, but makes function a generator.
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2020-07-18 15:39:38 +02:00
|
|
|
|
|
|
|
public function testFailFunction()
|
|
|
|
{
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$reason) {
|
|
|
|
++$invoked;
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
|
|
|
|
|
|
|
$failure = Promise\fail($exception);
|
|
|
|
|
|
|
|
$failure->onResolve($callback);
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|