Skip to content

Commit f302c92

Browse files
committed
Move assertions to trait
1 parent f0a2e3b commit f302c92

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/AssertsPromise.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,59 @@ public function eventLoop(): LoopInterface
143143

144144
return $this->loop;
145145
}
146+
147+
/**
148+
* @param PromiseInterface $promise
149+
* @param callable $predicate
150+
* @param int|null $timeout
151+
* @throws AssertionFailedError
152+
*/
153+
public function assertTrueAboutPromise(
154+
PromiseInterface $promise,
155+
callable $predicate,
156+
int $timeout = null
157+
): void {
158+
$this->assertAboutPromise($promise, $predicate, $timeout);
159+
}
160+
161+
/**
162+
* @param PromiseInterface $promise
163+
* @param callable $predicate
164+
* @param int|null $timeout
165+
* @throws AssertionFailedError
166+
*/
167+
public function assertFalseAboutPromise(
168+
PromiseInterface $promise,
169+
callable $predicate,
170+
int $timeout = null
171+
): void {
172+
$this->assertAboutPromise($promise, $predicate, $timeout, false);
173+
}
174+
175+
/**
176+
* @param PromiseInterface $promise
177+
* @param callable $predicate
178+
* @param int|null $timeout
179+
* @param bool $assertTrue
180+
* @throws AssertionFailedError
181+
*/
182+
private function assertAboutPromise(
183+
PromiseInterface $promise,
184+
callable $predicate,
185+
int $timeout = null,
186+
bool $assertTrue = true
187+
): void {
188+
$result = $assertTrue ? false : true;
189+
$this->addToAssertionCount(1);
190+
191+
try {
192+
$result = $predicate($this->waitForPromise($promise, $timeout));
193+
} catch (TimeoutException $exception) {
194+
$this->fail('Promise was cancelled due to timeout');
195+
} catch (Exception $exception) {
196+
$this->fail('Failed asserting that promise was fulfilled. Promise was rejected');
197+
}
198+
199+
$assertTrue ? $this->assertTrue($result) : $this->assertFalse($result);
200+
}
146201
}

0 commit comments

Comments
 (0)