Skip to content

Commit

Permalink
Add new command: test launcher. Very specific, useful only with Bitba…
Browse files Browse the repository at this point in the history
…n's framework
  • Loading branch information
falvarez committed Sep 20, 2016
1 parent ff6e499 commit 5d4edb6
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ Shows debug information about:

This command, may be unavailable in any future versions of these tools, as it has been added for debug purposes.

## Run Bitban Lightweight tests

Runs Bitban's framework lightweight tests. It assumes that Bitban's Development VM is up and running, and it has test launcher script `run_light_tests.sh` installed.

`bin/php-cqtools test:run --vmHost[=VMHOST] [<projectPath]`

`projectPath` argument sets path to be processed. Default value is current project base path.

`--vmHost=VMHOST` option sets Development VM hostname

This command has no use for projects not integrating Bitban's framework.

## References

Here are several interesting links with information (and inspiration) about this subject.
Expand Down
110 changes: 110 additions & 0 deletions src/Command/Test/RunCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* Copyright 2016 Bitban Technologies, S.L.
* Todos los derechos reservados.
*/

namespace Bitban\PhpCodeQualityTools\Command\Test;

use Bitban\PhpCodeQualityTools\Command\BaseCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class RunCommand extends BaseCommand
{
const COMMAND_NAME = 'test:run';
const COMMAND_DESCRIPTION = 'Runs lightweight tests of Bitban\'s framework';
const COMMAND_HELP = 'Runs lightweight tests of Bitban\'s framework. Not useful outside of there.';
const OPTION_VM_HOST = 'vmHost';

protected function configure()
{
parent::configure();
$this
->setName(self::COMMAND_NAME)
->setDescription(self::COMMAND_DESCRIPTION)
->setHelp(self::COMMAND_HELP)
->addOption(self::OPTION_VM_HOST, null, InputOption::VALUE_OPTIONAL, 'Host name of development VM', 'ndevel');
}

/**
* @param string $vmHost
* @return bool
*/
private function checkVmConnection($vmHost)
{
if (!$socket = @fsockopen($vmHost, 22)) {
return false;
}
fclose($socket);
return true;
}

/**
* @param Process $process
* @param OutputInterface $output
*/
private function runTests($process, $output)
{
if ($output->isDebug()) {
$process->run(function ($type, $buffer) use ($output) {
if (Process::ERR === $type) {
$output->write('<fg=yellow>' . $buffer . '</fg=yellow>');
} else {
$output->write($buffer);
}
});
} else {
$process->run();
}
}

/**
* @param Process $process
* @param OutputInterface $output
* @return bool
*/
private function outputTestsResult($process, $output)
{
$processExitCode = $process->getExitCode();
if ($processExitCode > 0) {
if (strpos($process->getErrorOutput(), 'bash') !== false) {
$output->writeln('<error>run_light_tests.sh script not found, could not perform any test</error>');
} else {
$output->writeln($process->getOutput());
$output->writeln('<error>Tests FAILED</error>');
}
} else {
$output->writeln('<info>Tests OK</info>');
}

return $processExitCode === 0;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$vmHost = $input->getOption(self::OPTION_VM_HOST);
if (!$this->checkVmConnection($vmHost)) {
$output->writeln("<fg=red>VM host '$vmHost' is down, could not perform any test</fg=red>");
return false;
}

$project = basename($this->projectBasepath);

$output->writeln("<fg=yellow>Running lightweight tests for project '$project', please be patient...</fg=yellow>");

$commandLine = <<<CMD
ssh -t root@$vmHost "~/run_light_tests.sh {$project}"
CMD;

$process = new Process($commandLine);
$process->setTimeout(3600);

$this->runTests($process, $output);
$testsResult = $this->outputTestsResult($process, $output);
return $testsResult;
}
}
7 changes: 5 additions & 2 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
use Bitban\PhpCodeQualityTools\Command\GitHooks\PostCheckoutCommand;
use Bitban\PhpCodeQualityTools\Command\GitHooks\PostMergeCommand;
use Bitban\PhpCodeQualityTools\Command\GitHooks\UninstallCommand;
use Bitban\PhpCodeQualityTools\Command\Test\RunCommand;
use Symfony\Component\Console\Application as BaseApplication;

class Application extends BaseApplication
{
const APP_NAME = 'Bitban Technologies PHP Code Quality Tools';
const APP_VERSION = '0.9.10';
const APP_VERSION = '0.9.11';

public function __construct()
{
Expand All @@ -38,7 +39,9 @@ public function __construct()
new ValidateCommand(),
new CustomFixCommand(),

new ShowValuesCommand()
new ShowValuesCommand(),

new RunCommand()
]);
}
}

0 comments on commit 5d4edb6

Please sign in to comment.