Skip to content

Commit 5ca5146

Browse files
authored
Merge pull request #57 from CharlotteDunoisLabs/ls-fix
Rename ls to lsStream and make interface-compatible ls method
2 parents 9637fed + 0a22d50 commit 5ca5146

File tree

8 files changed

+106
-7
lines changed

8 files changed

+106
-7
lines changed

src/AdapterInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\Filesystem;
44

5+
use React\Filesystem\ObjectStream;
56
use React\EventLoop\LoopInterface;
67
use React\Promise\PromiseInterface;
78

@@ -111,6 +112,14 @@ public function stat($filename);
111112
*/
112113
public function ls($path);
113114

115+
/**
116+
* List contents of the given path.
117+
*
118+
* @param string $path
119+
* @return ObjectStream
120+
*/
121+
public function lsStream($path);
122+
114123
/**
115124
* Touch the given path, either creating a file, or updating mtime on the file.
116125
*

src/ChildProcess/Adapter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Exception;
77
use React\EventLoop\LoopInterface;
88
use React\Filesystem\ObjectStream;
9+
use React\Filesystem\ObjectStreamSink;
910
use React\Filesystem\AdapterInterface;
1011
use React\Filesystem\CallInvokerInterface;
1112
use React\Filesystem\FilesystemInterface;
@@ -322,6 +323,15 @@ public function stat($filename)
322323
* @return PromiseInterface
323324
*/
324325
public function ls($path)
326+
{
327+
return ObjectStreamSink::promise($this->lsStream($path));
328+
}
329+
330+
/**
331+
* @param string $path
332+
* @return ObjectStream
333+
*/
334+
public function lsStream($path)
325335
{
326336
$stream = new ObjectStream();
327337

src/Eio/Adapter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use React\Filesystem\ModeTypeDetector;
1111
use React\Filesystem\Node\NodeInterface;
1212
use React\Filesystem\ObjectStream;
13+
use React\Filesystem\ObjectStreamSink;
1314
use React\Filesystem\OpenFileLimiter;
1415
use React\Filesystem\PermissionFlagResolver;
1516
use React\Filesystem\TypeDetectorInterface;
1617
use React\Promise\Deferred;
18+
use React\Promise\PromiseInterface;
1719

1820
class Adapter implements AdapterInterface
1921
{
@@ -191,9 +193,18 @@ public function chown($path, $uid, $gid)
191193
}
192194

193195
/**
194-
* {@inheritDoc}
196+
* @param string $path
197+
* @return PromiseInterface
195198
*/
196199
public function ls($path)
200+
{
201+
return ObjectStreamSink::promise($this->lsStream($path));
202+
}
203+
204+
/**
205+
* {@inheritDoc}
206+
*/
207+
public function lsStream($path)
197208
{
198209
$stream = new ObjectStream();
199210

src/Node/Directory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ public function __construct($path, FilesystemInterface $filesystem, RecursiveInv
6969
*/
7070
public function ls()
7171
{
72-
return ObjectStreamSink::promise($this->lsStreaming());
72+
return $this->adapter->ls($this->path);
7373
}
7474

7575
/**
7676
* {@inheritDoc}
7777
*/
7878
public function lsStreaming()
7979
{
80-
return $this->adapter->ls($this->path);
80+
return $this->adapter->lsStream($this->path);
8181
}
8282

8383
/**

tests/ChildProcess/AdapterTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use React\EventLoop\Factory;
66
use React\Filesystem\ChildProcess\Adapter;
77
use React\Filesystem\Filesystem;
8+
use React\Filesystem\Node\File;
89
use React\Filesystem\Node\NodeInterface;
910
use React\Promise\Deferred;
1011
use React\Promise\FulfilledPromise;
@@ -278,6 +279,52 @@ public function testSymlink()
278279
}
279280

280281
public function testLs()
282+
{
283+
$loop = \React\EventLoop\Factory::create();
284+
$adapter = new Adapter($loop, [
285+
'pool' => [
286+
'class' => 'WyriHaximus\React\ChildProcess\Pool\Pool\Dummy',
287+
],
288+
]);
289+
$invoker = $this->getMock('React\Filesystem\CallInvokerInterface', [
290+
'__construct',
291+
'invokeCall',
292+
'isEmpty',
293+
]);
294+
$adapter->setInvoker($invoker);
295+
$fs = Filesystem::createFromAdapter($adapter);
296+
297+
$deferred = new Deferred();
298+
299+
$invoker
300+
->expects($this->once())
301+
->method('invokeCall')
302+
->with(
303+
'readdir',
304+
[
305+
'path' => 'foo.bar',
306+
'flags' => 2,
307+
]
308+
)->will($this->returnValue($deferred->promise()))
309+
;
310+
311+
$promise = $adapter->ls('foo.bar');
312+
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
313+
314+
$deferred->resolve([
315+
[
316+
'type' => 'file',
317+
'name' => 'bar.foo',
318+
],
319+
]);
320+
321+
$nodes = $this->await($promise, $loop);
322+
$nodes->rewind();
323+
324+
$this->assertEquals(new File('foo.bar/bar.foo', $fs), $nodes->current());
325+
}
326+
327+
public function testLsStream()
281328
{
282329
$loop = $this->getMock('React\EventLoop\LoopInterface');
283330
$adapter = new Adapter($loop, [
@@ -307,7 +354,7 @@ public function testLs()
307354
)->will($this->returnValue($deferred->promise()))
308355
;
309356

310-
$stream = $adapter->ls('foo.bar');
357+
$stream = $adapter->lsStream('foo.bar');
311358
$this->assertInstanceOf('React\Filesystem\ObjectStream', $stream);
312359

313360
$calledOnData = false;

tests/Node/DirectoryTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,41 @@ public function testLs()
4545

4646
$filesystem = $this->mockAdapter($loop);
4747

48-
$lsStream = $this->getMock('React\Filesystem\ObjectStream');
48+
$ls = \React\Promise\resolve();
4949

5050
$filesystem
5151
->expects($this->once())
5252
->method('ls')
5353
->with()
54-
->will($this->returnValue($lsStream))
54+
->will($this->returnValue($ls))
5555
;
5656

5757
$directory = new Directory($path, Filesystem::createFromAdapter($filesystem));
5858

5959
$this->assertInstanceOf('React\Promise\PromiseInterface', $directory->ls());
6060
}
6161

62+
public function testLsStream()
63+
{
64+
$path = '/home/foo/bar';
65+
$loop = $this->getMock('React\EventLoop\LoopInterface');
66+
67+
$filesystem = $this->mockAdapter($loop);
68+
69+
$lsStream = $this->getMock('React\Filesystem\ObjectStream');
70+
71+
$filesystem
72+
->expects($this->once())
73+
->method('lsStream')
74+
->with()
75+
->will($this->returnValue($lsStream))
76+
;
77+
78+
$directory = new Directory($path, Filesystem::createFromAdapter($filesystem));
79+
80+
$this->assertInstanceOf('React\Filesystem\ObjectStream', $directory->lsStreaming());
81+
}
82+
6283
public function testCreate()
6384
{
6485
$path = 'foo.bar';

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function mockAdapter(LoopInterface $loop = null)
3535
'chown',
3636
'stat',
3737
'ls',
38+
'lsStream',
3839
'touch',
3940
'open',
4041
'read',

tests/UnknownNodeType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace React\Tests\Filesystem;
44

55
use React\Filesystem\Node\NodeInterface;
6-
use React\Filesystem\Node\ObjectStream;
6+
use React\Filesystem\ObjectStream;
77

88
class UnknownNodeType implements NodeInterface
99
{

0 commit comments

Comments
 (0)