Skip to content

Commit 96a1f5a

Browse files
authored
Merge pull request #21 from ace411/master
Add assertTrueAboutPromise() and assertFalseAboutPromise()
2 parents 4dc6e7f + f302c92 commit 96a1f5a

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

README.md

+77
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ assertions for testing ReactPHP promises.
1818
- [assertPromiseFulfillsWithInstanceOf()](#assertpromisefulfillswithinstanceof)
1919
- [assertPromiseRejects()](#assertpromiserejects())
2020
- [assertPromiseRejectsWith()](#assertpromiserejectswith)
21+
- [assertTrueAboutPromise](#asserttrueaboutpromise)
22+
- [assertFalseAboutPromise](#assertfalseaboutpromise)
2123

2224
- [Helpers](#helpers)
2325
- [waitForPromiseToFulfill()](#waitforpromisetofulfill)
@@ -267,6 +269,81 @@ Failed asserting that promise rejects with a specified reason.
267269
Failed asserting that LogicException Object (...) is an instance of class "InvalidArgumentException".
268270
```
269271

272+
### assertTrueAboutPromise()
273+
`assertTrueAboutPromise(PromiseInterface $promise, callable $predicate, int $timeout = null): void`
274+
275+
The test fails if the value encapsulated in the Promise does not conform to an arbitrary predicate.
276+
277+
You can specify `$timeout` in seconds to wait for promise to be resolved.
278+
If the promise was not fulfilled in specified timeout, it rejects with `React\Promise\Timer\TimeoutException`.
279+
When not specified, timeout is set to 2 seconds.
280+
281+
```php
282+
final class AssertTrueAboutPromiseTest extends TestCase
283+
{
284+
/** @test */
285+
public function promise_encapsulates_integer(): void
286+
{
287+
$deferred = new Deferred();
288+
$deferred->resolve(23);
289+
290+
$this->assertTrueAboutPromise($deferred->promise(), function ($val) {
291+
return is_object($val);
292+
});
293+
}
294+
}
295+
```
296+
297+
```bash
298+
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.
299+
300+
F 1 / 1 (100%)
301+
302+
Time: 136 ms, Memory: 4.00MB
303+
304+
There was 1 failure:
305+
306+
1) seregazhuk\React\PromiseTesting\tests\AssertTrueAboutPromiseTest::promise_encapsulates_integer
307+
Failed asserting that false is true.
308+
```
309+
310+
### assertFalseAboutPromise()
311+
`assertFalseAboutPromise(PromiseInterface $promise, callable $predicate, int $timeout = null): void`
312+
313+
The test fails if the value encapsulated in the Promise conforms to an arbitrary predicate.
314+
315+
You can specify `$timeout` in seconds to wait for promise to be resolved.
316+
If the promise was not fulfilled in specified timeout, it rejects with `React\Promise\Timer\TimeoutException`.
317+
When not specified, timeout is set to 2 seconds.
318+
319+
```php
320+
final class AssertFalseAboutPromiseTest extends TestCase
321+
{
322+
/** @test */
323+
public function promise_encapsulates_object(): void
324+
{
325+
$deferred = new Deferred();
326+
$deferred->resolve(23);
327+
328+
$this->assertFalseAboutPromise($deferred->promise(), function ($val) {
329+
return is_int($val);
330+
});
331+
}
332+
}
333+
```
334+
335+
```bash
336+
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.
337+
338+
F 1 / 1 (100%)
339+
340+
Time: 136 ms, Memory: 4.00MB
341+
342+
There was 1 failure:
343+
344+
1) seregazhuk\React\PromiseTesting\tests\AssertFalseAboutPromiseTest::promise_encapsulates_object
345+
Failed asserting that true is false.
346+
```
270347

271348
## Helpers
272349

src/AssertsPromise.php

+55
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
}

tests/AboutPromiseTest.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace seregazhuk\React\PromiseTesting\tests;
4+
5+
use Exception;
6+
use React\Promise\Deferred;
7+
use seregazhuk\React\PromiseTesting\TestCase;
8+
9+
final class AboutPromiseTest extends TestCase
10+
{
11+
/** @test */
12+
public function predicate_function_reveals_what_is_true_about_promise(): void
13+
{
14+
try {
15+
$fst = new Deferred;
16+
$fst->resolve(23);
17+
18+
$snd = new Deferred;
19+
$snd->reject(new Exception('An error'));
20+
21+
$thd = new Deferred;
22+
$thd->reject('Another error');
23+
24+
$this->assertTrueAboutPromise($fst->promise(), function ($val): bool {
25+
return is_int($val) && $val > 20;
26+
});
27+
$this->assertTrueAboutPromise($fst->promise(), 'is_string');
28+
29+
$this->assertTrueAboutPromise($snd->promise(), 'is_string');
30+
31+
$this->assertTrueAboutPromise($thd->promise(), 'is_string');
32+
} catch (Exception $exception) {
33+
$this->assertRegExp(
34+
'/Failed asserting that .+/',
35+
$exception->getMessage()
36+
);
37+
}
38+
}
39+
40+
/** @test */
41+
public function predicate_function_reveals_what_is_false_about_promise(): void
42+
{
43+
try {
44+
$fst = new Deferred;
45+
$fst->resolve(23);
46+
47+
$snd = new Deferred;
48+
$snd->reject(new Exception('An error'));
49+
50+
$thd = new Deferred;
51+
$thd->reject('Another error');
52+
53+
$this->assertFalseAboutPromise($fst->promise(), function ($val): bool {
54+
return is_int($val) && $val > 20;
55+
});
56+
$this->assertFalseAboutPromise($fst->promise(), 'is_string');
57+
58+
$this->assertFalseAboutPromise($snd->promise(), 'is_string');
59+
60+
$this->assertFalseAboutPromise($thd->promise(), 'is_int');
61+
} catch (Exception $exception) {
62+
$this->assertRegExp(
63+
'/Failed asserting that .+/',
64+
$exception->getMessage()
65+
);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)