|
2 | 2 |
|
3 | 3 | namespace seregazhuk\React\PromiseTesting;
|
4 | 4 |
|
5 |
| -use PHPUnit\Framework\AssertionFailedError; |
6 | 5 | use PHPUnit\Framework\TestCase as PHPUnitTestCase;
|
7 |
| -use React\EventLoop\LoopInterface; |
8 |
| -use React\Promise\PromiseInterface; |
9 |
| -use Clue\React\Block; |
10 |
| -use Exception; |
11 |
| -use React\EventLoop\Factory as LoopFactory; |
12 |
| -use React\Promise\Timer\TimeoutException; |
13 | 6 |
|
14 | 7 | abstract class TestCase extends PHPUnitTestCase
|
15 | 8 | {
|
16 |
| - private const DEFAULT_WAIT_TIMEOUT = 2; |
17 |
| - |
18 |
| - private $loop; |
| 9 | + use AssertPromise; |
19 | 10 |
|
20 | 11 | protected function setUp(): void
|
21 | 12 | {
|
22 |
| - $this->loop = LoopFactory::create(); |
23 |
| - } |
24 |
| - |
25 |
| - /** |
26 |
| - * @param PromiseInterface $promise |
27 |
| - * @param int|null $timeout seconds to wait for resolving |
28 |
| - * @throws AssertionFailedError |
29 |
| - */ |
30 |
| - public function assertPromiseFulfills(PromiseInterface $promise, int $timeout = null): void |
31 |
| - { |
32 |
| - $failMessage = 'Failed asserting that promise fulfills. '; |
33 |
| - $this->addToAssertionCount(1); |
34 |
| - |
35 |
| - try { |
36 |
| - $this->waitForPromise($promise, $timeout); |
37 |
| - } catch (TimeoutException $exception) { |
38 |
| - $this->fail($failMessage . 'Promise was cancelled due to timeout.'); |
39 |
| - } catch (Exception $exception) { |
40 |
| - $this->fail($failMessage . 'Promise was rejected.'); |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - /** |
45 |
| - * @param PromiseInterface $promise |
46 |
| - * @param mixed $value |
47 |
| - * @param int|null $timeout |
48 |
| - * @throws AssertionFailedError |
49 |
| - */ |
50 |
| - public function assertPromiseFulfillsWith(PromiseInterface $promise, $value, int $timeout = null): void |
51 |
| - { |
52 |
| - $failMessage = 'Failed asserting that promise fulfills with a specified value. '; |
53 |
| - $result = null; |
54 |
| - $this->addToAssertionCount(1); |
55 |
| - |
56 |
| - try { |
57 |
| - $result = $this->waitForPromise($promise, $timeout); |
58 |
| - } catch (TimeoutException $exception) { |
59 |
| - $this->fail($failMessage . 'Promise was cancelled due to timeout.'); |
60 |
| - } catch (Exception $exception) { |
61 |
| - $this->fail($failMessage . 'Promise was rejected.'); |
62 |
| - } |
63 |
| - |
64 |
| - $this->assertEquals($value, $result, $failMessage); |
65 |
| - } |
66 |
| - |
67 |
| - /** |
68 |
| - * @throws AssertionFailedError |
69 |
| - */ |
70 |
| - public function assertPromiseFulfillsWithInstanceOf(PromiseInterface $promise, string $class, int $timeout = null): void |
71 |
| - { |
72 |
| - $failMessage = "Failed asserting that promise fulfills with a value of class $class. "; |
73 |
| - $result = null; |
74 |
| - $this->addToAssertionCount(1); |
75 |
| - |
76 |
| - try { |
77 |
| - $result = $this->waitForPromise($promise, $timeout); |
78 |
| - } catch (TimeoutException $exception) { |
79 |
| - $this->fail($failMessage . 'Promise was cancelled due to timeout.'); |
80 |
| - } catch (Exception $exception) { |
81 |
| - $this->fail($failMessage . 'Promise was rejected.'); |
82 |
| - } |
83 |
| - |
84 |
| - $this->assertInstanceOf($class, $result, $failMessage); |
85 |
| - } |
86 |
| - |
87 |
| - /** |
88 |
| - * @param PromiseInterface $promise |
89 |
| - * @param int|null $timeout |
90 |
| - * @throws AssertionFailedError |
91 |
| - */ |
92 |
| - public function assertPromiseRejects(PromiseInterface $promise, int $timeout = null): void |
93 |
| - { |
94 |
| - $this->addToAssertionCount(1); |
95 |
| - |
96 |
| - try { |
97 |
| - $this->waitForPromise($promise, $timeout); |
98 |
| - } catch (Exception $exception) { |
99 |
| - return; |
100 |
| - } |
101 |
| - |
102 |
| - $this->fail('Failed asserting that promise rejects. Promise was fulfilled.'); |
103 |
| - } |
104 |
| - |
105 |
| - /** |
106 |
| - * @throws AssertionFailedError |
107 |
| - */ |
108 |
| - public function assertPromiseRejectsWith(PromiseInterface $promise, string $reasonExceptionClass, int $timeout = null): void |
109 |
| - { |
110 |
| - try { |
111 |
| - $this->waitForPromise($promise, $timeout); |
112 |
| - } catch (Exception $reason) { |
113 |
| - $this->assertInstanceOf( |
114 |
| - $reasonExceptionClass, $reason, 'Failed asserting that promise rejects with a specified reason.' |
115 |
| - ); |
116 |
| - } |
117 |
| - |
118 |
| - $this->fail('Failed asserting that promise rejects. Promise was fulfilled.'); |
119 |
| - } |
120 |
| - |
121 |
| - /** |
122 |
| - * @throws Exception |
123 |
| - * @return mixed |
124 |
| - */ |
125 |
| - public function waitForPromiseToFulfill(PromiseInterface $promise, int $timeout = null) |
126 |
| - { |
127 |
| - try { |
128 |
| - return $this->waitForPromise($promise, $timeout); |
129 |
| - } catch (Exception $exception) { |
130 |
| - $reason = get_class($exception); |
131 |
| - $this->fail("Failed to fulfill a promise. It was rejected with {$reason}."); |
132 |
| - } |
133 |
| - } |
134 |
| - |
135 |
| - /** |
136 |
| - * @return mixed |
137 |
| - * @throws Exception |
138 |
| - */ |
139 |
| - public function waitForPromise(PromiseInterface $promise, int $timeout = null) |
140 |
| - { |
141 |
| - return Block\await($promise, $this->loop, $timeout ?: self::DEFAULT_WAIT_TIMEOUT); |
142 |
| - } |
| 13 | + parent::setUp(); |
143 | 14 |
|
144 |
| - public function eventLoop(): LoopInterface |
145 |
| - { |
146 |
| - return $this->loop; |
| 15 | + $this->eventLoop(); |
147 | 16 | }
|
148 | 17 | }
|
0 commit comments