Skip to content

Commit

Permalink
Add support for markdown format
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored May 10, 2022
1 parent 6c908c1 commit 829dc39
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,4 @@ jobs:
run: chmod +x churn.phar

- name: "Run Phar"
run: ./churn.phar
run: ./churn.phar --format=markdown >> $GITHUB_STEP_SUMMARY
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ You can configure `churn` to output the result in different formats. The availab

* `csv`
* `json`
* `markdown`
* `text` (default)

To use a different format use `--format` option. Example command for `json`:
Expand Down
43 changes: 43 additions & 0 deletions src/Result/Render/MarkdownResultsRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Churn\Result\Render;

use Symfony\Component\Console\Output\OutputInterface;

/**
* @internal
*/
final class MarkdownResultsRenderer implements ResultsRendererInterface
{
/**
* Renders the results.
*
* @param OutputInterface $output Output Interface.
* @param array<array<float|integer|string>> $results The results.
*/
public function render(OutputInterface $output, array $results): void
{
$output->writeln('| File | Times Changed | Complexity | Score |');
$output->writeln('|------|---------------|------------|-------|');

foreach ($results as $result) {
$output->writeln($this->inline($result));
}
}

/**
* @param array<float|integer|string> $data The data to inline.
*/
private function inline(array $data): string
{
$escapedData = \array_map(static function ($item) {
return \is_string($item)
? \str_replace('|', '\\|', $item)
: $item;
}, $data);

return '| ' . \implode(' | ', $escapedData) . ' |';
}
}
8 changes: 7 additions & 1 deletion src/Result/ResultsRendererFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Churn\Result\Render\ConsoleResultsRenderer;
use Churn\Result\Render\CsvResultsRenderer;
use Churn\Result\Render\JsonResultsRenderer;
use Churn\Result\Render\MarkdownResultsRenderer;
use Churn\Result\Render\ResultsRendererInterface;
use InvalidArgumentException;

Expand All @@ -15,8 +16,9 @@
*/
final class ResultsRendererFactory
{
private const FORMAT_JSON = 'json';
private const FORMAT_CSV = 'csv';
private const FORMAT_JSON = 'json';
private const FORMAT_MD = 'markdown';
private const FORMAT_TEXT = 'text';

/**
Expand All @@ -35,6 +37,10 @@ public function getRenderer(string $format): ResultsRendererInterface
return new JsonResultsRenderer();
}

if (self::FORMAT_MD === $format) {
return new MarkdownResultsRenderer();
}

if (self::FORMAT_TEXT === $format) {
return new ConsoleResultsRenderer();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Result/Render/CsvResultsRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function it_can_be_instantiated()
}

/** @test */
public function it_can_render_the_results_as_json()
public function it_can_render_the_results_as_csv()
{
$results = [
['filename1.php', 5, 7, 0.625],
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Result/Render/MarkdownResultsRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Churn\Tests\Unit\Result\Render;

use Churn\Result\Render\MarkdownResultsRenderer;
use Churn\Tests\BaseTestCase;
use Mockery as m;
use Symfony\Component\Console\Output\OutputInterface;

class MarkdownResultsRendererTest extends BaseTestCase
{
/** @test */
public function it_can_be_instantiated()
{
$this->assertInstanceOf(MarkdownResultsRenderer::class, new MarkdownResultsRenderer());
}

/** @test */
public function it_can_render_the_results_as_markdown()
{
$results = [
['filename1.php', 5, 7, 0.625],
['path/filename2.php', 3, 4, 0.242],
['pa|th/filename3.php', 1, 5, 0.08],
];

$output = m::mock(OutputInterface::class);
$output->shouldReceive('writeln')->once()->with('| File | Times Changed | Complexity | Score |');
$output->shouldReceive('writeln')->once()->with('|------|---------------|------------|-------|');
$output->shouldReceive('writeln')->once()->with('| filename1.php | 5 | 7 | 0.625 |');
$output->shouldReceive('writeln')->once()->with('| path/filename2.php | 3 | 4 | 0.242 |');
$output->shouldReceive('writeln')->once()->with('| pa\\|th/filename3.php | 1 | 5 | 0.08 |');

(new MarkdownResultsRenderer())->render($output, $results);
}
}
7 changes: 7 additions & 0 deletions tests/Unit/Result/ResultsRendererFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Churn\Result\Render\ConsoleResultsRenderer;
use Churn\Result\Render\CsvResultsRenderer;
use Churn\Result\Render\JsonResultsRenderer;
use Churn\Result\Render\MarkdownResultsRenderer;
use Churn\Result\ResultsRendererFactory;
use Churn\Tests\BaseTestCase;
use InvalidArgumentException;
Expand Down Expand Up @@ -36,6 +37,12 @@ public function it_returns_the_csv_renderer_when_provided_csv_format()
$this->assertInstanceOf(CsvResultsRenderer::class, $this->factory->getRenderer('csv'));
}

/** @test */
public function it_returns_the_markdown_renderer_when_provided_markdown_format()
{
$this->assertInstanceOf(MarkdownResultsRenderer::class, $this->factory->getRenderer('markdown'));
}

/** @test */
public function it_returns_the_console_renderer_when_provided_text_format()
{
Expand Down

0 comments on commit 829dc39

Please sign in to comment.