Skip to content

Commit bc1260b

Browse files
Set up GitHub actions
Automatically run phpcs and phpunit Update tests so that they pass on PHP 8.3 and when running in GitHub actions and have long file names that phpcs shortens.
1 parent fd9cfbc commit bc1260b

File tree

5 files changed

+79
-10
lines changed

5 files changed

+79
-10
lines changed

.github/workflows/ci.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
lint:
11+
name: Lint with PHPCS
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Setup PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: 8.4
21+
tools: composer
22+
23+
- name: Install composer dependencies
24+
run: composer install
25+
26+
- name: Run phpcs
27+
run: vendor/bin/phpcs -p -s
28+
29+
test:
30+
name: "PHPUnit: PHP ${{ matrix.php }}"
31+
runs-on: ubuntu-latest
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
php:
36+
- 8.3
37+
- 8.4
38+
steps:
39+
- name: Checkout
40+
uses: actions/checkout@v4
41+
42+
- name: Setup PHP
43+
uses: shivammathur/setup-php@v2
44+
with:
45+
php-version: ${{ matrix.php }}
46+
tools: composer
47+
48+
- name: Install composer dependencies
49+
run: composer install
50+
51+
- name: Run phpunit
52+
run: vendor/bin/phpunit

src/Tests/SniffOutputTest.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class SniffOutputTest extends TestCase {
2424

2525
private string $standard;
2626
private Config $phpcsConfig;
27+
private const REPORT_WIDTH = 80;
2728

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

4142
$this->phpcsConfig = $config;
4243
}
@@ -69,12 +70,28 @@ public function testCodesnifferRules( string $file ) {
6970
$this->assertFileExists( $reportFile );
7071
$reportFileContents = file_get_contents( $reportFile );
7172
// Use a placeholder `{dir}` so that tests pass regardless of where
72-
// in the filesystem the code is located.
73-
$reportFileContents = str_replace(
74-
"FILE: {dir}/",
75-
"FILE: " . __DIR__ . "/",
76-
$reportFileContents
77-
);
73+
// in the filesystem the code is located. But, codesniffer will trim
74+
// the file name used if it would otherwise exceed the report width,
75+
// see `PHP_CodeSniffer\Reports\Full::generateFileReport()` (as of
76+
// codesniffer 3.11.3)
77+
if ( strlen( $file ) <= ( self::REPORT_WIDTH - 6 ) ) {
78+
$reportFileContents = str_replace(
79+
"FILE: {dir}/",
80+
"FILE: " . __DIR__ . "/",
81+
$reportFileContents
82+
);
83+
} else {
84+
$dotdotdotDir = '...' . substr(
85+
__DIR__,
86+
strlen( $file ) - ( self::REPORT_WIDTH - 6 )
87+
);
88+
89+
$reportFileContents = str_replace(
90+
"FILE: {dir}/",
91+
"FILE: " . $dotdotdotDir . "/",
92+
$reportFileContents
93+
);
94+
}
7895
$this->assertSame( trim( $reportFileContents ), trim( $report ) );
7996

8097
// .fixed file must exist and match the fixed output if there are

src/Tests/data/ExtraSniffsApplied.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
declare( strict_types = 1 );
33

44
function usesDeprecated() {
5-
lcg_value();
5+
utf8_encode( "foo" );
66
}
77

88
#[FirstAttrib, SecondAttrib]

src/Tests/data/ExtraSniffsApplied.php.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
declare( strict_types = 1 );
33

44
function usesDeprecated() {
5-
lcg_value();
5+
utf8_encode( "foo" );
66
}
77

88
#[FirstAttrib]

src/Tests/data/ExtraSniffsApplied.report

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FILE: {dir}/data/ExtraSniffsApplied.php
22
--------------------------------------------------------------------------------
33
FOUND 2 ERRORS AFFECTING 2 LINES
44
--------------------------------------------------------------------------------
5-
5 | ERROR | [ ] Function lcg_value() has been deprecated
5+
5 | ERROR | [ ] Function utf8_encode() has been deprecated
66
| | (Generic.PHP.DeprecatedFunctions.Deprecated)
77
8 | ERROR | [x] 2 attributes are joined.
88
| | (SlevomatCodingStandard.Attributes.DisallowAttributesJoining.DisallowedAttributesJoining)

0 commit comments

Comments
 (0)