2016-08-18 05:25:54 +02:00
|
|
|
<?php declare(strict_types = 1);
|
2016-08-16 23:39:25 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
use Interop\Async\{ Loop, Promise };
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
class Placeholder {
|
|
|
|
use \Amp\Internal\Placeholder {
|
|
|
|
resolve as public;
|
|
|
|
fail as public;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PlaceholderTest extends \PHPUnit_Framework_TestCase {
|
2016-08-18 05:25:54 +02:00
|
|
|
/** @var \Amp\Test\Placeholder */
|
2016-08-18 04:17:31 +02:00
|
|
|
private $placeholder;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
public function setUp() {
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder = new Placeholder;
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhenOnSuccess() {
|
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenOnSuccess
|
|
|
|
*/
|
|
|
|
public function testMultipleWhensOnSuccess() {
|
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
|
|
|
$this->placeholder->when($callback);
|
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(3, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenOnSuccess
|
|
|
|
*/
|
|
|
|
public function testWhenAfterSuccess() {
|
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenAfterSuccess
|
|
|
|
*/
|
|
|
|
public function testMultipleWhenAfterSuccess() {
|
|
|
|
$value = "Resolution value";
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $value;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
|
|
|
$this->placeholder->when($callback);
|
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(3, $invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenOnSuccess
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($expected);
|
2016-07-12 18:20:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenAfterSuccess
|
|
|
|
*/
|
|
|
|
public function testWhenThrowingForwardsToLoopHandlerAfterSuccess() {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve($expected);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWhenOnFail() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $exception;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenOnFail
|
|
|
|
*/
|
|
|
|
public function testMultipleWhensOnFail() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $exception;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
|
|
|
$this->placeholder->when($callback);
|
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(3, $invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenOnFail
|
|
|
|
*/
|
|
|
|
public function testWhenAfterFail() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $exception;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenAfterFail
|
|
|
|
*/
|
|
|
|
public function testMultipleWhensAfterFail() {
|
|
|
|
$exception = new \Exception;
|
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$callback = function ($exception, $value) use (&$invoked, &$result) {
|
|
|
|
++$invoked;
|
|
|
|
$result = $exception;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
|
|
|
$this->placeholder->when($callback);
|
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame(3, $invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenOnSuccess
|
|
|
|
*/
|
|
|
|
public function testWhenThrowingForwardsToLoopHandlerOnFail() {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail(new \Exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWhenOnSuccess
|
|
|
|
*/
|
|
|
|
public function testWhenThrowingForwardsToLoopHandlerAfterFail() {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->fail(new \Exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when($callback);
|
2016-07-12 18:20:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame(1, $invoked);
|
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testResolveWithPromiseBeforeWhen() {
|
|
|
|
$promise = $this->getMockBuilder(Promise::class)->getMock();
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->expects($this->once())
|
2016-07-12 18:20:06 +02:00
|
|
|
->method("when")
|
|
|
|
->with($this->callback("is_callable"));
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->placeholder->resolve($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when(function () {});
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testResolveWithPromiseAfterWhen() {
|
|
|
|
$promise = $this->getMockBuilder(Promise::class)->getMock();
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise->expects($this->once())
|
2016-07-12 18:20:06 +02:00
|
|
|
->method("when")
|
|
|
|
->with($this->callback("is_callable"));
|
|
|
|
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->when(function () {});
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->placeholder->resolve($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 21:35:58 +02:00
|
|
|
* @expectedException \Error
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
|
|
|
public function testDoubleResolve() {
|
2016-08-18 04:17:31 +02:00
|
|
|
$this->placeholder->resolve();
|
|
|
|
$this->placeholder->resolve();
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
}
|