Skip to content

Commit f91bda8

Browse files
committed
Tests: set up mechanism to allow for testing CBF specific code
See the description of how this works in the `CONTRIBUTING` file for more information. Includes adjustments to the GH Actions workflows to ensure the CBF specific tests are: * Always run for the `quicktest` and "normal" test runs. * Run for code coverage and that the code coverage reports are send in a way that they can be merged correctly. Includes adding a "requires CS mode" condition to a few tests which would otherwise fail in CBF mode.
1 parent 45316c6 commit f91bda8

File tree

8 files changed

+147
-7
lines changed

8 files changed

+147
-7
lines changed

.github/CONTRIBUTING.md

+30
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,36 @@ However, there are a few places which include OS-specific conditions, most notab
346346

347347
Tests which cover code which have Windows specific conditions should be marked with a `@group Windows` annotation to allow for running those tests separately/selectively in CI.
348348

349+
#### Tests covering code which has CS/CBF specific behaviour
350+
351+
There are a few places in PHPCS where code uses a global `PHP_CODESNIFFER_CBF` constant to determine what to do.
352+
This makes testing this code more complicated.
353+
354+
Tests which will only work correctly when `PHP_CODESNIFFER_CBF === false` should get the following test skip condition at the top of the test method:
355+
```php
356+
if (PHP_CODESNIFFER_CBF === true) {
357+
$this->markTestSkipped('This test needs CS mode to run');
358+
}
359+
```
360+
361+
Tests which are specifically intended to cover code run when `PHP_CODESNIFFER_CBF === true` should:
362+
1. Be annotated with `@group CBF`.
363+
2. Have a test skip condition at the top of the test method like so:
364+
```php
365+
if (PHP_CODESNIFFER_CBF === false) {
366+
$this->markTestSkipped('This test needs CBF mode to run');
367+
}
368+
```
369+
370+
By default, the tests are run with the `PHP_CODESNIFFER_CBF` constant set to `false` and tests in the `@group CBF` will not be run.
371+
372+
To run the tests specific to the use of `PHP_CODESNIFFER_CBF === true`:
373+
1. Set `<php><env name="PHP_CODESNIFFER_CBF" value="1"/></php>` in a `phpunit.xml` file or set the ENV variable on an OS-level.
374+
2. Run the tests like so:
375+
```bash
376+
vendor/bin/phpunit --group CBF --exclude-group nothing
377+
```
378+
349379

350380
### Submitting Your Pull Request
351381

.github/workflows/quicktest.yml

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ jobs:
6666
if: ${{ matrix.os == 'windows-latest' }}
6767
run: php "vendor/bin/phpunit" --group Windows --no-coverage
6868

69+
- name: 'PHPUnit: run select tests in CBF mode'
70+
run: php "vendor/bin/phpunit" tests/AllTests.php --group CBF --exclude-group nothing --no-coverage
71+
env:
72+
PHP_CODESNIFFER_CBF: '1'
73+
6974
# Note: The code style check is run as an integration test.
7075
- name: 'PHPCS: check code style without cache, no parallel'
7176
run: php "bin/phpcs" --no-cache --parallel=1

.github/workflows/test.yml

+31-1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ jobs:
155155
if: ${{ matrix.skip_tests != true }}
156156
run: php "vendor/bin/phpunit" --no-coverage
157157

158+
- name: 'PHPUnit: run select tests in CBF mode'
159+
run: php "vendor/bin/phpunit" tests/AllTests.php --group CBF --exclude-group nothing --no-coverage
160+
env:
161+
PHP_CODESNIFFER_CBF: '1'
162+
158163
- name: 'PHPCS: check code style without cache, no parallel'
159164
if: ${{ matrix.custom_ini == false && matrix.php != '7.4' }}
160165
run: php "bin/phpcs" --no-cache --parallel=1
@@ -257,6 +262,22 @@ jobs:
257262
if: ${{ matrix.os != 'windows-latest' && steps.phpunit_version.outputs.VERSION >= '9.3' }}
258263
run: php "vendor/bin/phpunit" --coverage-cache ./build/phpunit-cache
259264

