Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ composer.lock
.php_cs
.php_cs.cache
.phpunit.result.cache
.phpunit.cache/test-results
build

coverage
Expand Down
1 change: 1 addition & 0 deletions src/Application/Formatters/FormatResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class FormatResolver
/** @var array<non-empty-string, class-string<Formatter>> */
public const FORMATTERS = [
'github' => GithubFormatter::class,
'json' => JsonFormatter::class,
'table' => TableFormatter::class,
];

Expand Down
45 changes: 45 additions & 0 deletions src/Application/Formatters/JsonFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Juampi92\Phecks\Application\Formatters;

use Juampi92\Phecks\Application\Contracts\Formatter;
use Juampi92\Phecks\Domain\Violations\Violation;
use Juampi92\Phecks\Domain\Violations\ViolationsCollection;
use Juampi92\Phecks\Domain\Violations\ViolationSeverity;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class JsonFormatter implements Formatter
{
protected OutputInterface $output;

public function __construct(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
}

public function format(ViolationsCollection $violations): void
{
$report = [
'package' => 'phecks',
'violations' => $violations->toArray(),
'summary' => $this->buildSummary($violations),
'total' => $violations->count(),
];

$this->output->writeln(json_encode($report, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT));
}

/**
* @return array{errors: int, warnings: int}
*/
private function buildSummary(ViolationsCollection $violations): array
{
$groupedBySeverity = $violations->groupBy->getSeverity();

return [
'errors' => $groupedBySeverity->get(ViolationSeverity::ERROR)?->count() ?? 0,
'warnings' => $groupedBySeverity->get(ViolationSeverity::WARNING)?->count() ?? 0,
];
}
}
18 changes: 17 additions & 1 deletion src/Domain/Violations/Violation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Juampi92\Phecks\Domain\Violations;

use Illuminate\Contracts\Support\Arrayable;
use Juampi92\Phecks\Domain\DTOs\FileMatch;

class Violation
class Violation implements Arrayable
{
private string $identifier;

Expand Down Expand Up @@ -85,4 +86,19 @@ public function setSeverity(string $severity): self

return $this;
}

/**
* @return array{identifier: string, file: string, line: int|null, message: string, url: string|null, severity: string}
*/
public function toArray(): array
{
return [
'identifier' => $this->getIdentifier(),
'file' => $this->getTarget(),
'line' => $this->getLine(),
'message' => $this->getMessage(),
'url' => $this->getUrl(),
'severity' => $this->getSeverity(),
];
}
}
5 changes: 5 additions & 0 deletions tests/Unit/Formatters/FormatResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Juampi92\Phecks\Application\Formatters\FormatResolver;
use Juampi92\Phecks\Application\Formatters\GithubFormatter;
use Juampi92\Phecks\Application\Formatters\JsonFormatter;
use Juampi92\Phecks\Application\Formatters\TableFormatter;
use Juampi92\Phecks\Tests\Unit\TestCase;
use Mockery;
Expand Down Expand Up @@ -39,6 +40,10 @@ public static function formatterDataProvider(): array
'formatter' => 'github',
'expected' => GithubFormatter::class,
],
'json' => [
'formatter' => 'json',
'expected' => JsonFormatter::class,
],
];
}

Expand Down
188 changes: 188 additions & 0 deletions tests/Unit/Formatters/JsonFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

namespace Juampi92\Phecks\Tests\Unit\Formatters;

use Juampi92\Phecks\Application\Formatters\JsonFormatter;
use Juampi92\Phecks\Domain\DTOs\FileMatch;
use Juampi92\Phecks\Domain\Violations\Violation;
use Juampi92\Phecks\Domain\Violations\ViolationsCollection;
use Juampi92\Phecks\Domain\Violations\ViolationSeverity;
use Juampi92\Phecks\Tests\Unit\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

