/
AsyncTestCaseTest.php
137 lines (113 loc) · 3.74 KB
/
AsyncTestCaseTest.php
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
<?php
namespace Amp\PHPUnit\Test;
use Amp\Deferred;
use Amp\Delayed;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Promise;
use PHPUnit\Framework\AssertionFailedError;
use function Amp\call;
class AsyncTestCaseTest extends AsyncTestCase
{
public function testThatMethodRunsInLoopContext(): Promise
{
$returnDeferred = new Deferred(); // make sure our test runs to completion
$testDeferred = new Deferred(); // used by our defer callback to ensure we're running on the Loop
$testDeferred->promise()->onResolve(function ($err = null, $data = null) use ($returnDeferred) {
$this->assertEquals('foobar', $data, 'Expected the data to be what was resolved in Loop::defer');
$returnDeferred->resolve();
});
Loop::defer(function () use ($testDeferred) {
$testDeferred->resolve('foobar');
});
return $returnDeferred->promise();
}
public function testThatWeHandleNotPromiseReturned(): \Generator
{
$testDeferred = new Deferred();
$testData = new \stdClass();
$testData->val = null;
Loop::defer(function () use ($testData, $testDeferred) {
$testData->val = true;
$testDeferred->resolve();
});
yield $testDeferred->promise();
$this->assertTrue($testData->val, 'Expected our test to run on loop to completion');
}
public function testExpectingAnExceptionThrown(): \Generator
{
$throwException = function () {
return call(function () {
yield new Delayed(100);
throw new \Exception('threw the error');
});
};
$this->expectException(\Exception::class);
$this->expectExceptionMessage('threw the error');
yield $throwException();
}
public function testNestedLoop()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('threw the error');
Loop::run(function () {
throw new \Exception('threw the error');
});
}
public function testExpectingAnErrorThrown(): \Generator
{
$this->expectException(\Error::class);
yield call(function () {
throw new \Error;
});
}
public function argumentSupportProvider(): array
{
return [
['foo', 42, true],
];
}
/**
* @param string $foo
* @param int $bar
* @param bool $baz
*
* @dataProvider argumentSupportProvider
*/
public function testArgumentSupport(string $foo, int $bar, bool $baz)
{
$this->assertSame('foo', $foo);
$this->assertSame(42, $bar);
$this->assertTrue($baz);
}
public function testSetTimeout(): \Generator
{
$this->setTimeout(100);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Expected test to complete before 100ms time limit');
yield new Delayed(200);
}
public function testSetMinimumRunTime(): \Generator
{
$this->setMinimumRuntime(100);
$func = function () {
yield new Delayed(75);
return 'finished';
};
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessageRegExp("/Expected test to take at least 100ms but instead took (\d+)ms/");
yield call($func);
}
public function testSetMinimumRunTimeWithWatchersOnly()
{
$this->setMinimumRuntime(100);
Loop::delay(100, $this->createCallback(1));
}
public function testCreateCallback()
{
$mock = $this->createCallback(1, function (int $value): int {
return $value + 1;
});
$this->assertSame(2, $mock(1));
}
}