seergazhuk / react-promise-testing
基于PHPUnit的用于测试ReactPHP承诺的库
Requires
- php: >=8.0
- clue/block-react: ^1.4
- phpunit/php-code-coverage: ^9.2
- phpunit/phpunit: ^9.5
- react/promise: ^2.8
README
一个提供一组方便的断言用于测试ReactPHP承诺的库。底层使用clue/php-block-react来阻塞承诺。
在测试异步代码和承诺时,事情可能会变得有些棘手。这个库提供了一组方便的断言用于测试ReactPHP承诺。
目录
安装
依赖
此库需要PHP 8.0或更高版本。
安装此库推荐使用Composer。 你是Composer的新手吗?
有关版本升级的详细信息,请参阅变更日志。
composer require seregazhuk/react-promise-testing --dev
快速开始
使用特质seregazhuk\React\PromiseTesting\AssertsPromise
或从seregazhuk\React\PromiseTesting\TestCase
类扩展您的测试类,该类本身扩展了PHPUnit TestCase
。
final class MyTest extends TestCase { /** @test */ public function promise_fulfills_with_a_response_object() { $browser = new Clue\React\Buzz\Browser($this->eventLoop()); $promise = $browser->get('http://www.google.com/'); $this->assertPromiseFulfillsWithInstanceOf($promise, ResponseInterface::class); } }
使用特质
use PHPUnit\Framework\TestCase; use seregazhuk\React\PromiseTesting\AssertsPromise; final class MyTest extends TestCase { use AssertsPromise; /** @test */ public function promise_fulfills_with_a_response_object() { $browser = new Clue\React\Buzz\Browser($this->eventLoop()); $promise = $browser->get('http://www.google.com/'); $this->assertPromiseFulfillsWithInstanceOf($promise, ResponseInterface::class); } }
上面的测试检查指定的承诺是否通过ResponseInterface
实例完成。
事件循环
为了进行承诺断言,我们需要运行循环。在每个测试之前,都会创建一个新的事件循环实例(在setUp()
方法中)。如果您需要循环来构建依赖项,您应该使用eventLoop()
方法来检索它。
断言
assertPromiseFulfills()
public function assertPromiseFulfills(PromiseInterface $promise, int $timeout = null): void
如果$promise
拒绝,则测试失败。
您可以在秒数中指定$timeout
以等待承诺解决。如果在指定的超时时间内承诺没有完成,则测试失败。未指定时,超时设置为2秒。
final class PromiseFulfillsTest extends TestCase { /** @test */ public function promise_fulfills(): void { $deferred = new Deferred(); $deferred->reject(); $this->assertPromiseFulfills($deferred->promise(), 1); } }
PHPUnit 8.5.2 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 189 ms, Memory: 4.00MB There was 1 failure: 1) seregazhuk\React\PromiseTesting\tests\PromiseFulfillTest::promise_fulfills Failed asserting that promise fulfills. Promise was rejected.
assertPromiseFulfillsWith()
assertPromiseFulfillsWith(PromiseInterface $promise, $value, int $timeout = null): void
如果$promise
没有使用指定的$value
完成,则测试失败。
您可以在秒数中指定$timeout
以等待承诺完成。如果在指定的超时时间内承诺没有完成,则测试失败。未指定时,超时设置为2秒。
final class PromiseFulfillsWithTest extends TestCase { /** @test */ public function promise_fulfills_with_a_specified_value(): void { $deferred = new Deferred(); $deferred->resolve(1234); $this->assertPromiseFulfillsWith($deferred->promise(), 1); } }
PHPUnit 8.5.2 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 180 ms, Memory: 4.00MB There was 1 failure: 1) seregazhuk\React\PromiseTesting\tests\PromiseFulfillsWithTest::promise_fulfills_with_a_specified_value Failed asserting that promise fulfills with a specified value. Failed asserting that 1234 matches expected 1.
assertPromiseFulfillsWithInstanceOf()
assertPromiseFulfillsWithInstanceOf(PromiseInterface $promise, string $class, int $timeout = null): void
如果$promise
没有使用指定的$class
完成,则测试失败。
您可以在秒数中指定$timeout
以等待承诺完成。如果在指定的超时时间内承诺没有完成,则测试失败。未指定时,超时设置为2秒。
final class PromiseFulfillsWithInstanceOfTest extends TestCase { /** @test */ public function promise_fulfills_with_an_instance_of_class(): void { $deferred = new Deferred(); $deferred->resolve(new MyClass); $this->assertPromiseFulfillsWithInstanceOf($deferred->promise(), MyClass::class); } }
PHPUnit 8.5.2 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 180 ms, Memory: 4.00MB There was 1 failure: 1) seregazhuk\React\PromiseTesting\tests\PromiseFulfillsWithWithInstanceOfTest::promise_fulfills_with_an_instance_of_class Failed asserting that promise fulfills with a value of class MyClass.
assertPromiseRejects()
assertPromiseRejects(PromiseInterface $promise, int $timeout = null): void
如果$promise
完成,则测试失败。
您可以在秒数中指定$timeout
以等待承诺解决。如果在指定的超时时间内承诺没有完成,它将使用React\Promise\Timer\TimeoutException
拒绝。未指定时,超时设置为2秒。
final class PromiseRejectsTest extends TestCase { /** @test */ public function promise_rejects(): void { $deferred = new Deferred(); $deferred->resolve(); $this->assertPromiseRejects($deferred->promise()); } }
PHPUnit 8.5.2 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 175 ms, Memory: 4.00MB There was 1 failure: 1) seregazhuk\React\PromiseTesting\tests\PromiseRejectsTest::promise_rejects Failed asserting that promise rejects. Promise was fulfilled.
assertPromiseRejectsWith()
assertPromiseRejectsWith(PromiseInterface $promise, string $reasonExceptionClass, int $timeout = null): void
如果$promise
没有使用指定的异常类拒绝,则测试失败。
您可以在秒数中指定$timeout
以等待承诺解决。如果在指定的超时时间内承诺没有完成,它将使用React\Promise\Timer\TimeoutException
拒绝。未指定时,超时设置为2秒。
final class PromiseRejectsWithTest extends TestCase { /** @test */ public function promise_rejects_with_a_specified_reason(): void { $deferred = new Deferred(); $deferred->reject(new \LogicException()); $this->assertPromiseRejectsWith($deferred->promise(), \InvalidArgumentException::class); } }
PHPUnit 8.5.2 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 136 ms, Memory: 4.00MB There was 1 failure: 1) seregazhuk\React\PromiseTesting\tests\PromiseRejectsWithTest::promise_rejects_with_a_specified_reason Failed asserting that promise rejects with a specified reason. Failed asserting that LogicException Object (...) is an instance of class "InvalidArgumentException".
assertTrueAboutPromise()
assertTrueAboutPromise(PromiseInterface $promise, callable $predicate, int $timeout = null): void
如果Promise封装的值不符合任意谓词,则测试失败。
您可以在秒数中指定$timeout
以等待承诺解决。如果在指定的超时时间内承诺没有完成,它将使用React\Promise\Timer\TimeoutException
拒绝。未指定时,超时设置为2秒。
final class AssertTrueAboutPromiseTest extends TestCase { /** @test */ public function promise_encapsulates_integer(): void { $deferred = new Deferred(); $deferred->resolve(23); $this->assertTrueAboutPromise($deferred->promise(), function ($val) { return is_object($val); }); } }
PHPUnit 8.5.2 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 136 ms, Memory: 4.00MB There was 1 failure: 1) seregazhuk\React\PromiseTesting\tests\AssertTrueAboutPromiseTest::promise_encapsulates_integer Failed asserting that false is true.
assertFalseAboutPromise()
boolean assertFalseAboutPromise(PromiseInterface $promise, callable $predicate, int $timeout = null): void
如果Promise封装的值符合任意谓词,则测试失败。
您可以在秒数中指定$timeout
以等待承诺解决。如果在指定的超时时间内承诺没有完成,它将使用React\Promise\Timer\TimeoutException
拒绝。未指定时,超时设置为2秒。
final class AssertFalseAboutPromiseTest extends TestCase { /** @test */ public function promise_encapsulates_object(): void { $deferred = new Deferred(); $deferred->resolve(23); $this->assertFalseAboutPromise($deferred->promise(), function ($val) { return is_int($val); }); } }
PHPUnit 8.5.2 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 136 ms, Memory: 4.00MB There was 1 failure: 1) seregazhuk\React\PromiseTesting\tests\AssertFalseAboutPromiseTest::promise_encapsulates_object Failed asserting that true is false.
辅助函数
waitForPromiseToFulfill()
function waitForPromiseToFulfill(PromiseInterface $promise, int $timeout = null)
.
当您想解析一个Promise并获取其解析值时,可以使用此辅助函数。
尝试在指定的$timeout
秒内解析一个$promise
,并返回解析值。如果未设置$timeout
,则默认为2秒。如果$promise
未解析,则测试失败。
final class WaitForPromiseToFulfillTest extends TestCase { /** @test */ public function promise_fulfills(): void { $deferred = new Deferred(); $deferred->reject(new \Exception()); $value = $this->waitForPromiseToFulfill($deferred->promise()); } }
PHPUnit 8.5.2 by Sebastian Bergmann and contributors. F 1 / 1 (100%) Time: 223 ms, Memory: 6.00MB There was 1 failure: 1) seregazhuk\React\PromiseTesting\tests\WaitForPromiseToFulfillTest::promise_fulfills Failed to fulfill a promise. It was rejected with Exception.
waitForPromise()
function waitForPromise(PromiseInterface $promise, int $timeout = null)
.
尝试在指定的$timeout
秒内解析指定的$promise
。如果未设置$timeout
,则默认为2秒。如果Promise解析成功,则返回解析值;如果Promise拒绝,则抛出拒绝原因;如果Promise在指定的$timeout
内未解析,则抛出React\Promise\Timer\TimeoutException
。
当您需要以同步方式从已解析的Promise获取值时,此辅助函数可能很有用。
$value = $this->waitForPromise($cache->get('key'));