2018-11-26 19:36:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp\Deferred;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2018-11-26 19:36:46 +01:00
|
|
|
use Amp\Promise;
|
2020-09-28 05:19:52 +02:00
|
|
|
use function Amp\await;
|
2018-11-26 19:36:46 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class WrapTest extends AsyncTestCase
|
2018-11-26 19:36:46 +01:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testSuccess(): void
|
2018-11-26 19:36:46 +01:00
|
|
|
{
|
|
|
|
$deferred = new Deferred();
|
|
|
|
|
|
|
|
$promise = Promise\wrap($deferred->promise(), function () {
|
|
|
|
return 2;
|
|
|
|
});
|
|
|
|
|
|
|
|
$deferred->resolve(1);
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$result = await($promise);
|
2018-11-26 19:36:46 +01:00
|
|
|
|
|
|
|
$this->assertSame(2, $result);
|
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testFailure(): void
|
2018-11-26 19:36:46 +01:00
|
|
|
{
|
|
|
|
$deferred = new Deferred();
|
|
|
|
|
|
|
|
$promise = Promise\wrap($deferred->promise(), function () {
|
|
|
|
throw new \Exception('bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
$deferred->fail(new \Exception('foo'));
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('bar');
|
|
|
|
|
|
|
|
await($promise);
|
2018-11-26 19:36:46 +01:00
|
|
|
}
|
|
|
|
}
|