Skip to content

Commit

Permalink
Merge pull request #1191 from dustinleblanc/artify
Browse files Browse the repository at this point in the history
Provides art command
  • Loading branch information
TeslaDethray committed Sep 14, 2016
2 parents 3584938 + 3e47e37 commit da4f14b
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 16 deletions.
1 change: 1 addition & 0 deletions assets/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SGVsbG8gV29ybGQh
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
},
"autoload-dev": {
"psr-4": {
"Pantheon\\Terminus\\UnitTests\\": "tests/new_unit_tests/"
"Pantheon\\Terminus\\UnitTests\\": "tests/new_unit_tests/",
"Pantheon\\Terminus\\Tests\\": "tests/"
}
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion config/constants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TERMINUS_CONFIG_DIR: '~/terminus'
TERMINUS_LOG_DIR: '/tmp'
TERMINUS_PLUGINS_DIR: '[[ TERMINUS_CONFIG_DIR ]]/plugins'
TERMINUS_TOKENS_DIR: '[[ TERMINUS_CACHE_DIR ]]/tokens'
TERMINUS_ASSETS_DIR: '[[ TERMINUS_ROOT ]]/php/assets'
TERMINUS_ASSETS_DIR: '[[ TERMINUS_ROOT ]]/assets'
TERMINUS_TEMPLATES_DIR: '[[ TERMINUS_ROOT ]]/templates'

# Helpers
Expand Down
40 changes: 26 additions & 14 deletions src/Commands/ArtCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class ArtCommand extends TerminusCommand
{

/**
* Displays Pantheon ASCII artwork
*
Expand All @@ -18,20 +17,33 @@ class ArtCommand extends TerminusCommand
* Displays the rocket artwork
*/
public function art($name) {
// Symfony Style
$this->io()->title("Symfony Style Example.");
$artwork_content = $this->retrieveArt($name);
$this->io()->text("<comment>{$artwork_content}</comment>");
}

// Symfony OutputInterface
$this->output()->writeln("Wow!");
$this->output()->writeln("The verbosity level is " . $this->output->getVerbosity() . "\n");
$this->output()->writeln("Print this message only in verbose mode", OutputInterface::VERBOSITY_VERBOSE);
$this->output()->writeln("Print this message only in very verbose mode", OutputInterface::VERBOSITY_VERY_VERBOSE);
$this->output()->writeln("Print this message only in debug mode", OutputInterface::VERBOSITY_DEBUG);
/**
* @param $name
* @return string
*/
protected function retrieveArt($name)
{
$file_path = $this->config->get('assets_dir') . "/{$name}.txt";
if ($this->validateAsset($file_path)) {
$output = base64_decode(file_get_contents($file_path));
} else {
$output = "Not a valid work of art!";
}
return $output;
}

// Robo logger
$this->log()->warning('This is a warning log message. (Warnings always printed)');
$this->log()->notice('This is a notice log message. (Also always printed)');
$this->log()->info('This is an informational log message. (Verbose only)');
$this->log()->debug('This is a debug log message. (Debug only)');
/**
* Check to see if an asset exists.
*
* @param $file_path
* @return bool
*/
private function validateAsset($file_path)
{
return file_exists($file_path);
}
}
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);
}
}
}
31 changes: 31 additions & 0 deletions tests/new_unit_tests/Commands/ArtCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Pantheon\Terminus\UnitTests\Commands;

use Pantheon\Terminus\Tests\CommandTestCase;

class ArtCommandTest extends CommandTestCase
{

public function setUp()
{
parent::setUp();
$this->setInput(['command' => 'art', 'name' => 'hello']);
}

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

/**
* @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()->fetchTrimmedOutput());
}
}

0 comments on commit da4f14b

Please sign in to comment.