1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 18:38:17 +01:00
amp/test/PlaceholderTraitTest.php

361 lines
9.5 KiB
PHP
Raw Normal View History

<?php
2016-07-12 18:20:06 +02:00
namespace Amp\Test;
use Amp\Loop;
use Amp\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 PlaceholderTraitTest 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 testOnResolveOnSuccess() {
2016-07-12 18:20:06 +02:00
$value = "Resolution value";
$invoked = 0;
$callback = function ($exception, $value) use (&$invoked, &$result) {
++$invoked;
$result = $value;
};
$this->placeholder->onResolve($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 testOnResolveOnSuccess
2016-07-12 18:20:06 +02:00
*/
public function testMultipleOnResolvesOnSuccess() {
2016-07-12 18:20:06 +02:00
$value = "Resolution value";
$invoked = 0;
$callback = function ($exception, $value) use (&$invoked, &$result) {
++$invoked;
$result = $value;
};
$this->placeholder->onResolve($callback);
$this->placeholder->onResolve($callback);
$this->placeholder->onResolve($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 testOnResolveOnSuccess
2016-07-12 18:20:06 +02:00
*/
public function testOnResolveAfterSuccess() {
2016-07-12 18:20:06 +02:00
$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
$this->placeholder->onResolve($callback);
2016-07-12 18:20:06 +02:00
$this->assertSame(1, $invoked);
$this->assertSame($value, $result);
}
/**
* @depends testOnResolveAfterSuccess
2016-07-12 18:20:06 +02:00
*/
public function testMultipleOnResolveAfterSuccess() {
2016-07-12 18:20:06 +02:00
$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
$this->placeholder->onResolve($callback);
$this->placeholder->onResolve($callback);
$this->placeholder->onResolve($callback);
2016-07-12 18:20:06 +02:00
$this->assertSame(3, $invoked);
$this->assertSame($value, $result);
}
/**
* @depends testOnResolveOnSuccess
2016-07-12 18:20:06 +02:00
*/
public function testOnResolveThrowingForwardsToLoopHandlerOnSuccess() {
Loop::run(function () use (&$invoked) {
2016-07-12 18:20:06 +02:00
$invoked = 0;
$expected = new \Exception;
Loop::setErrorHandler(function ($exception) use (&$invoked, $expected) {
++$invoked;
$this->assertSame($expected, $exception);
});
$callback = function () use ($expected) {
throw $expected;
};
$this->placeholder->onResolve($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 testOnResolveAfterSuccess
2016-07-12 18:20:06 +02:00
*/
public function testOnResolveThrowingForwardsToLoopHandlerAfterSuccess() {
Loop::run(function () use (&$invoked) {
2016-07-12 18:20:06 +02:00
$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
$this->placeholder->onResolve($callback);
2016-07-12 18:20:06 +02:00
});
$this->assertSame(1, $invoked);
}
public function testOnResolveOnFail() {
2016-07-12 18:20:06 +02:00
$exception = new \Exception;
$invoked = 0;
$callback = function ($exception, $value) use (&$invoked, &$result) {
++$invoked;
$result = $exception;
};
$this->placeholder->onResolve($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 testOnResolveOnFail
2016-07-12 18:20:06 +02:00
*/
public function testMultipleOnResolvesOnFail() {
2016-07-12 18:20:06 +02:00
$exception = new \Exception;
$invoked = 0;
$callback = function ($exception, $value) use (&$invoked, &$result) {
++$invoked;
$result = $exception;
};
$this->placeholder->onResolve($callback);
$this->placeholder->onResolve($callback);
$this->placeholder->onResolve($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 testOnResolveOnFail
2016-07-12 18:20:06 +02:00
*/
public function testOnResolveAfterFail() {
2016-07-12 18:20:06 +02:00
$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
$this->placeholder->onResolve($callback);
2016-07-12 18:20:06 +02:00
$this->assertSame(1, $invoked);
$this->assertSame($exception, $result);
}
/**
* @depends testOnResolveAfterFail
2016-07-12 18:20:06 +02:00
*/
public function testMultipleOnResolvesAfterFail() {
2016-07-12 18:20:06 +02:00
$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
$this->placeholder->onResolve($callback);
$this->placeholder->onResolve($callback);
$this->placeholder->onResolve($callback);
2016-07-12 18:20:06 +02:00
$this->assertSame(3, $invoked);
$this->assertSame($exception, $result);
}
/**
* @depends testOnResolveOnSuccess
2016-07-12 18:20:06 +02:00
*/
public function testOnResolveThrowingForwardsToLoopHandlerOnFail() {
Loop::run(function () use (&$invoked) {
2016-07-12 18:20:06 +02:00
$invoked = 0;
$expected = new \Exception;
Loop::setErrorHandler(function ($exception) use (&$invoked, $expected) {
++$invoked;
$this->assertSame($expected, $exception);
});
$callback = function () use ($expected) {
throw $expected;
};
$this->placeholder->onResolve($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 testOnResolveOnSuccess
2016-07-12 18:20:06 +02:00
*/
public function testOnResolveThrowingForwardsToLoopHandlerAfterFail() {
Loop::run(function () use (&$invoked) {
2016-07-12 18:20:06 +02:00
$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
$this->placeholder->onResolve($callback);
2016-07-12 18:20:06 +02:00
});
$this->assertSame(1, $invoked);
}
public function testResolveWithPromiseBeforeOnResolve() {
2016-11-14 20:59:21 +01:00
$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())
->method("onResolve")
2016-07-12 18:20:06 +02:00
->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
$this->placeholder->onResolve(function () {});
2016-07-12 18:20:06 +02:00
}
public function testResolveWithPromiseAfterOnResolve() {
2016-11-14 20:59:21 +01:00
$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())
->method("onResolve")
2016-07-12 18:20:06 +02:00
->with($this->callback("is_callable"));
$this->placeholder->onResolve(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
* @expectedExceptionMessage Promise has already been resolved
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
}
/**
* @expectedException \Error
* @expectedExceptionMessage Promise has already been resolved
*/
public function testResolveAgainWithinOnResolveCallback() {
Loop::run(function () {
$this->placeholder->onResolve(function () {
$this->placeholder->resolve();
});
$this->placeholder->resolve();
});
}
public function testOnResolveWithGenerator() {
$invoked = false;
$this->placeholder->onResolve(function ($exception, $value) use (&$invoked) {
$invoked = true;
return $value;
yield; // Unreachable, but makes function a generator.
});
$this->placeholder->resolve(1);
$this->assertTrue($invoked);
}
/**
* @depends testOnResolveWithGenerator
*/
public function testOnResolveWithGeneratorAfterResolve() {
$this->placeholder->resolve(1);
$invoked = false;
$this->placeholder->onResolve(function ($exception, $value) use (&$invoked) {
$invoked = true;
return $value;
yield; // Unreachable, but makes function a generator.
});
$this->assertTrue($invoked);
}
2016-07-12 18:20:06 +02:00
}