class JsonFormatterTest extends TestCase
{
public function test_can_render_empty_violations(): void
{
// Arrange

$formatter = new JsonFormatter(
new ArrayInput([]),
$output = new BufferedOutput(),
);

$violations = ViolationsCollection::empty();

$expectedReport = $this->loadStub('json-empty-violations.json');

// Act

$formatter->format($violations);

// Assert

$actualReport = json_decode($output->fetch(), true);

$this->assertEquals($expectedReport, $actualReport);
}

public function test_can_render_single_error_violation(): void
{
// Arrange

$formatter = new JsonFormatter(
new ArrayInput([]),
$output = new BufferedOutput(),
);

$violations = new ViolationsCollection([
new Violation(
'MyError',
new FileMatch('./app/FileOne.php', 15),
'Testing error',
'https://www.foo.bar'
),
]);

$expectedReport = $this->loadStub('json-single-error.json');

// Act

$formatter->format($violations);

// Assert

$actualReport = json_decode($output->fetch(), true);

$this->assertEquals($expectedReport, $actualReport);
}

public function test_can_render_multiple_violations_with_mixed_severities(): void
{
// Arrange

$formatter = new JsonFormatter(
new ArrayInput([]),
$output = new BufferedOutput(),
);

$violations = new ViolationsCollection([
new Violation(
'FirstError',
new FileMatch('./app/FileOne.php', 15),
'First error message',
'https://docs.example.com'
),
new Violation(
'FirstWarning',
new FileMatch('./app/FileTwo.php', 30),
'First warning message',
null,
ViolationSeverity::WARNING
),
new Violation(
'SecondError',
new FileMatch('./app/FileThree.php', 45),
'Second error message',
null,
ViolationSeverity::ERROR
),
]);

$expectedReport = $this->loadStub('json-mixed-violations.json');

// Act

$formatter->format($violations);

// Assert

$actualReport = json_decode($output->fetch(), true);

$this->assertEquals($expectedReport, $actualReport);
}

public function test_can_render_violation_without_line_number(): void
{
// Arrange

$formatter = new JsonFormatter(
new ArrayInput([]),
$output = new BufferedOutput(),
);

$violations = new ViolationsCollection([
new Violation(
'NoLineError',
new FileMatch('./app/FileOne.php'),
'Error without line number',
null
),
]);

$expectedReport = $this->loadStub('json-violation-without-line.json');

// Act

$formatter->format($violations);

// Assert

$actualReport = json_decode($output->fetch(), true);

$this->assertEquals($expectedReport, $actualReport);
}

public function test_output_is_valid_json(): void
{
// Arrange

$formatter = new JsonFormatter(
new ArrayInput([]),
$output = new BufferedOutput(),
);

$violations = new ViolationsCollection([
new Violation(
'TestError',
new FileMatch('./app/Test.php', 10),
'Test message',
null
),
]);

// Act

$formatter->format($violations);

$jsonOutput = $output->fetch();

// Assert

$this->assertNotFalse(json_decode($jsonOutput));

$this->assertEquals(JSON_ERROR_NONE, json_last_error());
}

/*
* Helpers.
*/

private function loadStub(string $filename): array
{
$content = file_get_contents(__DIR__ . '/stubs/' . $filename);

return json_decode($content, true);
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Formatters/stubs/json-empty-violations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"package": "phecks",
"violations": [],
"summary": {
"errors": 0,
"warnings": 0
},
"total": 0
}
34 changes: 34 additions & 0 deletions tests/Unit/Formatters/stubs/json-mixed-violations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"package": "phecks",
"violations": [
{
"identifier": "FirstError",
"file": "./app/FileOne.php",
"line": 15,
"message": "First error message",
"url": "https://docs.example.com",
"severity": "error"
},
{
"identifier": "FirstWarning",
"file": "./app/FileTwo.php",
"line": 30,
"message": "First warning message",
"url": null,
"severity": "warning"
},
{
"identifier": "SecondError",
"file": "./app/FileThree.php",
"line": 45,
"message": "Second error message",
"url": null,
"severity": "error"
}
],
"summary": {
"errors": 2,
"warnings": 1
},
"total": 3
}
18 changes: 18 additions & 0 deletions tests/Unit/Formatters/stubs/json-single-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"package": "phecks",
"violations": [
{
"identifier": "MyError",
"file": "./app/FileOne.php",
"line": 15,
"message": "Testing error",
"url": "https://www.foo.bar",
"severity": "error"
}
],
"summary": {
"errors": 1,
"warnings": 0
},
"total": 1
}
18 changes: 18 additions & 0 deletions tests/Unit/Formatters/stubs/json-violation-without-line.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"package": "phecks",
"violations": [
{
"identifier": "NoLineError",
"file": "./app/FileOne.php",
"line": null,
"message": "Error without line number",
"url": null,
"severity": "error"
}
],
"summary": {
"errors": 1,
"warnings": 0
},
"total": 1
}