Skip to content

Commit a6928b1

Browse files
committed
Inject input and output stream into Stdio (DI)
1 parent 80dca99 commit a6928b1

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/Stdio.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace Clue\React\Stdio;
44

5-
use React\Stream\StreamInterface;
65
use React\Stream\CompositeStream;
76
use React\EventLoop\LoopInterface;
8-
use React\Stream\ReadableStream;
9-
use React\Stream\Stream;
7+
use React\Stream\ReadableStreamInterface;
8+
use React\Stream\WritableStreamInterface;
109

1110
class Stdio extends CompositeStream
1211
{
@@ -16,13 +15,23 @@ class Stdio extends CompositeStream
1615
private $readline;
1716
private $needsNewline = false;
1817

19-
public function __construct(LoopInterface $loop, $input = true)
18+
public function __construct(LoopInterface $loop, ReadableStreamInterface $input = null, WritableStreamInterface $output = null, Readline $readline = null)
2019
{
21-
$this->input = new Stdin($loop);
20+
if ($input === null) {
21+
$input = new Stdin($loop);
22+
}
23+
24+
if ($output === null) {
25+
$output = new Stdout(STDOUT);
26+
}
2227

23-
$this->output = new Stdout(STDOUT);
28+
if ($readline === null) {
29+
$readline = new Readline($input, $output);
30+
}
2431

25-
$this->readline = new Readline($this->input, $this->output);
32+
$this->input = $input;
33+
$this->output = $output;
34+
$this->readline = $readline;
2635

2736
$that = $this;
2837

@@ -35,10 +44,6 @@ public function __construct(LoopInterface $loop, $input = true)
3544
$this->readline->on('data', function($line) use ($that) {
3645
$that->emit('line', array($line, $that));
3746
});
38-
39-
if (!$input) {
40-
$this->pause();
41-
}
4247
}
4348

4449
public function pause()

tests/StdioTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use React\EventLoop\Factory;
44
use Clue\React\Stdio\Stdio;
5+
use Clue\React\Stdio\Readline;
56

67
class StdioTest extends TestCase
78
{
@@ -12,8 +13,24 @@ public function setUp()
1213
$this->loop = Factory::create();
1314
}
1415

15-
public function testCtor()
16+
public function testCtorDefaultArgs()
1617
{
1718
$stdio = new Stdio($this->loop);
19+
$stdio->close();
20+
}
21+
22+
public function testCtorArgsWillBeReturnedByGetters()
23+
{
24+
$input = $this->getMock('React\Stream\ReadableStreamInterface');
25+
$output = $this->getMock('React\Stream\WritableStreamInterface');
26+
27+
//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
28+
$readline = new Readline($input, $output);
29+
30+
$stdio = new Stdio($this->loop, $input, $output, $readline);
31+
32+
$this->assertSame($input, $stdio->getInput());
33+
$this->assertSame($output, $stdio->getOutput());
34+
$this->assertSame($readline, $stdio->getReadline());
1835
}
1936
}

0 commit comments

Comments
 (0)