-
Notifications
You must be signed in to change notification settings - Fork 203
/
StreamProcessorTest.php
201 lines (163 loc) · 6.45 KB
/
StreamProcessorTest.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
declare(strict_types=1);
namespace VCR\Tests\Unit\Util;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use VCR\Util\StreamProcessor;
final class StreamProcessorTest extends TestCase
{
/**
* test flock with file_put_contents.
*/
public function testFlockWithFilePutContents(): void
{
$processor = new StreamProcessor();
$processor->intercept();
$testData = 'test data';
$testFilePath = 'tests/fixtures/file_put_contents';
$res = file_put_contents($testFilePath, $testData, \LOCK_EX);
unlink($testFilePath);
$processor->restore();
$this->assertEquals(\strlen($testData), $res);
}
/**
* @dataProvider streamOpenAppendFilterProvider
*/
public function testStreamOpenShouldAppendFilters(bool $expected, int $option, ?bool $shouldProcess = null): void
{
$mock = $this->getMockBuilder('VCR\Util\StreamProcessor')
->disableOriginalConstructor()
->setMethods(['intercept', 'restore', 'appendFiltersToStream', 'shouldProcess'])
->getMock();
if (null !== $shouldProcess) {
$mock->expects($this->once())->method('shouldProcess')->willReturn($shouldProcess);
}
if ($expected) {
$mock->expects($this->once())->method('appendFiltersToStream');
} else {
$mock->expects($this->never())->method('appendFiltersToStream');
}
$mock->stream_open('tests/fixtures/streamprocessor_data', 'r', $option, $fullPath);
$mock->stream_close();
}
/** @return array<array<bool|int>> */
public function streamOpenAppendFilterProvider(): array
{
return [
[true, StreamProcessor::STREAM_OPEN_FOR_INCLUDE, true],
[false, StreamProcessor::STREAM_OPEN_FOR_INCLUDE, false],
[false, 0],
];
}
/** @return array<string[]> */
public function streamOpenFileModesWhichDoNotCreateFiles(): array
{
return [
['r'],
['rb'],
['rt'],
['r+'],
];
}
/**
* @dataProvider streamOpenFileModesWhichDoNotCreateFiles
*/
public function testStreamOpenShouldNotFailOnNonExistingFile(string $fileMode): void
{
$test = $this;
set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($test): void {
$test->fail('should not throw errors');
});
$processor = new StreamProcessor();
$result = $processor->stream_open('tests/fixtures/unknown', $fileMode, StreamProcessor::STREAM_OPEN_FOR_INCLUDE, $fullPath);
$this->assertFalse($result);
restore_error_handler();
}
/**
* @doesNotPerformAssertions
*/
public function testUrlStatSuccessfully(): void
{
$test = $this;
set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($test): void {
$test->fail('should not throw errors');
});
$processor = new StreamProcessor();
$processor->url_stat('tests/fixtures/streamprocessor_data', 0);
restore_error_handler();
}
public function testUrlStatFileNotFound(): void
{
$processor = new StreamProcessor();
$this->expectWarning();
$processor->url_stat('file_not_found', 0);
}
/**
* @doesNotPerformAssertions
*/
public function testQuietUrlStatFileNotFoundToBeQuiet(): void
{
$processor = new StreamProcessor();
$processor->url_stat('file_not_found', \STREAM_URL_STAT_QUIET);
}
public function testDirOpendir(): void
{
$processor = new StreamProcessor();
$this->assertTrue($processor->dir_opendir('tests/fixtures'));
$processor->dir_closedir();
}
public function testDirOpendirNotFound(): void
{
$test = $this;
set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($test): bool {
$test->assertStringContainsString('opendir(not_found', $errstr);
return true;
});
$processor = new StreamProcessor();
$this->assertFalse($processor->dir_opendir('not_found'));
restore_error_handler();
}
public function testMakeDir(): void
{
$mock = $this->getStreamProcessorMock();
$mock->expects($this->exactly(2))->method('restore');
$mock->expects($this->exactly(2))->method('intercept');
$this->assertTrue($mock->mkdir('tests/fixtures/unittest_streamprocessor', 0777, 0));
$this->assertTrue($mock->rmdir('tests/fixtures/unittest_streamprocessor'));
}
public function testRename(): void
{
$mock = $this->getStreamProcessorMock();
$mock->expects($this->exactly(3))->method('restore');
$mock->expects($this->exactly(3))->method('intercept');
$this->assertTrue($mock->mkdir('tests/fixtures/unittest_streamprocessor', 0777, 0));
$this->assertTrue($mock->rename('tests/fixtures/unittest_streamprocessor', 'tests/fixtures/sp'));
$this->assertTrue($mock->rmdir('tests/fixtures/sp'));
}
public function testStreamMetadata(): void
{
if (!\function_exists('posix_getuid')) {
$this->markTestSkipped('Requires "posix_getuid" function.');
}
$mock = $this->getStreamProcessorMock();
$mock->expects($this->exactly(8))->method('restore');
$mock->expects($this->exactly(8))->method('intercept');
$path = 'tests/fixtures/unnitest_streamprocessor_metadata';
$this->assertTrue($mock->stream_metadata($path, \STREAM_META_TOUCH, null));
$this->assertTrue($mock->stream_metadata($path, \STREAM_META_TOUCH, [time(), time()]));
$this->assertTrue($mock->stream_metadata($path, \STREAM_META_OWNER_NAME, posix_getuid()));
$this->assertTrue($mock->stream_metadata($path, \STREAM_META_OWNER, posix_getuid()));
$this->assertTrue($mock->stream_metadata($path, \STREAM_META_GROUP_NAME, posix_getgid()));
$this->assertTrue($mock->stream_metadata($path, \STREAM_META_GROUP, posix_getgid()));
$this->assertTrue($mock->stream_metadata($path, \STREAM_META_ACCESS, 0777));
$this->assertTrue($mock->unlink($path));
}
/** @return StreamProcessor&MockObject */
protected function getStreamProcessorMock()
{
return $this->getMockBuilder(StreamProcessor::class)
->disableOriginalConstructor()
->setMethods(['intercept', 'restore'])
->getMock();
}
}