Skip to content

Commit f0607d3

Browse files
committed
🔧 QA: enable unit testing for the code in this repo
This: * Adds a PHPUnit `phpunit.xml.dist` configuration file. * Adds a `phpunit-bootstrap.php` file to load the necessary prerequisites for the unit testing. The PHPCS unit test suite needs a `bootstrap` file as of PHPCS 3.1.0 which, as of then, also supports PHPUnit 6.x. * Adds convenience scripts to the `composer.json` file to test the code of the repo. * Adds the necessary changes to the Travis script to test against the relevant PHP / PHPCS combinations. * And allows for individual developers to overload the `phpunit.xml.dist` file by ignoring the typical overload files.
1 parent 6ad034b commit f0607d3

7 files changed

Lines changed: 180 additions & 5 deletions

File tree

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
/.gitignore export-ignore
1010
/.travis.yml export-ignore
1111
/phpcs.xml.dist export-ignore
12+
/phpunit.xml.dist export-ignore
13+
/phpunit-bootstrap.php export-ignore
1214
/Debug/Tests export-ignore
1315

1416
#

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ vendor/
22
composer.lock
33
.phpcs.xml
44
phpcs.xml
5+
phpunit.xml

.travis.yml

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,39 @@ cache:
1313

1414
php:
1515
- 5.4
16-
- 7.3
16+
- 5.5
17+
- 5.6
18+
- 7.0
19+
- 7.1
20+
- 7.2
21+
22+
env:
23+
matrix:
24+
# `master`
25+
- PHPCS_VERSION="dev-master" LINT=1
26+
# Lowest supported PHPCS version.
27+
# In reality this is 3.0.2, but there is a bug in the unit test runner of that version.
28+
- PHPCS_VERSION="3.0.2"
1729

1830
# Define the stages used.
31+
# For non-PRs, only the sniff and quicktest stages are run.
32+
# For pull requests and merges, the full script is run (skipping quicktest).
33+
# Note: for pull requests, "develop" is the base branch name.
34+
# See: https://docs.travis-ci.com/user/conditions-v1
1935
stages:
2036
- name: sniff
37+
- name: quicktest
38+
if: type = push AND branch NOT IN (master, develop)
2139
- name: test
40+
if: branch IN (master, develop)
2241

2342
jobs:
2443
fast_finish: true
2544
include:
2645
#### SNIFF STAGE ####
2746
- stage: sniff
2847
php: 7.3
48+
env: PHPCS_VERSION="dev-master"
2949
addons:
3050
apt:
3151
packages:
@@ -44,26 +64,81 @@ jobs:
4464
- diff -B ./PHPCSDev/ruleset.xml <(xmllint --format "./PHPCSDev/ruleset.xml")
4565

4666

67+
#### QUICK TEST STAGE ####
68+
# This is a much quicker test which only runs the unit tests and linting against the low/high
69+
# supported PHP/PHPCS combinations.
70+
- stage: quicktest
71+
php: 7.3
72+
env: PHPCS_VERSION="dev-master" LINT=1
73+
- php: 7.2
74+
env: PHPCS_VERSION="3.0.2"
75+
76+
- php: 5.4
77+
env: PHPCS_VERSION="dev-master" LINT=1
78+
- php: 5.4
79+
env: PHPCS_VERSION="3.0.2"
80+
81+
#### TEST STAGE ####
82+
# Additional builds to prevent issues with PHPCS versions incompatible with certain PHP versions.
83+
- stage: test
84+
php: 7.3
85+
env: PHPCS_VERSION="dev-master" LINT=1
86+
# PHPCS is only compatible with PHP 7.3 as of version 3.3.1.
87+
- php: 7.3
88+
env: PHPCS_VERSION="3.3.1"
89+
# PHPCS is only compatible with PHP 7.4 as of version 3.5.0.
90+
- php: "7.4snapshot"
91+
env: PHPCS_VERSION=dev-master"
92+
93+
allow_failures:
94+
# Allow failures for unstable builds.
95+
- php: "7.4snapshot"
96+
97+
4798
before_install:
4899
# Speed up build time by disabling Xdebug when its not needed.
49100
- phpenv config-rm xdebug.ini || echo 'No xdebug config.'
50101

102+
# On stable PHPCS versions, allow for PHP deprecation notices.
103+
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
104+
- |
105+
if [[ "$TRAVIS_BUILD_STAGE_NAME" != "Sniff" && $PHPCS_BRANCH != "dev-master" ]]; then
106+
echo 'error_reporting = E_ALL & ~E_DEPRECATED' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
107+
fi
108+
51109
- export XMLLINT_INDENT=" "
52110

111+
# Set up test environment using Composer.
112+
- composer require --no-update --no-scripts squizlabs/php_codesniffer:${PHPCS_VERSION}
113+
- |
114+
if [[ "$TRAVIS_BUILD_STAGE_NAME" == "Sniff" ]]; then
115+
# The sniff stage doesn't run the unit tests, so no need for PHPUnit.
116+
composer remove --dev phpunit/phpunit --no-update --no-scripts
117+
elif [[ "$PHPCS_VERSION" < "3.1.0" ]]; then
118+
# PHPCS < 3.1.0 is not compatible with PHPUnit 6.x.
119+
composer require --dev phpunit/phpunit:"^4.0||^5.0" --no-update --no-scripts
120+
elif [[ "$PHPCS_VERSION" < "3.2.3" ]]; then
121+
# PHPCS < 3.2.3 is not compatible with PHPUnit 7.x.
122+
composer require --dev phpunit/phpunit:"^4.0||^5.0||^6.0" --no-update --no-scripts
123+
fi
124+
53125
# --prefer-dist will allow for optimal use of the travis caching ability.
54126
# The Composer PHPCS plugin takes care of setting the installed_paths for PHPCS.
55127
- composer install --prefer-dist --no-suggest
56128

