Skip to content

Commit

Permalink
Add builder methods for tty and idleTimeout options
Browse files Browse the repository at this point in the history
  • Loading branch information
TitasGailius committed Jun 24, 2020
1 parent 6928dec commit 3a5a455
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
72 changes: 71 additions & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ class Builder
*/
protected $with = [];

/**
* TTY mode.
*
* @var boolean|null
*/
protected $tty;

/**
* Max time since last output.
*
* @var mixed
*/
protected $idleTimeout;

/**
* Builder extensions.
*
Expand Down Expand Up @@ -261,6 +275,52 @@ public function with($key, $value = null)
return $this;
}

/**
* Enable or disable the TTY mode.
*
* @param bool $tty
* @return $this
*/
public function tty(bool $tty)
{
$this->tty = $tty;

return $this;
}

/**
* Enable TTY mode.
*
* @return $this
*/
public function enableTty()
{
return $this->tty(true);
}

/**
* Disable TTY mode.
*
* @return $this
*/
public function disableTty()
{
return $this->tty(false);
}

/**
* Set max time since last output.
*
* @param mixed $timeout
* @return $this
*/
public function idleTimeout($timeout)
{
$this->idleTimeout = $timeout;

return $this;
}

/**
* Execute a given command.
*
Expand Down Expand Up @@ -379,9 +439,19 @@ public function process()
$this->getSeconds($this->timeout)
];

return is_string($command)
$process = is_string($command)
? Process::fromShellCommandline(...$parameters)
: new Process(...$parameters);

if (! is_null($this->tty)) {
$process->setTty($this->tty);
}

if (! is_null($this->idleTimeout)) {
$process->setIdleTimeout($this->getSeconds($this->idleTimeout));
}

return $process;
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,52 @@ public function testOutputValidation($output)
(new Builder)->output($output)->run('echo Hello');
}

/**
* Test Terminal TTY mode.
*
* @return void
*/
public function testTtyMode()
{
$process = (new Builder)->tty(false)->process();
$this->assertFalse($process->isTty());

$process = (new Builder)->tty(true)->process();
$this->assertTrue($process->isTty());

$process = (new Builder)->disableTty()->process();
$this->assertFalse($process->isTty());

$process = (new Builder)->enableTty()->process();
$this->assertTrue($process->isTty());
}

/**
* Test builder idle timeout.
*
* @dataProvider twentySeconds
*/
public function testIdleTimeout($twentySeconds)
{
$process = (new Builder)->idleTimeout($twentySeconds)->process();
$this->assertEquals(20, $process->getIdleTimeout());
}

/**
* Get invalid outputs.
*
* @return array
*/
public function twentySeconds(): array
{
return [
[20],
[20.0],
[new DateInterval('PT20S')],
[(new DateTime)->add(new DateInterval('PT20S'))],
];
}

/**
* Create a new builder instance with a mocked process instance.
*
Expand Down

0 comments on commit 3a5a455

Please sign in to comment.