1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 05:11:42 +01:00
amp/test/AdaptTest.php

106 lines
2.5 KiB
PHP
Raw Normal View History

2016-07-12 11:20:06 -05:00
<?php
namespace Amp\Test;
use Amp;
use Amp\Failure;
use Amp\Success;
use Interop\Async\Awaitable;
class PromiseMock {
/**
* @var \Interop\Async\Awaitable
*/
private $awaitable;
public function __construct(Awaitable $awaitable) {
$this->awaitable = $awaitable;
}
public function then(callable $onFulfilled = null, callable $onRejected = null) {
$this->awaitable->when(function ($exception, $value) use ($onFulfilled, $onRejected) {
if ($exception) {
if ($onRejected) {
$onRejected($exception);
}
return;
}
if ($onFulfilled) {
$onFulfilled($value);
}
});
}
}
class AdaptTest extends \PHPUnit_Framework_TestCase {
public function testThenCalled() {
$mock = $this->getMockBuilder(PromiseMock::class)
->disableOriginalConstructor()
->getMock();
$mock->expects($this->once())
->method("then")
->with(
$this->callback(function ($resolve) {
return is_callable($resolve);
}),
$this->callback(function ($reject) {
return is_callable($reject);
})
);
$awaitable = Amp\adapt($mock);
$this->assertInstanceOf(Awaitable::class, $awaitable);
}
/**
* @depends testThenCalled
*/
public function testAwaitableFulfilled() {
$value = 1;
$promise = new PromiseMock(new Success($value));
$awaitable = Amp\adapt($promise);
$awaitable->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertSame($value, $result);
}
/**
* @depends testThenCalled
*/
public function testAwaitableRejected() {
$exception = new \Exception;
$promise = new PromiseMock(new Failure($exception));
$awaitable = Amp\adapt($promise);
$awaitable->when(function ($exception, $value) use (&$reason) {
$reason = $exception;
});
$this->assertSame($exception, $reason);
}
/**
2016-08-11 14:35:58 -05:00
* @expectedException \Error
2016-07-12 11:20:06 -05:00
*/
public function testScalarValue() {
Amp\adapt(1);
}
/**
2016-08-11 14:35:58 -05:00
* @expectedException \Error
2016-07-12 11:20:06 -05:00
*/
public function testNonThenableObject() {
Amp\adapt(new \stdClass);
}
}