265+
- name: "Run select tests in CBF mode with code coverage (PHPUnit < 9.3)"
266+
if: ${{ matrix.os != 'windows-latest' && steps.phpunit_version.outputs.VERSION < '9.3' }}
267+
run: >
268+
php "vendor/bin/phpunit" tests/AllTests.php
269+
--group CBF --exclude-group nothing --coverage-clover build/logs/clover-cbf.xml
270+
env:
271+
PHP_CODESNIFFER_CBF: '1'
272+
273+
- name: "Run select tests in CBF mode with code coverage (PHPUnit 9.3+)"
274+
if: ${{ matrix.os != 'windows-latest' && steps.phpunit_version.outputs.VERSION >= '9.3' }}
275+
run: >
276+
php "vendor/bin/phpunit" tests/AllTests.php --coverage-cache ./build/phpunit-cache
277+
--group CBF --exclude-group nothing --coverage-clover build/logs/clover-cbf.xml
278+
env:
279+
PHP_CODESNIFFER_CBF: '1'
280+
260281
- name: "Run the unit tests which may have different outcomes on Windows with code coverage (PHPUnit < 9.3)"
261282
if: ${{ matrix.os == 'windows-latest' && steps.phpunit_version.outputs.VERSION < '9.3' }}
262283
run: php "vendor/bin/phpunit" --group Windows
@@ -265,7 +286,7 @@ jobs:
265286
if: ${{ matrix.os == 'windows-latest' && steps.phpunit_version.outputs.VERSION >= '9.3' }}
266287
run: php "vendor/bin/phpunit" --group Windows --coverage-cache ./build/phpunit-cache
267288

268-
- name: Upload coverage results to Coveralls
289+
- name: "Upload coverage results to Coveralls (normal run)"
269290
if: ${{ success() }}
270291
uses: coverallsapp/github-action@v2
271292
with:
@@ -274,6 +295,15 @@ jobs:
274295
flag-name: os-${{ matrix.os }}-php-${{ matrix.php }}-custom-ini-${{ matrix.custom_ini }}
275296
parallel: true
276297

298+
- name: "Upload coverage results to Coveralls (CBF run)"
299+
if: ${{ matrix.os != 'windows-latest' && success() }}
300+
uses: coverallsapp/github-action@v2
301+
with:
302+
format: clover
303+
file: build/logs/clover-cbf.xml
304+
flag-name: cbf-os-${{ matrix.os }}-ubuntu-latest-php-${{ matrix.php }}-custom-ini-${{ matrix.custom_ini }}
305+
parallel: true
306+
277307
coveralls-finish:
278308
needs: coverage
279309
if: always() && needs.coverage.result == 'success'

phpunit.xml.dist

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
</testsuite>
1919
</testsuites>
2020

21+
<groups>
22+
<exclude>
23+
<group>CBF</group>
24+
</exclude>
25+
</groups>
26+
2127
<filter>
2228
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="false">
2329
<directory suffix=".php">./src</directory>
@@ -32,4 +38,8 @@
3238
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/>
3339
<log type="coverage-clover" target="build/logs/clover.xml"/>
3440
</logging>
41+
42+
<php>
43+
<env name="PHP_CODESNIFFER_CBF" value="0"/>
44+
</php>
3545
</phpunit>

