Skip to content

Loop::get() to prevent loop duplication #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions src/AssertsPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Clue\React\Block;
use Exception;
use PHPUnit\Framework\AssertionFailedError;
use React\EventLoop\Factory as LoopFactory;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use React\Promise\Timer\TimeoutException;
Expand All @@ -28,9 +28,9 @@ public function assertPromiseFulfills(PromiseInterface $promise, int $timeout =

try {
$this->waitForPromise($promise, $timeout);
} catch (TimeoutException $exception) {
} catch (TimeoutException) {
$this->fail($failMessage . 'Promise was cancelled due to timeout.');
} catch (Exception $exception) {
} catch (Exception) {
$this->fail($failMessage . 'Promise was rejected.');
}
}
Expand All @@ -41,17 +41,17 @@ public function assertPromiseFulfills(PromiseInterface $promise, int $timeout =
* @param int|null $timeout
* @throws AssertionFailedError
*/
public function assertPromiseFulfillsWith(PromiseInterface $promise, $value, int $timeout = null): void
public function assertPromiseFulfillsWith(PromiseInterface $promise, mixed $value, int $timeout = null): void
{
$failMessage = 'Failed asserting that promise fulfills with a specified value. ';
$result = null;
$this->addToAssertionCount(1);

try {
$result = $this->waitForPromise($promise, $timeout);
} catch (TimeoutException $exception) {
} catch (TimeoutException) {
$this->fail($failMessage . 'Promise was cancelled due to timeout.');
} catch (Exception $exception) {
} catch (Exception) {
$this->fail($failMessage . 'Promise was rejected.');
}

Expand All @@ -69,9 +69,9 @@ public function assertPromiseFulfillsWithInstanceOf(PromiseInterface $promise, s

try {
$result = $this->waitForPromise($promise, $timeout);
} catch (TimeoutException $exception) {
} catch (TimeoutException) {
$this->fail($failMessage . 'Promise was cancelled due to timeout.');
} catch (Exception $exception) {
} catch (Exception) {
$this->fail($failMessage . 'Promise was rejected.');
}

Expand All @@ -89,7 +89,7 @@ public function assertPromiseRejects(PromiseInterface $promise, int $timeout = n

try {
$this->waitForPromise($promise, $timeout);
} catch (Exception $exception) {
} catch (Exception) {
return;
}

Expand All @@ -115,10 +115,11 @@ public function assertPromiseRejectsWith(PromiseInterface $promise, string $reas
}

/**
* @throws Exception
* @param PromiseInterface $promise
* @param int|null $timeout
* @return mixed
*/
public function waitForPromiseToFulfill(PromiseInterface $promise, int $timeout = null)
public function waitForPromiseToFulfill(PromiseInterface $promise, int $timeout = null): mixed
{
try {
return $this->waitForPromise($promise, $timeout);
Expand All @@ -129,18 +130,20 @@ public function waitForPromiseToFulfill(PromiseInterface $promise, int $timeout
}

/**
* @param PromiseInterface $promise
* @param int|null $timeout
* @return mixed
* @throws Exception
*/
public function waitForPromise(PromiseInterface $promise, int $timeout = null)
public function waitForPromise(PromiseInterface $promise, int $timeout = null): mixed
{
return Block\await($promise, $this->eventLoop(), $timeout ?: $this->defaultWaitTimeout);
}

public function eventLoop(): LoopInterface
{
if (! $this->loop) {
$this->loop = LoopFactory::create();
$this->loop = Loop::get();
}

return $this->loop;
Expand All @@ -154,9 +157,10 @@ public function eventLoop(): LoopInterface
*/
public function assertTrueAboutPromise(
PromiseInterface $promise,
callable $predicate,
int $timeout = null
): void {
callable $predicate,
int $timeout = null
): void
{
$this->assertAboutPromise($promise, $predicate, $timeout);
}

Expand All @@ -168,9 +172,10 @@ public function assertTrueAboutPromise(
*/
public function assertFalseAboutPromise(
PromiseInterface $promise,
callable $predicate,
int $timeout = null
): void {
callable $predicate,
int $timeout = null
): void
{
$this->assertAboutPromise($promise, $predicate, $timeout, false);
}

Expand All @@ -183,18 +188,19 @@ public function assertFalseAboutPromise(
*/
private function assertAboutPromise(
PromiseInterface $promise,
callable $predicate,
int $timeout = null,
bool $assertTrue = true
): void {
callable $predicate,
int $timeout = null,
bool $assertTrue = true
): void
{
$result = $assertTrue ? false : true;
$this->addToAssertionCount(1);

try {
$result = $predicate($this->waitForPromise($promise, $timeout));
} catch (TimeoutException $exception) {
} catch (TimeoutException) {
$this->fail('Promise was cancelled due to timeout');
} catch (Exception $exception) {
} catch (Exception) {
$this->fail('Failed asserting that promise was fulfilled. Promise was rejected');
}

Expand Down