1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00
amp/test/FailureTest.php
Aaron Piotrowski 5651240615 Update to promise spec v0.3
Dropped strict-types due to spec requiring weak types in callbacks.
2016-12-29 16:29:27 -06:00

58 lines
1.3 KiB
PHP

<?php
namespace Amp\Test;
use Amp\Failure;
use Interop\Async\Loop;
class FailureTest extends \PHPUnit_Framework_TestCase {
/**
* @expectedException \TypeError
*/
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);
}
}