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
52 changes: 52 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: CI

on:
push:
branches:
- main
pull_request:

jobs:
lint:
name: Lint with PHPCS
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.4
tools: composer

- name: Install composer dependencies
run: composer install

- name: Run phpcs
run: vendor/bin/phpcs -p -s

test:
name: "PHPUnit: PHP ${{ matrix.php }}"
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php:
- 8.3
- 8.4
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer

- name: Install composer dependencies
run: composer install

- name: Run phpunit
run: vendor/bin/phpunit
31 changes: 24 additions & 7 deletions src/Tests/SniffOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SniffOutputTest extends TestCase {

private string $standard;
private Config $phpcsConfig;
private const REPORT_WIDTH = 80;

protected function setUp(): void {
parent::setUp();
Expand All @@ -36,7 +37,7 @@ protected function setUp(): void {
// for some reason passing `--tab-width=4` doesn't work, set manually
$config->tabWidth = 4;
// same with passing --report-width
$config->reportWidth = 80;
$config->reportWidth = self::REPORT_WIDTH;

$this->phpcsConfig = $config;
}
Expand Down Expand Up @@ -69,12 +70,28 @@ public function testCodesnifferRules( string $file ) {
$this->assertFileExists( $reportFile );
$reportFileContents = file_get_contents( $reportFile );
// Use a placeholder `{dir}` so that tests pass regardless of where
// in the filesystem the code is located.
$reportFileContents = str_replace(
"FILE: {dir}/",
"FILE: " . __DIR__ . "/",
$reportFileContents
);
// in the filesystem the code is located. But, codesniffer will trim
// the file name used if it would otherwise exceed the report width,
// see `PHP_CodeSniffer\Reports\Full::generateFileReport()` (as of
// codesniffer 3.11.3)
if ( strlen( $file ) <= ( self::REPORT_WIDTH - 6 ) ) {
$reportFileContents = str_replace(
"FILE: {dir}/",
"FILE: " . __DIR__ . "/",
$reportFileContents
);
} else {
$dotdotdotDir = '...' . substr(
__DIR__,
strlen( $file ) - ( self::REPORT_WIDTH - 6 )
);

$reportFileContents = str_replace(
"FILE: {dir}/",
"FILE: " . $dotdotdotDir . "/",
$reportFileContents
);
}
$this->assertSame( trim( $reportFileContents ), trim( $report ) );

// .fixed file must exist and match the fixed output if there are
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/data/ExtraSniffsApplied.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
declare( strict_types = 1 );

function usesDeprecated() {
lcg_value();
utf8_encode( "foo" );
}

#[FirstAttrib, SecondAttrib]
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/data/ExtraSniffsApplied.php.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
declare( strict_types = 1 );

function usesDeprecated() {
lcg_value();
utf8_encode( "foo" );
}

#[FirstAttrib]
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/data/ExtraSniffsApplied.report
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FILE: {dir}/data/ExtraSniffsApplied.php
--------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
--------------------------------------------------------------------------------
5 | ERROR | [ ] Function lcg_value() has been deprecated
5 | ERROR | [ ] Function utf8_encode() has been deprecated
| | (Generic.PHP.DeprecatedFunctions.Deprecated)
8 | ERROR | [x] 2 attributes are joined.
| | (SlevomatCodingStandard.Attributes.DisallowAttributesJoining.DisallowedAttributesJoining)
Expand Down