Skip to content

Commit 7112d79

Browse files
authored
Merge pull request #24 from WyriHaximus-labs/readable-stream-trait-emit-close
Emit close event once we reach the end of the stream
2 parents 6006008 + 2b9907e commit 7112d79

File tree

6 files changed

+44
-45
lines changed

6 files changed

+44
-45
lines changed

src/Node/File.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
use React\Filesystem\ObjectStreamSink;
99
use React\Filesystem\Stream\GenericStreamInterface;
1010
use React\Promise\Deferred;
11+
use React\Promise\FulfilledPromise;
12+
use React\Promise\RejectedPromise;
1113
use React\Promise\Stream;
1214
use React\Stream\ReadableStreamInterface;
1315
use React\Stream\WritableStreamInterface;
14-
use React\Promise\FulfilledPromise;
15-
use React\Promise\RejectedPromise;
1616

1717
class File implements FileInterface
1818
{
@@ -149,9 +149,7 @@ public function close()
149149
public function getContents()
150150
{
151151
return $this->open('r')->then(function ($stream) {
152-
return Stream\buffer($stream)->always(function () {
153-
$this->close();
154-
});
152+
return Stream\buffer($stream);
155153
});
156154
}
157155

src/Stream/GenericStreamTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public function close()
9393
}
9494

9595
$this->closed = true;
96-
$this->emit('end', [$this]);
9796

9897
$this->filesystem->close($this->fileDescriptor)->then(function () {
9998
$this->emit('close', [$this]);

src/Stream/ReadableStreamTrait.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ protected function performRead($chunkSize)
9999
if ($this->readCursor < $this->size) {
100100
$this->readChunk();
101101
} else {
102-
$this->emit('end', [
103-
$this,
104-
]);
102+
$this->emit('end', [$this]);
103+
$this->close();
105104
}
106105
});
107106
}

tests/Node/FileTest.php

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use React\Filesystem\Node\Directory;
88
use React\Filesystem\Node\File;
99
use React\Filesystem\ObjectStream;
10+
use React\Filesystem\Stream\ReadableStream;
1011
use React\Promise\Deferred;
1112
use React\Promise\FulfilledPromise;
1213
use React\Promise\RejectedPromise;
@@ -296,27 +297,48 @@ public function testGetContents()
296297
{
297298
$path = 'foo.bar';
298299
$fd = '0123456789abcdef';
299-
$filesystem = $this->mockAdapter();
300300

301-
$stream = $this->getMock('React\Filesystem\Stream\ReadableStream', [
302-
'getFiledescriptor',
303-
'resume',
304-
], [
305-
'foo:bar',
306-
$fd,
307-
$filesystem,
301+
$openPromise = $this->getMock('React\Promise\PromiseInterface', [
302+
'then',
308303
]);
309304

310-
$stream
305+
$filesystem = $this->mockAdapter();
306+
307+
$filesystem
311308
->expects($this->once())
312-
->method('getFiledescriptor')
313-
->with()
314-
->will($this->returnValue($fd))
309+
->method('stat')
310+
->with($path)
311+
->will($this->returnValue(new FulfilledPromise([
312+
'size' => 1,
313+
])))
315314
;
316315

317-
$openPromise = $this->getMock('React\Promise\PromiseInterface', [
318-
'then',
319-
]);
316+
$filesystem
317+
->expects($this->once())
318+
->method('open')
319+
->with($path, 'r')
320+
->will($this->returnValue($openPromise))
321+
;
322+
323+
$filesystem
324+
->expects($this->once())
325+
->method('read')
326+
->with($fd, 1, 0)
327+
->will($this->returnValue(new FulfilledPromise(str_repeat('a', 1))))
328+
;
329+
330+
$filesystem
331+
->expects($this->once())
332+
->method('close')
333+
->with($fd)
334+
->will($this->returnValue(new FulfilledPromise()))
335+
;
336+
337+
$stream = new ReadableStream(
338+
$path,
339+
$fd,
340+
$filesystem
341+
);
320342

321343
$openPromise
322344
->expects($this->once())
@@ -327,13 +349,6 @@ public function testGetContents()
327349
}))
328350
;
329351

330-
$filesystem
331-
->expects($this->once())
332-
->method('open')
333-
->with($path, 'r')
334-
->will($this->returnValue($openPromise))
335-
;
336-
337352
$getContentsPromise = (new File($path, Filesystem::createFromAdapter($filesystem)))->getContents();
338353
$this->assertInstanceOf('React\Promise\PromiseInterface', $getContentsPromise);
339354
}

tests/Stream/ReadableStreamTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,11 @@ public function testClose($className, $stat)
137137
$stream
138138
->expects($this->at(0))
139139
->method('emit')
140-
->with('end', [$stream])
141-
;
142-
143-
$stream
144-
->expects($this->at(1))
145-
->method('emit')
146140
->with('close', [$stream])
147141
;
148142

149143
$stream
150-
->expects($this->at(2))
144+
->expects($this->at(1))
151145
->method('removeAllListeners')
152146
->with()
153147
;

tests/Stream/WritableStreamTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,11 @@ public function testClose($className)
194194
$stream
195195
->expects($this->at(0))
196196
->method('emit')
197-
->with('end', [$stream])
198-
;
199-
200-
$stream
201-
->expects($this->at(1))
202-
->method('emit')
203197
->with('close', [$stream])
204198
;
205199

206200
$stream
207-
->expects($this->at(2))
201+
->expects($this->at(1))
208202
->method('removeAllListeners')
209203
->with()
210204
;

0 commit comments

Comments
 (0)