1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00
amp/test/CaptureTest.php

113 lines
2.8 KiB
PHP
Raw Normal View History

2016-08-17 22:25:54 -05:00
<?php declare(strict_types = 1);
2016-07-12 11:20:06 -05:00
namespace Amp\Test;
use Amp;
2016-11-14 13:59:21 -06:00
use Amp\{ Failure, Success };
use Interop\Async\Promise;
2016-07-12 11:20:06 -05:00
class CaptureTest extends \PHPUnit_Framework_TestCase {
2016-11-14 13:59:21 -06:00
public function testSuccessfulPromise() {
2016-07-12 11:20:06 -05:00
$invoked = false;
$callback = function ($exception) use (&$invoked) {
$invoked = true;
return -1;
};
$value = 1;
2016-11-14 13:59:21 -06:00
$promise = new Success($value);
2016-07-12 11:20:06 -05:00
2016-11-14 13:59:21 -06:00
$promise = Amp\capture($promise, \Exception::class, $callback);
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 11:20:06 -05:00
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
2016-11-14 13:59:21 -06:00
$promise->when($callback);
2016-07-12 11:20:06 -05:00
$this->assertFalse($invoked);
$this->assertSame($value, $result);
}
2016-11-14 13:59:21 -06:00
public function testFailedPromise() {
2016-07-12 11:20:06 -05:00
$invoked = false;
$callback = function ($exception) use (&$invoked, &$reason) {
$invoked = true;
$reason = $exception;
return -1;
};
$exception = new \Exception;
2016-11-14 13:59:21 -06:00
$promise = new Failure($exception);
2016-07-12 11:20:06 -05:00
2016-11-14 13:59:21 -06:00
$promise = Amp\capture($promise, \Exception::class, $callback);
$this->assertInstanceOf(Promise::class, $promise);
2016-07-12 11:20:06 -05:00
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
2016-11-14 13:59:21 -06:00
$promise->when($callback);
2016-07-12 11:20:06 -05:00
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
$this->assertSame(-1, $result);
}
/**
2016-11-14 13:59:21 -06:00
* @depends testFailedPromise
2016-07-12 11:20:06 -05:00
*/
public function testCallbackThrowing() {
$invoked = false;
$callback = function ($exception) use (&$invoked) {
$invoked = true;
throw new \Exception;
};
$exception = new \Exception;
2016-11-14 13:59:21 -06:00
$promise = new Failure($exception);
2016-07-12 11:20:06 -05:00
2016-11-14 13:59:21 -06:00
$promise = Amp\capture($promise, \Exception::class, $callback);
2016-07-12 11:20:06 -05:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2016-11-14 13:59:21 -06:00
$promise->when($callback);
2016-07-12 11:20:06 -05:00
$this->assertTrue($invoked);
$this->assertNotSame($exception, $reason);
}
/**
2016-11-14 13:59:21 -06:00
* @depends testFailedPromise
2016-07-12 11:20:06 -05:00
*/
public function testUnmatchedExceptionClass() {
$invoked = false;
$callback = function ($exception) use (&$invoked, &$reason) {
$invoked = true;
$reason = $exception;
return -1;
};
$exception = new \LogicException;
2016-11-14 13:59:21 -06:00
$promise = new Failure($exception);
2016-07-12 11:20:06 -05:00
2016-11-14 13:59:21 -06:00
$promise = Amp\capture($promise, \RuntimeException::class, $callback);
2016-07-12 11:20:06 -05:00
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
2016-11-14 13:59:21 -06:00
$promise->when($callback);
2016-07-12 11:20:06 -05:00
$this->assertFalse($invoked);
$this->assertSame($exception, $reason);
}
}