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 eebedfb commit c701a8f
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 1 deletion.
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
210 changes: 210 additions & 0 deletions tests/CommandTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?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 ConsoleOutput
*/
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;
}

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

/**
* @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 mixed
*/
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;
}

public function runCommand()
{
$this->status_code = $this->runner->run($this->input, $this->output);
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;
}

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);
}
}
}
1 change: 0 additions & 1 deletion 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 Down

0 comments on commit c701a8f

Please sign in to comment.