From 621dd401b20d5d578e770506b7131623f3a5edd1 Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Fri, 14 Feb 2025 21:33:16 -0800 Subject: [PATCH 1/3] Set up GitHub actions Automatically run phpcs and phpunit --- .github/workflows/ci.yml | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a5a033a --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 From 7a0f0c91344a35e86919dc1663bf037b9f9dec6c Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Fri, 14 Feb 2025 21:38:42 -0800 Subject: [PATCH 2/3] Tests: test a function deprecated before 8.4 So that the tests that run on 8.3 pass --- src/Tests/data/ExtraSniffsApplied.php | 2 +- src/Tests/data/ExtraSniffsApplied.php.fixed | 2 +- src/Tests/data/ExtraSniffsApplied.report | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Tests/data/ExtraSniffsApplied.php b/src/Tests/data/ExtraSniffsApplied.php index 85559a1..855fa4d 100644 --- a/src/Tests/data/ExtraSniffsApplied.php +++ b/src/Tests/data/ExtraSniffsApplied.php @@ -2,7 +2,7 @@ declare( strict_types = 1 ); function usesDeprecated() { - lcg_value(); + utf8_encode( "foo" ); } #[FirstAttrib, SecondAttrib] diff --git a/src/Tests/data/ExtraSniffsApplied.php.fixed b/src/Tests/data/ExtraSniffsApplied.php.fixed index 8057319..958efa3 100644 --- a/src/Tests/data/ExtraSniffsApplied.php.fixed +++ b/src/Tests/data/ExtraSniffsApplied.php.fixed @@ -2,7 +2,7 @@ declare( strict_types = 1 ); function usesDeprecated() { - lcg_value(); + utf8_encode( "foo" ); } #[FirstAttrib] diff --git a/src/Tests/data/ExtraSniffsApplied.report b/src/Tests/data/ExtraSniffsApplied.report index 83026a8..17f93e3 100644 --- a/src/Tests/data/ExtraSniffsApplied.report +++ b/src/Tests/data/ExtraSniffsApplied.report @@ -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) From b8f4cd0dd89475e2cf6043405bdc7e60bdad18ec Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Fri, 14 Feb 2025 22:08:45 -0800 Subject: [PATCH 3/3] Tests: work around phpcs handling of long paths --- src/Tests/SniffOutputTest.php | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Tests/SniffOutputTest.php b/src/Tests/SniffOutputTest.php index 4c11cf0..897a899 100644 --- a/src/Tests/SniffOutputTest.php +++ b/src/Tests/SniffOutputTest.php @@ -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(); @@ -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; } @@ -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