Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
Merge pull request #47 from mikey179/master
Browse files Browse the repository at this point in the history
api rework
  • Loading branch information
mikey179 committed Jun 18, 2014
2 parents 44f53a7 + 4111e4f commit 960d8a1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### BC breaks

* removed namespace prefix `net`, base namespace is now `stubbles\console` only
* deprecated `stubbles\console\Executor::getOutputStream()`, use `stubbles\console\Executor::out()` instead, will be removed with 4.0.0

### Other changes

Expand Down
53 changes: 30 additions & 23 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions src/main/php/ConsoleExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class ConsoleExecutor implements Executor
*
* @type OutputStream
*/
protected $out;
private $out;
/**
* redirect direction
*
* @type string
*/
protected $redirect = '2>&1';
private $redirect = '2>&1';

/**
* sets the output stream to write data outputted by executed command to
Expand All @@ -45,11 +45,22 @@ public function streamOutputTo(OutputStream $out)
*
* @return OutputStream
*/
public function getOutputStream()
public function out()
{
return $this->out;
}

/**
* returns the output stream to write data outputted by executed command to
*
* @return OutputStream
* @deprecated since 3.0.0, use out() instead, will be removed with 4.0.0
*/
public function getOutputStream()
{
return $this->out();
}

/**
* sets the redirect
*
Expand All @@ -76,10 +87,11 @@ public function execute($command)
throw new RuntimeException('Can not execute ' . $command);
}

// must read all output even if we don't need it, otherwise we don't
// receive a correct return code when closing the process file pointer
while (!feof($pd) && false !== ($line = fgets($pd, 4096))) {
$line = chop($line);
if (null !== $this->out) {
$this->out->writeLine($line);
$this->out->writeLine(rtrim($line));
}
}

Expand Down Expand Up @@ -127,7 +139,7 @@ public function executeDirect($command)

$result = [];
while (!feof($pd) && false !== ($line = fgets($pd, 4096))) {
$result[] = chop($line);
$result[] = rtrim($line);
}

$returnCode = pclose($pd);
Expand Down
8 changes: 8 additions & 0 deletions src/main/php/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public function streamOutputTo(OutputStream $out);
*
* @return OutputStream
*/
public function out();

/**
* returns the output stream to write data outputted by executed command to
*
* @return OutputStream
* @deprecated since 3.0.0, use out() instead, will be removed with 4.0.0
*/
public function getOutputStream();

/**
Expand Down
29 changes: 24 additions & 5 deletions src/test/php/ConsoleExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,34 @@ public function redirectToReturnsItself()
$this->assertSame($this->executor, $this->executor->redirectTo('2>&1'));
}

/**
* @test
*/
public function hasNoOutputStreamByDefault()
{
$this->assertNull($this->executor->out());
}

/**
* @test
*/
public function executeWithoutOutputStream()
{
$this->assertNull($this->executor->getOutputStream());
$this->assertSame($this->executor, $this->executor->execute('echo foo'));
}

/**
* @test
*/
public function outReturnsOutputStreamOriginallySet()
{
$mockOutputStream = $this->getMock('stubbles\streams\OutputStream');
$this->assertSame(
$mockOutputStream,
$this->executor->streamOutputTo($mockOutputStream)->out()
);
}

/**
* @test
*/
Expand All @@ -56,9 +75,10 @@ public function executeWithOutputStreamWritesResponseDataToOutputStream()
$mockOutputStream->expects($this->once())
->method('writeLine')
->with($this->equalTo('foo'));
$this->assertSame($this->executor, $this->executor->streamOutputTo($mockOutputStream));
$this->assertSame($mockOutputStream, $this->executor->getOutputStream());
$this->assertSame($this->executor, $this->executor->execute('echo foo'));
$this->assertSame(
$this->executor,
$this->executor->streamOutputTo($mockOutputStream)->execute('echo foo')
);
}

/**
Expand Down Expand Up @@ -133,5 +153,4 @@ public function executeDirectFailsThrowsRuntimeException()
{
$this->executor->executeDirect('php -r "throw new Exception();"');
}

}

0 comments on commit 960d8a1

Please sign in to comment.