Skip to content

Commit

Permalink
Merge pull request #169 from bmitch/bmitchell-166
Browse files Browse the repository at this point in the history
Fixes #166 - Results Factory to just return the Renderer
  • Loading branch information
bmitch authored Oct 21, 2017
2 parents 89e7890 + 04863fd commit e6d1afa
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 119 deletions.
12 changes: 7 additions & 5 deletions src/Commands/ChurnCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ class ChurnCommand extends Command
* The renderer factory.
* @var ResultsRendererFactory
*/
private $renderer;
private $renderFactory;

/**
* ChurnCommand constructor.
* @param ResultsLogic $resultsLogic The results logic.
* @param ProcessManager $processManager The process manager.
* @param ResultsRendererFactory $renderer The Results Renderer.
* @param ResultsRendererFactory $renderFactory The Results Renderer Factory.
*/
public function __construct(
ResultsLogic $resultsLogic,
ProcessManager $processManager,
ResultsRendererFactory $renderer
ResultsRendererFactory $renderFactory
) {
parent::__construct();
$this->resultsLogic = $resultsLogic;
$this->processManager = $processManager;
$this->renderer = $renderer;
$this->renderFactory = $renderFactory;
}

/**
Expand Down Expand Up @@ -91,7 +91,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$config->getMinScoreToShow(),
$config->getFilesToShow()
);
$this->renderer->renderResults($input->getOption('format'), $output, $resultCollection);

$renderer = $this->renderFactory->getRenderer($input->getOption('format'));
$renderer->render($output, $resultCollection);
}

/**
Expand Down
17 changes: 6 additions & 11 deletions src/Factories/ResultsRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use Churn\Renderers\Results\ConsoleResultsRenderer;
use Churn\Renderers\Results\JsonResultsRenderer;
use Churn\Results\ResultCollection;
use Churn\Renderers\Results\ResultsRendererInterface;
use InvalidArgumentException;
use Symfony\Component\Console\Output\OutputInterface;

class ResultsRendererFactory
{
Expand All @@ -15,22 +14,18 @@ class ResultsRendererFactory

/**
* Render the results
* @param string $format Format to render.
* @param OutputInterface $output Output Interface.
* @param ResultCollection $results Result Collection.
* @param string $format Format to render.
* @throws InvalidArgumentException If output format invalid.
* @return void
* @return ResultsRendererInterface
*/
public function renderResults(string $format, OutputInterface $output, ResultCollection $results)
public function getRenderer(string $format): ResultsRendererInterface
{
if ($format === self::FORMAT_JSON) {
(new JsonResultsRenderer())->render($output, $results);
return;
return new JsonResultsRenderer;
}

if ($format === self::FORMAT_TEXT) {
(new ConsoleResultsRenderer())->render($output, $results);
return;
return new ConsoleResultsRenderer;
}

throw new InvalidArgumentException('Invalid output format provided');
Expand Down
24 changes: 16 additions & 8 deletions src/Renderers/Results/ConsoleResultsRenderer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php declare(strict_types = 1);


namespace Churn\Renderers\Results;

use Churn\Results\ResultCollection;
Expand All @@ -17,17 +16,26 @@ class ConsoleResultsRenderer implements ResultsRendererInterface
*/
public function render(OutputInterface $output, ResultCollection $results)
{
echo "\n
___ _ _ __ __ ____ _ _ ____ _ _ ____
/ __)( )_( )( )( )( _ \( \( )___( _ \( )_( )( _ \
( (__ ) _ ( )(__)( ) / ) ((___))___/ ) _ ( )___/
\___)(_) (_)(______)(_)\_)(_)\_) (__) (_) (_)(__) https://github.com/bmitch/churn-php\n\n";
$output->write($this->getHeader());

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

$table->render();
echo "\n";

$output->write("\n");
}

/**
* Get the header.
* @return string
*/
private function getHeader(): string
{
return "\n
___ _ _ __ __ ____ _ _ ____ _ _ ____
/ __)( )_( )( )( )( _ \( \( )___( _ \( )_( )( _ \
( (__ ) _ ( )(__)( ) / ) ((___))___/ ) _ ( )___/
\___)(_) (_)(______)(_)\_)(_)\_) (__) (_) (_)(__) https://github.com/bmitch/churn-php\n\n";
}
}
2 changes: 1 addition & 1 deletion src/Results/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getScore(int $maxCommits, int $maxComplexity): float
* so in order to end up with a high score, we invert the value by
* subtracting it from 1.
*/
return 1 - $distanceFromTopRightCorner;
return round(1 - $distanceFromTopRightCorner, 3);
// @codingStandardsIgnoreEnd
}

Expand Down
70 changes: 19 additions & 51 deletions tests/Unit/Factories/ResultsRendererFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,44 @@
use Churn\Factories\ResultsRendererFactory;
use Churn\Renderers\Results\ConsoleResultsRenderer;
use Churn\Renderers\Results\JsonResultsRenderer;
use Churn\Results\ResultCollection;
use Churn\Tests\BaseTestCase;
use Symfony\Component\Console\Output\OutputInterface;
use InvalidArgumentException;

