-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathTestCase.php
More file actions
118 lines (101 loc) · 2.93 KB
/
TestCase.php
File metadata and controls
118 lines (101 loc) · 2.93 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
<?php
namespace React\Tests\Filesystem;
use Clue\React\Block;
use React\EventLoop\LoopInterface;
use React\Promise\PromiseInterface;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
class TestCase extends PHPUnitTestCase
{
const TIMEOUT = 30;
protected $tmpDir;
protected $startTime;
protected function mockAdapter(LoopInterface $loop = null)
{
if ($loop === null) {
$loop = $this->getMock('React\EventLoop\LoopInterface');
}
$mock = $this->getMock('React\Filesystem\AdapterInterface', [
'__construct',
'getLoop',
'getFilesystem',
'setFilesystem',
'getInvoker',
'setInvoker',
'callFilesystem',
'isSupported',
'mkdir',
'rmdir',
'unlink',
'chmod',
'chown',
'stat',
'ls',
'lsStream',
'touch',
'open',
'read',
'write',
'close',
'rename',
'readlink',
'symlink',
'detectType',
], [
$loop,
]);
$mock
->expects($this->any())
->method('getLoop')
->with()
->will($this->returnValue($loop))
;
return $mock;
}
public function setUp()
{
$this->tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'react-filesystem-tests' . DIRECTORY_SEPARATOR . uniqid('', true) . DIRECTORY_SEPARATOR;
mkdir($this->tmpDir, 0777, true);
$this->startTime = time();
}
protected function checkIfTimedOut($maxExecutionTime = self::TIMEOUT)
{
if (($this->startTime + $maxExecutionTime) <= time()) {
$this->fail('Manual timeout');
}
}
protected function setLoopTimeout(LoopInterface $loop, $maxExecutionTime = self::TIMEOUT)
{
$loop->addTimer($maxExecutionTime, function () use ($loop) {
$loop->stop();
$this->fail('Event loop timeout');
});
}
public function tearDown()
{
$this->rmdir($this->tmpDir);
}
protected function rmdir($dir)
{
$directory = dir($dir);
while (false !== ($entry = $directory->read())) {
if (in_array($entry, ['.', '..'])) {
continue;
}
if (is_dir($dir . $entry)) {
$this->rmdir($dir . $entry . DIRECTORY_SEPARATOR);
continue;
}
if (is_file($dir . $entry)) {
unlink($dir . $entry);
continue;
}
}
$directory->close();
}
protected function await(PromiseInterface $promise, LoopInterface $loop, $timeout = self::TIMEOUT)
{
$result = Block\await($promise, $loop, $timeout);
$loop->run(); // Ensure we let the loop run it's course to clean up
return $result;
}
}