-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAsyncTestCaseTest.php
More file actions
178 lines (134 loc) · 4.65 KB
/
AsyncTestCaseTest.php
File metadata and controls
178 lines (134 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php declare(strict_types=1);
namespace Amp\PHPUnit;
use Amp\DeferredFuture;
use Amp\Future;
use PHPUnit\Framework\AssertionFailedError;
use Revolt\EventLoop;
use function Amp\async;
use function Amp\delay;
class AsyncTestCaseTest extends AsyncTestCase
{
public function testThatMethodRunsInEventLoopContext(): Future
{
$returnDeferred = new DeferredFuture; // make sure our test runs to completion
$testDeferred = new DeferredFuture; // used by our defer callback to ensure we're running on the Loop
EventLoop::queue(function () use ($testDeferred, $returnDeferred): void {
$data = $testDeferred->getFuture()->await();
self::assertSame('foobar', $data);
$returnDeferred->complete();
});
EventLoop::queue(function () use ($testDeferred): void {
$testDeferred->complete('foobar');
});
return $returnDeferred->getFuture();
}
public function testHandleNullReturn(): void
{
$testDeferred = new DeferredFuture;
$testData = new \stdClass;
$testData->val = 0;
EventLoop::defer(function () use ($testData, $testDeferred) {
$testData->val++;
$testDeferred->complete();
});
$testDeferred->getFuture()->await();
self::assertSame(1, $testData->val);
}
public function testReturningFuture(): Future
{
$deferred = new DeferredFuture;
EventLoop::delay(0.1, fn () => $deferred->complete('value'));
$returnValue = $deferred->getFuture();
$this->expectNotToPerformAssertions();
return $returnValue; // Return value used by testReturnValueFromDependentTest
}
public function testExpectingAnExceptionThrown(): Future
{
$throwException = function (): void {
delay(0.1);
throw new TestException('threw the error');
};
$this->expectException(TestException::class);
$this->expectExceptionMessage('threw the error');
return async($throwException);
}
public function testExpectingAnErrorThrown(): Future
{
$this->expectException(\Error::class);
return async(function (): void {
throw new \Error;
});
}
public static function provideArguments(): array
{
return [
['foo', 42, true],
];
}
/**
* @dataProvider provideArguments
*/
public function testArgumentSupport(string $foo, int $bar, bool $baz): void
{
self::assertSame('foo', $foo);
self::assertSame(42, $bar);
self::assertTrue($baz);
}
/**
* @depends testReturningFuture
*/
public function testReturnValueFromDependentTest(string $value = null): void
{
self::assertSame('value', $value);
}
public function testSetTimeout(): void
{
$this->setTimeout(0.5);
$this->expectNotToPerformAssertions();
delay(0.25);
}
public function testSetTimeoutReplace(): void
{
$this->setTimeout(0.5);
$this->setTimeout(1);
delay(0.75);
$this->expectNotToPerformAssertions();
}
public function testSetTimeoutWithFuture(): Future
{
$this->setTimeout(0.1);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Expected test to complete before 0.100s time limit');
$deferred = new DeferredFuture;
EventLoop::delay(0.2, fn () => $deferred->complete());
return $deferred->getFuture();
}
public function testSetTimeoutWithAwait(): void
{
$this->setTimeout(0.1);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Expected test to complete before 0.100s time limit');
delay(0.2);
}
public function testSetMinimumRunTime(): void
{
$this->setMinimumRuntime(1);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessageMatches("/Expected test to take at least 1.000s but instead took 0.\d{3}s/");
delay(0.5);
}
public function testCreateCallback(): void
{
$mock = $this->createCallback(1, fn (int $value) => $value + 1, [1]);
self::assertSame(2, $mock(1));
}
public function testThrowToEventLoop(): void
{
$this->setTimeout(0.1);
EventLoop::queue(static fn () => throw new TestException('message'));
$this->expectException(UnhandledException::class);
$pattern = "/(.+) thrown to event loop error handler: (.*)/";
$this->expectExceptionMessageMatches($pattern);
(new DeferredFuture)->getFuture()->await();
}
}