2016-12-29 14:09:49 -06:00
|
|
|
<?php
|
2016-08-16 16:39:25 -05:00
|
|
|
|
2016-07-12 11:20:06 -05:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp\Failure;
|
|
|
|
use Interop\Async\Loop;
|
|
|
|
|
|
|
|
class FailureTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
/**
|
2016-08-11 14:35:58 -05:00
|
|
|
* @expectedException \TypeError
|
2016-07-12 11:20:06 -05:00
|
|
|
*/
|
|
|
|
public function testConstructWithNonException() {
|
|
|
|
$failure = new Failure(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhen() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$reason) {
|
|
|
|
++$invoked;
|
|
|
|
$reason = $exception;
|
|
|
|
};
|
|
|
|
|
|
|
|
$success = new Failure($exception);
|
|
|
|
|
|
|
|
$success->when($callback);
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhen
|
|
|
|
*/
|
|
|
|
public function testWhenThrowingForwardsToLoopHandlerOnSuccess() {
|
|
|
|
Loop::execute(function () use (&$invoked) {
|
|
|
|
$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 Failure(new \Exception);
|
|
|
|
|
|
|
|
$success->when($callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
}
|