Skip to content

Commit

Permalink
Add Command test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinleblanc committed Sep 14, 2016
1 parent d8ba266 commit d5e986d
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 5 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
"Pantheon\\Terminus\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Pantheon\\Terminus\\Tests\\": "tests/"
}
},
"require-dev": {
"behat/behat": "^3.1",
"phpunit/phpcov": "^2.0",
Expand Down
230 changes: 230 additions & 0 deletions tests/CommandTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php
namespace Pantheon\Terminus\Tests;

use League\Container\Container;
use Pantheon\Terminus\Config;
use Pantheon\Terminus\Runner;
use Pantheon\Terminus\Terminus;
use Robo\Robo;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;

abstract class CommandTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @var Terminus
*/
protected $app;
/**
* @var string
*/
protected $status_code;
/**
* @var Config
*/
protected $config;
/**
* @var Container
*/
protected $container;
/**
* @var OutputInterface
*/
protected $output;
/**
* @var Runner
*/
protected $runner;
/**
* @var ArrayInput
*/
protected $input;

/**
* @return Terminus
*/
public function getApp()
{
return $this->app;
}

/**
* @param Terminus $app
* @return $this
*/
public function setApp($app)
{
$this->app = $app;
return $this;
}

/**
* @return Config
*/
public function getConfig()
{
return $this->config;
}

/**
* @param Config $config
* @return CommandTestCase
*/
public function setConfig($config)
{
$this->config = $config;
return $this;
}

/**
* @return Runner
*/
public function getRunner()
{
return $this->runner;
}

/**
* @param Runner $runner
* @return CommandTestCase
*/
public function setRunner($runner)
{
$this->runner = $runner;
return $this;
}

/**
* @return OutputInterface
*/
public function getOutput()
{
return $this->output;
}

/**
* Convert the output of a command to an easily digested string.
* @return string|OutputInterface
*/
public function fetchTrimmedOutput()
{
if (get_class($this->output) == BufferedOutput::class) {
return trim($this->getOutput()->fetch());
}
return $this->getOutput();
}

/**
* @param OutputInterface $output
* @return CommandTestCase
*/
public function setOutput(OutputInterface $output)
{
$this->output = $output;
return $this;
}

/**
* @return mixed
*/
public function getContainer()
{
return $this->container;
}

/**
* @param mixed $container
* @return CommandTestCase
*/
public function setContainer($container)
{
$this->container = $container;
return $this;
}

/**
* @return int
*/
public function getStatusCode()
{
return $this->status_code;
}

/**
* @param mixed $status_code
* @return CommandTestCase
*/
public function setStatusCode($status_code)
{
$this->status_code = $status_code;
return $this;
}

/**
* @return mixed
*/
public function getInput()
{
return $this->input;
}

/**
* @param mixed $input
* @return CommandTestCase
*/
public function setInput($input)
{
$this->input = new ArrayInput($input);
return $this;
}

/**
* Run the command and capture the exit code.
*
* @return $this
*/
public function runCommand()
{
$this->status_code = $this->runner->run($this->input, $this->output);
return $this;
}

/**
* @inheritdoc
*/
protected function setUp()
{
if (!$this->config) {
$this->config = new Config();
}

if (!$this->app) {
$this->app = new Terminus('Terminus', $this->config->get('version'), $this->config);
}

if (!$this->container) {
$this->container = new Container();
}

if (!$this->output) {
$this->output = new BufferedOutput();
}

if (!$this->input) {
$this->input = new ArrayInput([]);
}
// Configuring the dependency-injection container
Robo::configureContainer(
$this->container,
$this->config,
$this->input,
$this->output,
$this->app
);

if (!$this->runner) {
$this->runner = new Runner($this->container);
}
}
}
15 changes: 10 additions & 5 deletions tests/new_unit_tests/Commands/ArtCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Pantheon\Terminus\UnitTests\Commands;

use Pantheon\Terminus\Tests\CommandTestCase;
use Symfony\Component\Console\Output\BufferedOutput;

class ArtCommandTest extends CommandTestCase
{
Expand All @@ -13,14 +12,20 @@ public function setUp()
$this->setInput(['command' => 'art', 'name' => 'hello']);
}

public function testArtPrintsArt()
/**
* @test
*/
public function art_command_prints_contents_of_files_in_assets_directory()
{
$this->assertEquals('Hello World!', $this->runCommand()->getOutput());
$this->assertEquals('Hello World!', $this->runCommand()->fetchTrimmedOutput());
}

public function testArtRejectsNonExistantFiles()
/**
* @test
*/
public function art_command_rejects_files_not_in_assets_directory()
{
$this->setInput(['command' => 'art', 'name' => 'foo']);
$this->assertEquals('Not a valid work of art!', $this->runCommand()->getOutput());
$this->assertEquals('Not a valid work of art!', $this->runCommand()->fetchTrimmedOutput());
}
}

0 comments on commit d5e986d

Please sign in to comment.