class ResultsRendererFactoryTest extends BaseTestCase
{
/**
* @var ResultsRendererFactory
*/
private $resultsRendererFactory;

public function setUp()
{
$this->resultsRendererFactory = new ResultsRendererFactory();
}
private $factory;

/** @test */
public function it_can_be_created()
{
$this->assertInstanceOf(ResultsRendererFactory::class, $this->resultsRendererFactory);
$this->assertInstanceOf(ResultsRendererFactory::class, $this->factory);
}

/** @test */
public function it_throws_exception_if_format_is_invalid()
/** @test **/
public function it_returns_the_json_renderer_when_provided_json_format()
{
$this->expectException(\InvalidArgumentException::class);

$outputMock = $this->createMock(OutputInterface::class);
$resultsMock = $this->createMock(ResultCollection::class);
$this->assertInstanceOf(JsonResultsRenderer::class, $this->factory->getRenderer('json'));
}

$this->resultsRendererFactory->renderResults('invalid', $outputMock, $resultsMock);
/** @test **/
public function it_returns_the_console_renderer_when_provided_text_format()
{
$factory = new ResultsRendererFactory();
$this->assertInstanceOf(ConsoleResultsRenderer::class, $this->factory->getRenderer('text'));
}

/** @test */
public function it_can_render_json_format()
public function it_throws_exception_if_format_is_invalid()
{
$results = [
[
0 => 'file_1',
1 => 'commits_1',
2 => 'complexity_1',
3 => 'score_1',
],
[
0 => 'file_2',
1 => 'commits_2',
2 => 'complexity_2',
3 => 'score_2',
],
];
$resultsMock = $this->createMock(ResultCollection::class);
$resultsMock->method('toArray')->willReturn($results);

$expectedOutputData = json_encode([
[
'file' => 'file_1',
'commits' => 'commits_1',
'complexity' => 'complexity_1',
'score' => 'score_1',
],
[
'file' => 'file_2',
'commits' => 'commits_2',
'complexity' => 'complexity_2',
'score' => 'score_2',
]
]);
$outputMock = $this->createMock(OutputInterface::class);
$outputMock->expects($this->atLeastOnce())->method('write')->with($expectedOutputData);
$this->expectException(InvalidArgumentException::class);
$this->assertInstanceOf(ConsoleResultsRenderer::class, $this->factory->getRenderer('foobar'));
}

$this->resultsRendererFactory->renderResults(ResultsRendererFactory::FORMAT_JSON, $outputMock, $resultsMock);
public function setUp()
{
$this->factory = new ResultsRendererFactory();
}
}
59 changes: 17 additions & 42 deletions tests/Unit/Renderers/Results/JsonResultsRendererTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Churn\Results\Result;
use Mockery as m;
use Churn\Tests\BaseTestCase;
use Churn\Renderers\Results\JsonResultsRenderer;
Expand All @@ -9,53 +10,27 @@
class JsonResultsRendererTest extends BaseTestCase
{
/** @test **/
public function testRender()
public function it_can_be_instantiated()
{
$inputData = [
[
0 => '0_0_key_test',
1 => '0_1_key_test',
2 => '0_2_key_test',
3 => '0_3_key_test',
],
[
0 => '1_0_key_test',
1 => '1_1_key_test',
2 => '1_2_key_test',
3 => '1_3_key_test',
],
];
$this->assertInstanceOf(JsonResultsRenderer::class, new JsonResultsRenderer);
}

$outputData = json_encode([
[
'file' => '0_0_key_test',
'commits' => '0_1_key_test',
'complexity' => '0_2_key_test',
'score' => '0_3_key_test',
],
[
'file' => '1_0_key_test',
'commits' => '1_1_key_test',
'complexity' => '1_2_key_test',
'score' => '1_3_key_test',
],
/** @test **/
public function it_can_render_the_results_as_json()
{
$resultCollection = new ResultCollection([
new Result(['file' => 'filename1.php', 'commits' => 5, 'complexity' => 7]),
new Result(['file' => 'filename2.php', 'commits' => 3, 'complexity' => 4]),
new Result(['file' => 'filename3.php', 'commits' => 1, 'complexity' => 5]),
new Result(['file' => 'filename4.php', 'commits' => 1, 'complexity' => 1]),
new Result(['file' => 'filename5.php', 'commits' => 8, 'complexity' => 1]),
]);

$output = m::mock(OutputInterface::class);
$output->shouldReceive('write')->atLeast()->once()->with($outputData);

$result = m::mock(ResultCollection::class);
$result->shouldReceive('toArray')->andReturn($inputData);
$output->shouldReceive('write')->atLeast()->once()->with(
'[{"file":"filename1.php","commits":5,"complexity":7,"score":0.625},{"file":"filename2.php","commits":3,"complexity":4,"score":0.242},{"file":"filename3.php","commits":1,"complexity":5,"score":0.08},{"file":"filename4.php","commits":1,"complexity":1,"score":-0.225},{"file":"filename5.php","commits":8,"complexity":1,"score":0.143}]'
);

$renderer = new JsonResultsRenderer;
$renderer->render($output, $result);
}

/**
* @inheritdoc
*/
public function tearDown()
{
m::close();
(new JsonResultsRenderer)->render($output, $resultCollection);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Results/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function it_can_calculate_the_score()
$maxCommits = 10;
$maxComplexity = 10;

$this->assertEquals(0.41690481051547, $this->result->getScore($maxCommits, $maxComplexity));
$this->assertEquals(0.417, $this->result->getScore($maxCommits, $maxComplexity));
}

/** @test */
Expand Down

0 comments on commit e6d1afa

Please sign in to comment.