Skip to content

Commit e165ba3

Browse files
authored
Merge pull request #1 from yCodeTech/phpcs3
Fix testing on PHPCS 3.x
2 parents ec4a348 + d1c4ec3 commit e165ba3

File tree

7 files changed

+93
-107
lines changed

7 files changed

+93
-107
lines changed

.github/workflows/unit-tests.yml

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,28 @@ jobs:
6464
#
6565
# The matrix is set up so as not to duplicate the builds which are run for code coverage.
6666
php: ["7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]
67-
phpcs_version: ["4.x-dev"]
67+
phpcs_version: ["lowest", "stable", "4.x-dev"]
6868

69-
name: "Test: PHP ${{ matrix.php }}"
69+
exclude:
70+
- php: "8.3"
71+
phpcs_version: "lowest"
72+
73+
include:
74+
# Add some builds with variations of the dependency versions.
75+
- php: "8.4"
76+
phpcs_version: "stable"
77+
78+
# Test against dev versions of all dependencies with select PHP versions for early detection of issues.
79+
- php: "7.2"
80+
phpcs_version: "dev-master"
81+
- php: "7.2"
82+
phpcs_version: "4.x-dev"
83+
- php: "7.4"
84+
phpcs_version: "4.x-dev"
85+
- php: "8.2"
86+
phpcs_version: "4.x-dev"
87+
88+
name: "Test: PHP ${{ matrix.php }} - PHPCS ${{ matrix.phpcs_version }}"
7089

7190
steps:
7291
- name: Checkout code
@@ -90,6 +109,14 @@ jobs:
90109
ini-values: ${{ steps.set_ini.outputs.PHP_INI }}
91110
coverage: none
92111

112+
- name: "Composer: set PHPCS version for tests (dev/specific version)"
113+
if: ${{ matrix.phpcs_version != 'lowest' && matrix.phpcs_version != 'stable' }}
114+
run: composer require squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" --no-update --no-scripts --no-interaction
115+
116+
- name: "Composer: use lock file when necessary"
117+
if: ${{ matrix.phpcs_version == 'lowest' }}
118+
run: composer config --unset lock
119+
93120
# Install dependencies and handle caching in one go.
94121
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
95122
- name: Install Composer dependencies
@@ -98,29 +125,25 @@ jobs:
98125
# Bust the cache at least once a month - output format: YYYY-MM.
99126
custom-cache-suffix: $(date -u "+%Y-%m")
100127

128+
- name: "Composer: set PHPCS version for tests (lowest)"
129+
if: ${{ matrix.phpcs_version == 'lowest' }}
130+
run: composer update squizlabs/php_codesniffer --prefer-lowest --no-scripts --no-interaction
131+
101132
- name: Composer info
102133
run: composer info
103134

104-
- name: Grab PHPUnit version
105-
id: phpunit_version
106-
run: echo "VERSION=$(vendor/bin/phpunit --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"
135+
- name: Grab PHPCS version
136+
id: phpcs_version
137+
# yamllint disable-line rule:line-length
138+
run: echo "VERSION=$(vendor/bin/phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"
107139

108-
- name: "DEBUG: Show grabbed PHPUnit version"
109-
run: echo ${{ steps.phpunit_version.outputs.VERSION }}
140+
- name: "DEBUG: Show grabbed PHPCS version"
141+
run: echo ${{ steps.phpcs_version.outputs.VERSION }}
110142

111-
- name: Determine PHPUnit config file to use
112-
id: phpunit_config
113-
shell: bash
114-
run: |
115-
if [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '11.' ) }}" == "true" ]; then
116-
echo 'FILE=phpunit.xml' >> "$GITHUB_OUTPUT"
117-
elif [ "${{ startsWith( steps.phpunit_version.outputs.VERSION, '10.' ) }}" == "true" ]; then
118-
echo 'FILE=phpunit.xml' >> "$GITHUB_OUTPUT"
119-
else
120-
echo 'FILE=phpunit-lte9.xml' >> "$GITHUB_OUTPUT"
121-
fi
143+
- name: Run the unit tests (PHPCS 3.x)
144+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '3.' ) }}
145+
run: composer test-phpcs3
122146

