Skip to content

Commit afffccc

Browse files
committed
Add unit tests
1 parent 920b037 commit afffccc

File tree

10 files changed

+232
-10
lines changed

10 files changed

+232
-10
lines changed

.github/workflows/verify.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ name: Verify
33
on: [push, pull_request]
44

55
jobs:
6+
phpunit:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: shivammathur/setup-php@v1
12+
with:
13+
php-version: 7.3
14+
coverage: pcov
15+
16+
- name: Install dependencies
17+
run: composer install
18+
19+
- name: Run PHPUnit
20+
run: vendor/bin/phpunit --coverage-text
21+
622
phpcs:
723
runs-on: ubuntu-latest
824

@@ -16,10 +32,10 @@ jobs:
1632

1733
- name: Install dependencies
1834
run: composer install
19-
35+
2036
- name: Run PHP Codesniffer
2137
run: vendor/bin/phpcs --report=checkstyle -q | cs2pr
22-
38+
2339
phpstan:
2440
runs-on: ubuntu-latest
2541

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "micheh/phpcs-gitlab",
3-
"description": "PHPCodeSniffer report for Gitlab",
3+
"description": "Gitlab Report for PHP_CodeSniffer (display the violations in the Gitlab CI/CD Code Quality Report)",
44
"type": "library",
55
"license": "BSD-3-Clause",
66
"authors": [
@@ -16,11 +16,17 @@
1616
"require-dev": {
1717
"squizlabs/php_codesniffer": "^3.5",
1818
"phpstan/phpstan": "^0.12.25",
19-
"phpstan/phpstan-deprecation-rules": "^0.12.3"
19+
"phpstan/phpstan-deprecation-rules": "^0.12.3",
20+
"phpunit/phpunit": "^9.1"
2021
},
2122
"autoload": {
2223
"psr-4": {
2324
"Micheh\\PhpCodeSniffer\\": "src/"
2425
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"MichehTest\\PhpCodeSniffer\\": "tests/"
30+
}
2531
}
2632
}

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
<rule ref="PSR12"/>
1515

1616
<file>src</file>
17+
<file>tests</file>
1718
</ruleset>

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ parameters:
66
level: 8
77
paths:
88
- src
9+
- tests
910
autoload_files:
1011
- vendor/squizlabs/php_codesniffer/autoload.php
1112
checkMissingIterableValueType: false

phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
6+
bootstrap="vendor/squizlabs/php_codesniffer/autoload.php"
7+
colors="true"
8+
forceCoversAnnotation="true"
9+
>
10+
<testsuites>
11+
<testsuite name="Project Test Suite">
12+
<directory>tests</directory>
13+
</testsuite>
14+
</testsuites>
15+
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">src</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

src/Report/Gitlab.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHP_CodeSniffer\Reports\Report;
1414

1515
use function md5;
16+
use function rtrim;
1617
use function str_replace;
1718

1819
use const PHP_EOL;
@@ -45,11 +46,7 @@ public function generateFileReport($report, File $phpcsFile, $showSources = fals
4546
],
4647
];
4748

48-
if ($hasOutput) {
49-
echo ',';
50-
}
51-
52-
echo json_encode($issue, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
49+
echo json_encode($issue, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ',';
5350
$hasOutput = true;
5451
}
5552
}
@@ -72,6 +69,6 @@ public function generate(
7269
$interactive = false,
7370
$toScreen = true
7471
): void {
75-
echo '[' . $cachedData . ']' . PHP_EOL;
72+
echo '[' . rtrim($cachedData, ',') . ']' . PHP_EOL;
7673
}
7774
}

tests/Fixtures/MixedViolations.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2020, Michel Hunziker <info@michelhunziker.com>
5+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD-3-Clause License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace MichehTest\PhpCodeSniffer\Fixtures;
11+
12+
class MixedViolations implements ReportFixture
13+
{
14+
public function getReportData(): array
15+
{
16+
return [
17+
'filename' => 'tests/Report/GitlabTest.php',
18+
'errors' => 1,
19+
'warnings' => 1,
20+
'fixable' => 1,
21+
'messages' => [
22+
23 => [
23+
32 => [
24+
0 => [
25+
'message' => 'There must be a single space between the colon and type in a return type declaration',
26+
'source' => 'PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeReturnType',
27+
'severity' => 5,
28+
'fixable' => true,
29+
'type' => 'ERROR',
30+
],
31+
],
32+
],
33+
34 => [
34+
9 => [
35+
0 => [
36+
'message' => 'Line exceeds 120 characters; contains 124 characters',
37+
'source' => 'Generic.Files.LineLength.TooLong',
38+
'severity' => 5,
39+
'fixable' => false,
40+
'type' => 'WARNING',
41+
],
42+
],
43+
],
44+
],
45+
];
46+
}
47+
48+
public function getExpectedOutput(): string
49+
{
50+
return '{"type":"issue","categories":["Style"],"check_name":"PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeReturnType","fingerprint":"766bf1f7eff186fd667447bfb0291078","description":"There must be a single space between the colon and type in a return type declaration","location":{"path":"tests/Report/GitlabTest.php","lines":{"begin":23,"end":23}}},{"type":"issue","categories":["Style"],"check_name":"Generic.Files.LineLength.TooLong","fingerprint":"517eda4b85a33cd09fc933259f873191","description":"Line exceeds 120 characters; contains 124 characters","location":{"path":"tests/Report/GitlabTest.php","lines":{"begin":34,"end":34}}},';
51+
}
52+
}

