Skip to content

Commit

Permalink
Add an option to write in a file
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa committed Oct 18, 2020
1 parent 6ae08c2 commit b00ef40
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 14 deletions.
4 changes: 2 additions & 2 deletions bin/churn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require_once(__DIR__ . '/bootstrap.php');

use Churn\Commands\ChurnCommand;
use Churn\Command\RunCommand;
use DI\ContainerBuilder;
use PackageVersions\Versions;
use Symfony\Component\Console\Application;
Expand All @@ -12,5 +12,5 @@ $container = ContainerBuilder::buildDevContainer();

$version = substr(Versions::getVersion('bmitch/churn-php'), 0, -33);
$application = new Application('churn-php', $version);
$application->add($container->get(ChurnCommand::class));
$application->add($container->get(RunCommand::class));
$application->run();
52 changes: 44 additions & 8 deletions src/Commands/ChurnCommand.php → src/Command/RunCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace Churn\Commands;
namespace Churn\Command;

use Churn\Configuration\Config;
use Churn\Factories\ResultsRendererFactory;
Expand All @@ -11,27 +11,31 @@
use Churn\Process\Observer\OnSuccessProgress;
use Churn\Process\ProcessFactory;
use Churn\Process\ProcessHandlerFactory;
use Churn\Results\ResultCollection;
use function count;
use function file_get_contents;
use function fopen;
use InvalidArgumentException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Yaml\Yaml;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ChurnCommand extends Command
class RunCommand extends Command
{
private const LOGO ="
public const LOGO ="
___ _ _ __ __ ____ _ _ ____ _ _ ____
/ __)( )_( )( )( )( _ \( \( )___( _ \( )_( )( _ \
( (__ ) _ ( )(__)( ) / ) ((___))___/ ) _ ( )___/
\___)(_) (_)(______)(_)\_)(_)\_) (__) (_) (_)(__)";
\___)(_) (_)(______)(_)\_)(_)\_) (__) (_) (_)(__)
";

/**
* The results logic.
Expand Down Expand Up @@ -78,6 +82,7 @@ protected function configure(): void
->addArgument('paths', InputArgument::IS_ARRAY, 'Path to source to check.')
->addOption('configuration', 'c', InputOption::VALUE_OPTIONAL, 'Path to the configuration file', 'churn.yml') // @codingStandardsIgnoreLine
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format to use', 'text')
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'The path where to write the result')
->addOption('progress', 'p', InputOption::VALUE_NONE, 'Show progress bar')
->setDescription('Check files')
->setHelp('Checks the churn on the provided path argument(s).');
Expand All @@ -91,7 +96,7 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln(self::LOGO);
$this->displayLogo($input, $output);
$content = (string) @file_get_contents($input->getOption('configuration'));
$config = Config::create(Yaml::parse($content) ?? []);
$filesCollection = (new FileManager($config->getFileExtensions(), $config->getFilesToIgnore()))
Expand All @@ -106,8 +111,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$config->getMinScoreToShow(),
$config->getFilesToShow()
);
$renderer = $this->renderFactory->getRenderer($input->getOption('format'));
$renderer->render($output, $resultCollection);
$this->writeResult($input, $output, $resultCollection);
return 0;
}

Expand Down Expand Up @@ -144,12 +148,44 @@ private function getDirectoriesToScan(InputInterface $input, array $dirsConfigur
private function getOnSuccessObserver(InputInterface $input, OutputInterface $output, int $totalFiles): OnSuccess
{
if ((bool)$input->getOption('progress')) {
$output->writeln("\n");
$progressBar = new ProgressBar($output, $totalFiles);
$progressBar->start();
return new OnSuccessProgress($progressBar);
}

return new OnSuccessNull();
}

/**
* @param InputInterface $input Input.
* @param OutputInterface $output Output.
* @return void
*/
private function displayLogo(InputInterface $input, OutputInterface $output): void
{
if ($input->getOption('format') !== 'text' && empty($input->getOption('output'))) {
return;
}

$output->writeln(self::LOGO);
}

/**
* @param InputInterface $input Input.
* @param OutputInterface $output Output.
* @param ResultCollection $resultCollection The result to write.
* @return void
*/
private function writeResult(InputInterface $input, OutputInterface $output, ResultCollection $resultCollection): void
{
if ((bool)$input->getOption('progress')) {
$output->writeln("\n");
}
if (!empty($input->getOption('output'))) {
$output = new StreamOutput(fopen($input->getOption('output'), 'w+'), OutputInterface::VERBOSITY_NORMAL, false);
}

$renderer = $this->renderFactory->getRenderer($input->getOption('format'));
$renderer->render($output, $resultCollection);
}
}
4 changes: 0 additions & 4 deletions src/Renderers/Results/ConsoleResultsRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ class ConsoleResultsRenderer implements ResultsRendererInterface
*/
public function render(OutputInterface $output, ResultCollection $results): void
{
$output->writeln("\n");

$table = new Table($output);
$table->setHeaders(['File', 'Times Changed', 'Complexity', 'Score']);
$table->addRows($results->toArray());
$table->render();

$output->write("\n");
}
}
39 changes: 39 additions & 0 deletions tests/Integration/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types = 1);

namespace Churn\Tests\Integration\Command;

use Churn\Command\RunCommand;
use Churn\Tests\BaseTestCase;
use DI\ContainerBuilder;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class RunCommandTest extends BaseTestCase
{
/** @var CommandTester */
private $commandTester;

protected function setUp()
{
$container = ContainerBuilder::buildDevContainer();
$application = new Application('churn-php', 'test');
$application->add($container->get(RunCommand::class));
$command = $application->find('run');
$this->commandTester = new CommandTester($command);
}

protected function tearDown()
{
$this->commandTester = null;
}

/** @test */
public function it_displays_the_logo_at_the_beginning_by_default()
{
$exitCode = $this->commandTester->execute(['paths' => [realpath(__DIR__ . '/../..')]]);
$display = $this->commandTester->getDisplay();

$this->assertEquals(0, $exitCode);
$this->assertEquals(RunCommand::LOGO, substr($display, 0, strlen(RunCommand::LOGO)));
}
}

0 comments on commit b00ef40

Please sign in to comment.