123-
- name: Run the unit tests
124-
run: composer test -- -c ${{ steps.phpunit_config.outputs.FILE }}
125-
env:
126-
PHPCS_VERSION: ${{ matrix.phpcs_version }}
147+
- name: Run the unit tests (PHPCS 4.x)
148+
if: ${{ startsWith( steps.phpcs_version.outputs.VERSION, '4.' ) }}
149+
run: composer test-phpcs4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ composer.lock
77

88
# PHPUnit Cache
99
.phpunit.cache
10+
.phpunit.result.cache
1011

1112
# VScode
1213
.vscode

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Latest Stable Version](https://img.shields.io/github/v/release/yCodeTech/phpcs-standard?label=Stable)](https://packagist.org/packages/ycodetech/phpcs-standard)
44
![Minimum PHP Version](https://img.shields.io/packagist/dependency-v/yCodeTech/phpcs-standard/php?label=php)
5+
![PHPCS Version](https://img.shields.io/packagist/dependency-v/yCodeTech/phpcs-standard/squizlabs/php_codesniffer?label=PHPCS)
56
[![Unit Tests](https://github.com/yCodeTech/phpcs-standard/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/yCodeTech/phpcs-standard/actions/workflows/unit-tests.yml)
67

78
A custom [PHP_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer/tree/4.x) standard that enforces opinionated type and docblock rules with auto-fixing capabilities.

composer.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
],
1919
"require": {
2020
"php": ">=7.2",
21-
"squizlabs/php_codesniffer": "^4.0@dev",
21+
"squizlabs/php_codesniffer": "^3.13||^4.0@dev",
2222
"dealerdirect/phpcodesniffer-composer-installer": "^1.1"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "^8.5||^9.6||^10.5||^11.5",
25+
"phpunit/phpunit": "^8.5||^9.6",
2626
"phpcsstandards/phpcsdevtools": "^1.0",
2727
"php-parallel-lint/php-parallel-lint": "^1.4"
2828
},
@@ -40,18 +40,22 @@
4040
"lint": "@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git --exclude test_utils",
4141
"cs": "@php ./vendor/bin/phpcs --standard=yCodeTech",
4242
"cbf": "@php ./vendor/bin/phpcbf --standard=yCodeTech",
43-
"test": [
43+
"test-phpcs4": [
4444
"Composer\\Config::disableProcessTimeout",
4545
"@php ./vendor/bin/phpunit --no-coverage"
4646
],
47+
"test-phpcs3": [
48+
"Composer\\Config::disableProcessTimeout",
49+
"@php ./vendor/bin/phpunit --filter yCodeTech --no-coverage ./vendor/squizlabs/php_codesniffer/tests/AllTests.php"
50+
],
4751
"cs-test-file": "@cs ./test_utils/TestFile.php",
4852
"fix-test-file": "@cbf ./test_utils/TestFile.php",
4953
"restore-test-file": "bash ./test_utils/restore_errors.sh",
5054
"cs-tests": "@cs ./yCodeTech/Tests/ --extensions=inc",
5155
"test-all": [
5256
"@cs-test-file",
5357
"@cs-tests",
54-
"@test"
58+
"@test-phpcs4"
5559
],
5660
"list-tokens": "@php ./vendor/bin/phpcs --standard=PHPCSDebug ./test_utils/TestFile.php",
5761
"check-completeness": "@php ./vendor/bin/phpcs-check-feature-completeness ./yCodeTech",
@@ -61,12 +65,13 @@
6165
"lint": "Run PHP Parallel Lint to check for syntax errors in PHP files.",
6266
"cs": "Run PHPCS to check for coding standards violations.",
6367
"cbf": "Run PHPCBF to fix coding standards violations.",
64-
"test": "Run PHPUnit tests without code coverage.",
68+
"test-phpcs4": "PHPCS 4.x: Run PHPUnit tests without code coverage.",
69+
"test-phpcs3": "PHPCS 3.x: Run PHPUnit tests without code coverage.",
6570
"cs-test-file": "Run PHPCS on the test file.",
6671
"fix-test-file": "Run PHPCBF on the test file.",
6772
"restore-test-file": "Restore the test file to its original state.",
6873
"cs-tests": "Run PHPCS on all sniff unit tests.",
69-
"test-all": "Run @cs-test-file, @cs-tests, and @test in sequence.",
74+
"test-all": "Run @cs-test-file, @cs-tests, and @test-phpcs4 in sequence.",
7075
"list-tokens": "List all tokens used in the test file.",
7176
"check-completeness": "Check the completeness of the PHPCS standard.",
7277
"generate-docs": "Generate markdown documentation for the PHPCS standard."

phpunit-bootstrap.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
if (\defined('PHP_CODESNIFFER_IN_TESTS') === false) {
4+
\define('PHP_CODESNIFFER_IN_TESTS', true);
5+
}
6+
37
/*
48
* Load the necessary PHPCS files.
59
*/
@@ -25,7 +29,7 @@
2529
&& \file_exists($phpcsDir . '/tests/bootstrap.php')
2630
) {
2731
require_once $phpcsDir . '/autoload.php';
28-
require_once $phpcsDir . '/tests/bootstrap.php'; // PHPUnit 6.x+ support.
32+
require_once $phpcsDir . '/tests/bootstrap.php';
2933
} else {
3034
echo 'Uh oh... can\'t find PHPCS.
3135
@@ -37,3 +41,13 @@
3741

3842
exit(1);
3943
}
44+
45+
// Alias the PHPCS 3.x test case to the PHPCS 4.x name.
46+
if (class_exists('PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest') === true
47+
&& class_exists('PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase') === false
48+
) {
49+
class_alias(
50+
'PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest',
51+
'PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase'
52+
);
53+
}

phpunit-lte9.xml

Lines changed: 0 additions & 37 deletions
This file was deleted.

phpunit.xml

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,30 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
55
backupGlobals="true"
6-
beStrictAboutOutputDuringTests="true"
7-
beStrictAboutTestsThatDoNotTestAnything="false"
86
bootstrap="./phpunit-bootstrap.php"
9-
cacheDirectory=".phpunit.cache"
10-
displayDetailsOnTestsThatTriggerErrors="true"
11-
displayDetailsOnTestsThatTriggerWarnings="false"
12-
displayDetailsOnTestsThatTriggerNotices="true"
13-
displayDetailsOnTestsThatTriggerDeprecations="true"
14-
displayDetailsOnPhpunitDeprecations="false"
15-
failOnWarning="false"
16-
failOnNotice="true"
17-
failOnDeprecation="true"
18-
failOnPhpunitDeprecation="false"
19-
>
7+
beStrictAboutTestsThatDoNotTestAnything="false"
8+
convertErrorsToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertDeprecationsToExceptions="true"
12+
colors="true"
13+
forceCoversAnnotation="true">
14+
2015
<testsuites>
21-
<testsuite name="yCodeTech_Sniffs">
22-
<directory>./yCodeTech/Tests/</directory>
16+
<testsuite name="yCodeTech">
17+
<directory suffix="UnitTest.php">./yCodeTech/Tests/</directory>
2318
</testsuite>
2419
</testsuites>
2520

26-
<groups>
27-
<exclude>
28-
<group>CBF</group>
29-
</exclude>
30-
</groups>
31-
32-
<source>
33-
<include>
34-
<directory suffix=".php">./yCodeTech/</directory>
35-
</include>
36-
<exclude>
37-
<directory suffix="UnitTest.php">./yCodeTech/Tests/</directory>
38-
</exclude>
39-
</source>
40-
41-
<coverage includeUncoveredFiles="true">
42-
<report>
43-
<clover outputFile="build/logs/clover.xml"/>
44-
<text outputFile="php://stdout" showOnlySummary="true"/>
45-
</report>
46-
</coverage>
21+
<filter>
22+
<whitelist addUncoveredFilesFromWhitelist="true">
23+
<directory suffix=".php">./yCodeTech/Sniffs/</directory>
24+
</whitelist>
25+
</filter>
4726

48-
<php>
49-
<env name="PHP_CODESNIFFER_CBF" value="0"/>
50-
</php>
51-
</phpunit>
27+
<logging>
28+
<log type="coverage-clover" target="build/logs/clover.xml"/>
29+
</logging>
30+
</phpunit>

0 commit comments

Comments
 (0)