diff --git a/composer.json b/composer.json index 735951e..dadb43a 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,9 @@ "react/event-loop": "~0.4.0|~0.3.0", "react/child-process": "~0.4.0|~0.3.0" }, + "require-dev": { + "clue/block-react": "^1.1" + }, "autoload": { "psr-4": { "Clue\\React\\Zenity\\": "src/" } } diff --git a/src/Zen/BaseZen.php b/src/Zen/BaseZen.php index 55b6bb6..104a002 100644 --- a/src/Zen/BaseZen.php +++ b/src/Zen/BaseZen.php @@ -51,10 +51,16 @@ public function promise() public function close() { + // resolve with whatever is currently buffered $this->deferred->resolve(); if ($this->process !== null && $this->process->isRunning()) { $this->process->terminate(SIGKILL); + + // explicitly close all streams immediately + $this->process->stdin->close(); + $this->process->stdout->close(); + $this->process->stderr->close(); } return $this; diff --git a/tests/FunctionalLauncherTest.php b/tests/FunctionalLauncherTest.php index 27fc22a..614de24 100644 --- a/tests/FunctionalLauncherTest.php +++ b/tests/FunctionalLauncherTest.php @@ -1,10 +1,11 @@ launcher->launch($this->dialog); - $this->loop->run(); + $result = Block\await($promise, $this->loop, 1.0); - $promise->then($this->expectCallableOnceWith('--hello --world')); + $this->assertEquals('--hello --world', $result); } public function testDoesPassStdin() @@ -44,9 +45,9 @@ public function testDoesPassStdin() $zen->close(); }); - $this->loop->run(); + $result = Block\await($zen->promise(), $this->loop, 1.0); - $zen->promise()->then($this->expectCallableOnceWith('okay')); + $this->assertEquals('okay', $result); } public function testWaitForOutput() diff --git a/tests/Zen/BaseZenTest.php b/tests/Zen/BaseZenTest.php index 7a48406..596d281 100644 --- a/tests/Zen/BaseZenTest.php +++ b/tests/Zen/BaseZenTest.php @@ -22,6 +22,7 @@ public function setUp() $this->process->stdin = $this->instream; $this->process->stdout = $this->getMock('React\Stream\ReadableStreamInterface'); + $this->process->stderr = $this->getMock('React\Stream\ReadableStreamInterface'); } public function testClosingZenTerminatesProcess() diff --git a/tests/Zen/FunctionalBaseZenTest.php b/tests/Zen/FunctionalBaseZenTest.php index fb13a49..06236b7 100644 --- a/tests/Zen/FunctionalBaseZenTest.php +++ b/tests/Zen/FunctionalBaseZenTest.php @@ -1,8 +1,10 @@ go($process); - $this->loop->run(); + $result = Block\await($zen->promise(), $this->loop, 1.0); - $zen->promise()->then($this->expectCallableOnceWith('okay')); + $this->assertEquals('okay', $result); } public function testZenResolvesWithTrueWhenProcessHasNoOutput() @@ -31,9 +33,9 @@ public function testZenResolvesWithTrueWhenProcessHasNoOutput() $zen = new BaseZen(); $zen->go($process); - $this->loop->run(); + $result = Block\await($zen->promise(), $this->loop, 1.0); - $zen->promise()->then($this->expectCallableOnceWith(true)); + $this->assertTrue($result); } public function testZenRejectsWhenProcessReturnsError() @@ -44,9 +46,7 @@ public function testZenRejectsWhenProcessReturnsError() $zen = new BaseZen(); $zen->go($process); - $this->loop->run(); - - $zen->promise()->then(null, $this->expectCallableOnceWith(1)); + Block\await($zen->promise()->then(null, $this->expectCallableOnceWith(1)), $this->loop, 1.0); } public function testClosingZenResolvesWithOutputSoFar() @@ -61,9 +61,9 @@ public function testClosingZenResolvesWithOutputSoFar() $zen->close(); }); - $this->loop->run(); + $result = Block\await($zen->promise(), $this->loop, 1.0); - $zen->promise()->then($this->expectCallableOnceWith('okay')); + $this->assertEquals('okay', $result); } public function testTerminatingProcessReturnsError() @@ -76,10 +76,9 @@ public function testTerminatingProcessReturnsError() $this->loop->addTimer(0.1, function() use ($process) { $process->terminate(SIGKILL); + $process->stdin->end(); }); - $this->loop->run(); - - $zen->promise()->then(null, $this->expectCallableOnce()); + Block\await($zen->promise()->then(null, $this->expectCallableOnce()), $this->loop, 1.0); } }