57129

58130
script:
59131
# Lint PHP files against parse errors.
60-
- composer lint
132+
- if [[ "$LINT" == "1" ]]; then composer lint; fi
61133

62134
# Check that any sniffs available are feature complete.
63135
# This also acts as an integration test for the feature completeness script,
64136
# which is why it is run against various PHP versions and not in the "Sniff" stage.
65-
- composer check-complete
137+
- if [[ "$LINT" == "1" ]]; then composer check-complete; fi
66138

67-
# Validate the composer.json file on low/high PHP versions.
139+
# Validate the composer.json file.
68140
# @link https://getcomposer.org/doc/03-cli.md#validate
69-
- composer validate --no-check-all --strict
141+
- if [[ "$LINT" == "1" ]]; then composer validate --no-check-all --strict; fi
142+
143+
# Run the unit tests.
144+
- composer run-tests

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"require-dev" : {
2929
"roave/security-advisories" : "dev-master",
30+
"phpunit/phpunit" : "^4.5 || ^5.0 || ^6.0 || ^7.0",
3031
"jakub-onderka/php-parallel-lint": "^1.0",
3132
"jakub-onderka/php-console-highlighter": "^0.4"
3233
},
@@ -43,6 +44,9 @@
4344
"fix-cs": [
4445
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf"
4546
],
47+
"run-tests": [
48+
"@php ./vendor/phpunit/phpunit/phpunit --filter PHPCSStandards ./vendor/squizlabs/php_codesniffer/tests/AllTests.php"
49+
],
4650
"check-complete": [
4751
"@php ./bin/phpcs-check-feature-completeness ./Debug"
4852
]

phpcs.xml.dist

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,16 @@
4040
<!-- Enforce short arrays. -->
4141
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
4242

43+
44+
<!--
45+
#############################################################################
46+
SELECTIVE EXCLUSIONS
47+
Exclude specific files for specific sniffs and/or exclude sub-groups in sniffs.
48+
#############################################################################
49+
-->
50+
51+
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
52+
<exclude-pattern>/phpunit-bootstrap\.php$</exclude-pattern>
53+
</rule>
54+
4355
</ruleset>

phpunit-bootstrap.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* PHPCSDevTools, tools for PHP_CodeSniffer sniff developers.
4+
*
5+
* Bootstrap file for running the tests.
6+
*
7+
* - Load the PHPCS PHPUnit bootstrap file providing cross-version PHPUnit support.
8+
* {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1384}
9+
*
10+
* @package PHPCSDevTools
11+
* @copyright 2019 PHPCSDevTools Contributors
12+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
13+
* @link https://github.com/PHPCSStandards/PHPCSDevTools
14+
*/
15+
16+
if (defined('PHP_CODESNIFFER_IN_TESTS') === false) {
17+
define('PHP_CODESNIFFER_IN_TESTS', true);
18+
}
19+
20+
$ds = DIRECTORY_SEPARATOR;
21+
22+
/*
23+
* Load the necessary PHPCS files.
24+
*/
25+
// Get the PHPCS dir from an environment variable.
26+
$phpcsDir = getenv('PHPCS_DIR');
27+
$composerPHPCSPath = __DIR__ . $ds . 'vendor' . $ds . 'squizlabs' . $ds . 'php_codesniffer';
28+
29+
if ($phpcsDir === false && is_dir($composerPHPCSPath)) {
30+
// PHPCS installed via Composer.
31+
$phpcsDir = $composerPHPCSPath;
32+
} elseif ($phpcsDir !== false) {
33+
/*
34+
* PHPCS in a custom directory.
35+
* For this to work, the `PHPCS_DIR` needs to be set in a custom `phpunit.xml` file.
36+
*/
37+
$phpcsDir = realpath($phpcsDir);
38+
}
39+
40+
// Try and load the PHPCS bootstrap which loads the autoloader and PHPUnit aliases.
41+
if ($phpcsDir !== false && is_dir($phpcsDir)) {
42+
if (file_exists($phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php')) {
43+
require_once $phpcsDir . $ds . 'tests' . $ds . 'bootstrap.php'; // PHPUnit 6.x+ support as of PHPCS 3.1.0.
44+
}
45+
} else {
46+
echo 'Uh oh... can\'t find PHPCS.
47+
48+
If you use Composer, please run `composer install`.
49+
Otherwise, make sure you set a `PHPCS_DIR` environment variable in your phpunit.xml file
50+
pointing to the PHPCS directory.
51+
';
52+
53+
die(1);
54+
}
55+
56+
// Clean up.
57+
unset($ds, $phpcsDir, $composerPHPCSPath);

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
5+
backupGlobals="true"
6+
bootstrap="./phpunit-bootstrap.php"
7+
beStrictAboutTestsThatDoNotTestAnything="false"
8+
colors="true"
9+
forceCoversAnnotation="true">
10+
11+
<testsuites>
12+
<testsuite name="PHPCSDevTools">
13+
<directory suffix="UnitTest.php">./Debug/Tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
17+
<php>
18+
<!-- This line prevents issues with PHPCS trying to load sniff files for
19+
standards which we aren't testing.
20+
Ref: https://github.com/squizlabs/PHP_CodeSniffer/pull/1146 -->
21+
<env name="PHPCS_IGNORE_TESTS" value="PHPCompatibility"/>
22+
</php>
23+
</phpunit>
24+

0 commit comments

Comments
 (0)