tests/Fixtures/ReportFixture.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2020, Michel Hunziker <info@michelhunziker.com>
5+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD-3-Clause License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace MichehTest\PhpCodeSniffer\Fixtures;
11+
12+
interface ReportFixture
13+
{
14+
public function getReportData(): array;
15+
16+
public function getExpectedOutput(): string;
17+
}

tests/Fixtures/SingleViolation.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2020, Michel Hunziker <info@michelhunziker.com>
5+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD-3-Clause License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace MichehTest\PhpCodeSniffer\Fixtures;
11+
12+
class SingleViolation implements ReportFixture
13+
{
14+
public function getReportData(): array
15+
{
16+
return [
17+
'filename' => 'src/Report/Gitlab.php',
18+
'errors' => 1,
19+
'warnings' => 0,
20+
'fixable' => 1,
21+
'messages' => [
22+
18 => [
23+
25 => [
24+
0 => [
25+
'message' => 'Header blocks must not contain blank lines',
26+
'source' => 'PSR12.Files.FileHeader.SpacingInsideBlock',
27+
'severity' => 5,
28+
'fixable' => true,
29+
'type' => 'ERROR',
30+
],
31+
],
32+
],
33+
],
34+
];
35+
}
36+
37+
public function getExpectedOutput(): string
38+
{
39+
return '{"type":"issue","categories":["Style"],"check_name":"PSR12.Files.FileHeader.SpacingInsideBlock","fingerprint":"21f4cd5b7d88d9eb67402be5d56caa8b","description":"Header blocks must not contain blank lines","location":{"path":"src/Report/Gitlab.php","lines":{"begin":18,"end":18}}},';
40+
}
41+
}

tests/Report/GitlabTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2020, Michel Hunziker <info@michelhunziker.com>
5+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD-3-Clause License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace MichehTest\PhpCodeSniffer\Report;
11+
12+
use Micheh\PhpCodeSniffer\Report\Gitlab;
13+
use MichehTest\PhpCodeSniffer\Fixtures\MixedViolations;
14+
use MichehTest\PhpCodeSniffer\Fixtures\ReportFixture;
15+
use MichehTest\PhpCodeSniffer\Fixtures\SingleViolation;
16+
use PHP_CodeSniffer\Files\File;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class GitlabTest extends TestCase
20+
{
21+
/**
22+
* @var Gitlab
23+
*/
24+
private $report;
25+
26+
27+
protected function setUp(): void
28+
{
29+
$this->report = new Gitlab();
30+
}
31+
32+
/**
33+
* @covers \Micheh\PhpCodeSniffer\Report\Gitlab::generate
34+
*/
35+
public function testGenerate(): void
36+
{
37+
$this->expectOutputString("[{\"phpunit\":\"test\"}]\n");
38+
$this->report->generate('{"phpunit":"test"},', 5, 1, 2, 1);
39+
}
40+
41+
/**
42+
* @covers \Micheh\PhpCodeSniffer\Report\Gitlab::generate
43+
*/
44+
public function testGenerateWithEmpty(): void
45+
{
46+
$this->expectOutputString("[]\n");
47+
$this->report->generate('', 5, 0, 0, 0);
48+
}
49+
50+
public function violations(): array
51+
{
52+
return [
53+
'single' => [SingleViolation::class],
54+
'mixed' => [MixedViolations::class],
55+
];
56+
}
57+
58+
/**
59+
* @covers \Micheh\PhpCodeSniffer\Report\Gitlab::generateFileReport
60+
* @dataProvider violations
61+
*/
62+
public function testGenerateFileReport(string $class): void
63+
{
64+
$fixture = new $class();
65+
self::assertInstanceOf(ReportFixture::class, $fixture);
66+
67+
$this->expectOutputString($fixture->getExpectedOutput());
68+
$this->report->generateFileReport($fixture->getReportData(), $this->createMock(File::class));
69+
}
70+
}

0 commit comments

Comments
 (0)