tests/Core/Generators/GeneratorTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ public static function dataGeneratingDocs()
201201
*/
202202
public function testGeneratorWillShowEachStandardSeparately()
203203
{
204+
if (PHP_CODESNIFFER_CBF === true) {
205+
$this->markTestSkipped('This test needs CS mode to run');
206+
}
207+
204208
$standard = __DIR__.'/OneDocTest.xml';
205209
$_SERVER['argv'] = [
206210
'phpcs',

tests/Core/Ruleset/ExplainTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ public function testExplainWithDeprecatedSniffs()
217217
*/
218218
public function testExplainWillExplainEachStandardSeparately()
219219
{
220+
if (PHP_CODESNIFFER_CBF === true) {
221+
$this->markTestSkipped('This test needs CS mode to run');
222+
}
223+
220224
$standard = __DIR__.'/ExplainSingleSniffTest.xml';
221225
$_SERVER['argv'] = [
222226
'phpcs',

tests/Core/Ruleset/ShowSniffDeprecationsTest.php

+43-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function dataHasSniffDeprecations()
6868

6969

7070
/**
71-
* Test that the listing with deprecated sniffs will not show when specific command-line options are being used.
71+
* Test that the listing with deprecated sniffs will not show when specific command-line options are being used [1].
7272
*
7373
* @param string $standard The standard to use for the test.
7474
* @param array<string> $additionalArgs Optional. Additional arguments to pass.
@@ -103,24 +103,62 @@ public function testDeprecatedSniffsListDoesNotShow($standard, $additionalArgs=[
103103
public static function dataDeprecatedSniffsListDoesNotShow()
104104
{
105105
return [
106-
'Standard not using deprecated sniffs: PSR1' => [
106+
'Standard not using deprecated sniffs: PSR1' => [
107107
'standard' => 'PSR1',
108108
],
109-
'Standard using deprecated sniffs; explain mode' => [
109+
'Standard using deprecated sniffs; explain mode' => [
110110
'standard' => __DIR__.'/ShowSniffDeprecationsTest.xml',
111111
'additionalArgs' => ['-e'],
112112
],
113-
'Standard using deprecated sniffs; quiet mode' => [
113+
'Standard using deprecated sniffs; quiet mode' => [
114114
'standard' => __DIR__.'/ShowSniffDeprecationsTest.xml',
115115
'additionalArgs' => ['-q'],
116116
],
117+
];
118+
119+
}//end dataDeprecatedSniffsListDoesNotShow()
120+
121+
122+
/**
123+
* Test that the listing with deprecated sniffs will not show when specific command-line options are being used [2].
124+
*
125+
* {@internal Separate test method for the same thing as this test will only work in CS mode.}
126+
*
127+
* @param string $standard The standard to use for the test.
128+
* @param array<string> $additionalArgs Optional. Additional arguments to pass.
129+
*
130+
* @dataProvider dataDeprecatedSniffsListDoesNotShowNeedsCsMode
131+
*
132+
* @return void
133+
*/
134+
public function testDeprecatedSniffsListDoesNotShowNeedsCsMode($standard, $additionalArgs=[])
135+
{
136+
if (PHP_CODESNIFFER_CBF === true) {
137+
$this->markTestSkipped('This test needs CS mode to run');
138+
}
139+
140+
$this->testDeprecatedSniffsListDoesNotShow($standard, $additionalArgs);
141+
142+
}//end testDeprecatedSniffsListDoesNotShowNeedsCsMode()
143+
144+
145+
/**
146+
* Data provider.
147+
*
148+
* @see testDeprecatedSniffsListDoesNotShowNeedsCsMode()
149+
*
150+
* @return array<string, array<string, string|array<string>>>
151+
*/
152+
public static function dataDeprecatedSniffsListDoesNotShowNeedsCsMode()
153+
{
154+
return [
117155
'Standard using deprecated sniffs; documentation is requested' => [
118156
'standard' => __DIR__.'/ShowSniffDeprecationsTest.xml',
119157
'additionalArgs' => ['--generator=text'],
120158
],
121159
];
122160

123-
}//end dataDeprecatedSniffsListDoesNotShow()
161+
}//end dataDeprecatedSniffsListDoesNotShowNeedsCsMode()
124162

125163

126164
/**

tests/bootstrap.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,27 @@
1111
define('PHP_CODESNIFFER_IN_TESTS', true);
1212
}
1313

14+
/*
15+
* Determine whether the test suite should be run in CBF mode.
16+
*
17+
* Use `<php><env name="PHP_CODESNIFFER_CBF" value="1"/></php>` in a `phpunit.xml` file
18+
* or set the ENV variable at an OS-level to enable CBF mode.
19+
*
20+
* To run the CBF specific tests, use the following command:
21+
* vendor/bin/phpunit --group CBF --exclude-group nothing
22+
*
23+
* If the ENV variable has not been set, or is set to "false", the tests will run in CS mode.
24+
*/
25+
1426
if (defined('PHP_CODESNIFFER_CBF') === false) {
15-
define('PHP_CODESNIFFER_CBF', false);
27+
$cbfMode = getenv('PHP_CODESNIFFER_CBF');
28+
if ($cbfMode === '1') {
29+
define('PHP_CODESNIFFER_CBF', true);
30+
echo 'Note: Tests are running in "CBF" mode'.PHP_EOL.PHP_EOL;
31+
} else {
32+
define('PHP_CODESNIFFER_CBF', false);
33+
echo 'Note: Tests are running in "CS" mode'.PHP_EOL.PHP_EOL;
34+
}
1635
}
1736

1837
if (defined('PHP_CODESNIFFER_VERBOSITY') === false) {

0 commit comments

Comments
 (0)