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;
|
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 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);
|
|
|
|
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(0); // Tick event loop to execute onResolve callback.
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
}
|