Skip to content

Commit 283db08

Browse files
committed
tests: alias phpcs 3.x test case class to the new 4.x name.
- Added `phpunit-boostrap.php` to load phpcs bootstrap and autoload files from vendor and alias the phpcs 3.x test case class to the new 4.x name. This should fix the ci unit test error of the `AbstractSniffTestCase` class not being found while using phpcs 3.x
1 parent 590ff36 commit 283db08

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

phpunit-bootstrap.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* Load the necessary PHPCS files.
5+
*/
6+
// Get the PHPCS dir from an environment variable.
7+
$phpcsDir = \getenv('PHPCS_DIR');
8+
$composerPHPCSPath = __DIR__ . '/vendor/squizlabs/php_codesniffer';
9+
10+
if ($phpcsDir === false && \is_dir($composerPHPCSPath)) {
11+
// PHPCS installed via Composer.
12+
$phpcsDir = $composerPHPCSPath;
13+
}
14+
elseif ($phpcsDir !== false) {
15+
/*
16+
* PHPCS in a custom directory.
17+
* For this to work, the `PHPCS_DIR` needs to be set in a custom `phpunit.xml` file.
18+
*/
19+
$phpcsDir = \realpath($phpcsDir);
20+
}
21+
22+
// Try and load the PHPCS autoloader.
23+
if ($phpcsDir !== false
24+
&& \file_exists($phpcsDir . '/autoload.php')
25+
&& \file_exists($phpcsDir . '/tests/bootstrap.php')
26+
) {
27+
require_once $phpcsDir . '/autoload.php';
28+
require_once $phpcsDir . '/tests/bootstrap.php'; // PHPUnit 6.x+ support.
29+
} else {
30+
echo 'Uh oh... can\'t find PHPCS.
31+
32+
If you use Composer, please run `composer install`.
33+
Otherwise, make sure you set a `PHPCS_DIR` environment variable in your phpunit.xml file
34+
pointing to the PHPCS directory and that PHPCSUtils is included in the `installed_paths`
35+
for that PHPCS install.
36+
';
37+
38+
exit(1);
39+
}
40+
41+
// Alias the PHPCS 3.x test case to the PHPCS 4.x name.
42+
if (class_exists('PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest') === true
43+
&& class_exists('PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase') === false
44+
) {
45+
class_alias(
46+
'PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest',
47+
'PHP_CodeSniffer\Tests\Standards\AbstractSniffTestCase'
48+
);
49+
}

phpunit-lte9.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
backupGlobals="true"
66
beStrictAboutOutputDuringTests="true"
77
beStrictAboutTestsThatDoNotTestAnything="false"
8-
bootstrap="./vendor/squizlabs/php_codesniffer/tests/bootstrap.php"
8+
bootstrap="./phpunit-bootstrap.php"
99
convertErrorsToExceptions="true"
1010
convertWarningsToExceptions="true"
1111
convertNoticesToExceptions="true"

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
backupGlobals="true"
66
beStrictAboutOutputDuringTests="true"
77
beStrictAboutTestsThatDoNotTestAnything="false"
8-
bootstrap="./vendor/squizlabs/php_codesniffer/tests/bootstrap.php"
8+
bootstrap="./phpunit-bootstrap.php"
99
cacheDirectory=".phpunit.cache"
1010
displayDetailsOnTestsThatTriggerErrors="true"
1111
displayDetailsOnTestsThatTriggerWarnings="false"

0 commit comments

Comments
 (0)