Skip to content

Commit 7ae9728

Browse files
authored
Merge pull request #39 from WyriHaximus-labs/binairy-file-read-write
Test reading and writing from and to binary files
2 parents 65c6d55 + 9ce450a commit 7ae9728

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/ChildProcess/Adapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public function read($fileDescriptor, $length, $offset)
195195
'length' => $length,
196196
'offset' => $offset,
197197
]))->then(function ($payload) {
198-
return \React\Promise\resolve($payload['chunk']);
198+
return \React\Promise\resolve(base64_decode($payload['chunk']));
199199
});
200200
}
201201

@@ -209,7 +209,7 @@ public function read($fileDescriptor, $length, $offset)
209209
public function write($fileDescriptor, $data, $length, $offset)
210210
{
211211
return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('write', [
212-
'chunk' => $data,
212+
'chunk' => base64_encode($data),
213213
'length' => $length,
214214
'offset' => $offset,
215215
]));

src/WoolTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function read(array $payload)
172172
{
173173
fseek($this->fd, $payload['offset']);
174174
return \React\Promise\resolve([
175-
'chunk' => fread($this->fd, $payload['length']),
175+
'chunk' => base64_encode(fread($this->fd, $payload['length'])),
176176
]);
177177
}
178178

@@ -184,7 +184,7 @@ public function write(array $payload)
184184
{
185185
fseek($this->fd, $payload['offset']);
186186
return \React\Promise\resolve([
187-
'written' => fwrite($this->fd, $payload['chunk'], $payload['length']),
187+
'written' => fwrite($this->fd, base64_decode($payload['chunk']), $payload['length']),
188188
]);
189189
}
190190

tests/Adapters/FileTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ public function testGetContents(LoopInterface $loop, FilesystemInterface $filesy
151151
$this->assertSame($contents, $fileContents);
152152
}
153153

154+
/**
155+
* @dataProvider filesystemProvider
156+
*/
157+
public function testGetBinaryContents(LoopInterface $loop, FilesystemInterface $filesystem)
158+
{
159+
$file = __DIR__ . DIRECTORY_SEPARATOR . 'reactphp-logo.png';
160+
$this->assertFileExists($file);
161+
$fileContents = $this->await($filesystem->file($file)->getContents(), $loop);
162+
$this->assertSame(file_get_contents($file), $fileContents);
163+
}
164+
154165
/**
155166
* @dataProvider filesystemProvider
156167
*/
@@ -257,4 +268,16 @@ public function testPutContents(LoopInterface $loop, FilesystemInterface $filesy
257268
$this->assertFileExists($tempFile);
258269
$this->assertSame($contents, file_get_contents($tempFile));
259270
}
271+
/**
272+
* @dataProvider filesystemProvider
273+
*/
274+
public function testPutBinaryContents(LoopInterface $loop, FilesystemInterface $filesystem)
275+
{
276+
$contents = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'reactphp-logo.png');
277+
$filename = uniqid('', true);
278+
$tempFile = $this->tmpDir . $filename;
279+
$this->await($filesystem->file($tempFile)->putContents($contents), $loop);
280+
$this->assertFileExists($tempFile);
281+
$this->assertSame($contents, file_get_contents($tempFile));
282+
}
260283
}

tests/Adapters/reactphp-logo.png

57.5 KB
Loading

0 commit comments

Comments
 (0)