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;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2016-07-12 11:20:06 -05:00
|
|
|
use Amp\Success;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2016-07-12 11:20:06 -05:00
|
|
|
|
2017-03-11 14:57:03 +01:00
|
|
|
class SuccessTest extends \PHPUnit\Framework\TestCase {
|
2016-07-12 11:20:06 -05:00
|
|
|
/**
|
2016-08-12 16:58:53 -05:00
|
|
|
* @expectedException \Error
|
2016-07-12 11:20:06 -05:00
|
|
|
*/
|
|
|
|
public function testConstructWithNonException() {
|
2016-11-14 13:59:21 -06:00
|
|
|
$failure = new Success($this->getMockBuilder(Promise::class)->getMock());
|
2016-07-12 11:20:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhen() {
|
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
|
|
|
$success = new Success($value);
|
|
|
|
|
|
|
|
$success->when($callback);
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhen
|
|
|
|
*/
|
|
|
|
public function testWhenThrowingForwardsToLoopHandlerOnSuccess() {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked) {
|
2016-07-12 11:20:06 -05:00
|
|
|
$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 Success;
|
|
|
|
|
|
|
|
$success->when($callback);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
}
|