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;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Success;
|
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 SuccessTest extends BaseTest
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
2016-08-12 23:58:53 +02:00
|
|
|
* @expectedException \Error
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testConstructWithNonException()
|
|
|
|
{
|
2016-11-14 20:59:21 +01:00
|
|
|
$failure = new Success($this->getMockBuilder(Promise::class)->getMock());
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolve()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
|
|
|
$success = new Success($value);
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$success->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-28 01:37:55 +02:00
|
|
|
* @depends testOnResolve
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testOnResolveThrowingForwardsToLoopHandlerOnSuccess()
|
|
|
|
{
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$invoked = 0;
|
|
|
|
$expected = new \Exception;
|
|
|
|
|
|
|
|
Loop::setErrorHandler(function ($exception) use (&$invoked, $expected) {
|
|
|
|
++$invoked;
|
|
|
|
$this->assertSame($expected, $exception);
|
|
|
|
});
|
|
|
|
|
|
|
|
$callback = function () use ($expected) {
|
|
|
|
throw $expected;
|
|
|
|
};
|
|
|
|
|
|
|
|
$success = new Success;
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$success->onResolve($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
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 () {
|
|
|
|
$success = new Success;
|
|
|
|
$success->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
|
|
|
$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.
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|