Skip to content

Commit

Permalink
Implement reboot command.
Browse files Browse the repository at this point in the history
  • Loading branch information
boite committed Aug 17, 2016
1 parent d5f48e3 commit 5b0b9b1
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bin/droid-os
Expand Up @@ -21,7 +21,7 @@ if (!file_exists($loader)) {

require $loader;

$application = new Application('Droid Os', '1.0.0');
$application = new Application('Droid OS', '1.0.0');
$application->setCatchExceptions(true);
$registry = new DroidPlugin($application);
foreach ($registry->getCommands() as $command) {
Expand Down
40 changes: 39 additions & 1 deletion src/Command/OsRebootCommand.php
Expand Up @@ -5,15 +5,53 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessBuilder;

class OsRebootCommand extends Command
{
private $processBuilder;

public function __construct(ProcessBuilder $builder, $name = null)
{
$this->processBuilder = $builder;
return parent::__construct($name);
}

public function configure()
{
$this->setName('os:reboot');
$this
->setName('os:reboot')
->setDescription('Initiate a reboot.')
;
}

public function execute(InputInterface $input, OutputInterface $output)
{
$p = $this->getProcess(array('shutdown', '-r', 'now', '&&', 'logout'));

$output->writeln('<info>I attempt to initiate a reboot.</info>');
if ($p->run()) {
$output->writeln(
sprintf(
'<error>I have failed to initiate a reboot. Code %d: %s</error>',
$p->getExitCode(),
$p->getErrorOutput()
)
);
return $p->getExitCode();
}
$output->writeln('<info>I have successfully initiated a reboot.</info>');
}

/**
* @return \Symfony\Component\Process\Process
*/
private function getProcess($arguments)
{
return $this
->processBuilder
->setArguments($arguments)
->getProcess()
;
}
}
4 changes: 3 additions & 1 deletion src/DroidPlugin.php
Expand Up @@ -2,6 +2,8 @@

namespace Droid\Plugin\Os;

use Symfony\Component\Process\ProcessBuilder;

use Droid\Plugin\Os\Command\OsRebootCommand;

class DroidPlugin
Expand All @@ -14,7 +16,7 @@ public function __construct($droid)
public function getCommands()
{
return array(
new OsRebootCommand,
new OsRebootCommand(new ProcessBuilder),
);
}
}
90 changes: 87 additions & 3 deletions test/Droid/Command/OsRebootCommandTest.php
Expand Up @@ -14,6 +14,7 @@ class OsRebootCommandTest extends PHPUnit_Framework_TestCase
protected $app;
protected $process;
protected $processBuilder;
protected $tester;

protected function setUp()
{
Expand All @@ -29,14 +30,97 @@ protected function setUp()
->getMock()
;

$command = new OsRebootCommand; #($this->processBuilder);
$command = new OsRebootCommand($this->processBuilder);

$this->app = new Application;
$this->app->add($command);

$this->tester = new CommandTester($command);
}

public function testRebootWillFail()
{
$this
->processBuilder
->expects($this->once())
->method('setArguments')
->with(array('shutdown', '-r', 'now', '&&', 'logout'))
->willReturnSelf()
;
$this
->processBuilder
->expects($this->once())
->method('getProcess')
->willReturn($this->process)
;
$this
->process
->expects($this->once())
->method('run')
->willReturn(1)
;
$this
->process
->expects($this->atLeastOnce())
->method('getExitCode')
->willReturn(1)
;
$this
->process
->expects($this->once())
->method('getErrorOutput')
->willReturn('something went awry')
;

$this->tester->execute(
array('command' => $this->app->find('os:reboot'))
);

$this->assertRegExp(
'/I have failed to initiate a reboot\. Code 1: something went awry/',
$this->tester->getDisplay()
);
}

public function testReboot()
public function testRebootWillSucceed()
{
$this->markTestIncomplete('This test has not been written.');
$this
->processBuilder
->expects($this->once())
->method('setArguments')
->with(array('shutdown', '-r', 'now', '&&', 'logout'))
->willReturnSelf()
;
$this
->processBuilder
->expects($this->once())
->method('getProcess')
->willReturn($this->process)
;
$this
->process
->expects($this->once())
->method('run')
->willReturn(0)
;
$this
->process
->expects($this->never())
->method('getExitCode')
;
$this
->process
->expects($this->never())
->method('getErrorOutput')
;

$this->tester->execute(
array('command' => $this->app->find('os:reboot'))
);

$this->assertRegExp(
'/I have successfully initiated a reboot/',
$this->tester->getDisplay()
);
}
}

0 comments on commit 5b0b9b1

Please sign in to comment.