From af16eb9eca64f88a92932fc45354e243831f2ba1 Mon Sep 17 00:00:00 2001 From: Pieter Oliver Date: Mon, 27 Jan 2025 17:53:30 +0000 Subject: [PATCH 1/2] Add in phpunit 11 support Credited to dmaicher Co-authored-by: dmaicher --- composer.json | 2 +- src/Constraint/ArraySubset.php | 59 +++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 52ad9dc..f5c034d 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "This package provides ArraySubset and related asserts once deprecated in PHPUnit 8", "type": "library", "require": { - "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0", + "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0", "php": "^5.4 || ^7.0 || ^8.0" }, "license": "MIT", diff --git a/src/Constraint/ArraySubset.php b/src/Constraint/ArraySubset.php index 4e04156..6a59899 100644 --- a/src/Constraint/ArraySubset.php +++ b/src/Constraint/ArraySubset.php @@ -8,8 +8,12 @@ use ArrayObject; use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\ExpectationFailedException; -use PHPUnit\SebastianBergmann\Comparator\ComparisonFailure as Phar_ComparisonFailure; +use PHPUnit\SebastianBergmann\Comparator\ComparisonFailure as ComparisonFailure_In_Phar_Old; +use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old; +use PHPUnitPHAR\SebastianBergmann\Comparator\ComparisonFailure as ComparisonFailure_In_Phar; +use PHPUnitPHAR\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar; use SebastianBergmann\Comparator\ComparisonFailure; +use SebastianBergmann\Exporter\Exporter; use SebastianBergmann\RecursionContext\InvalidArgumentException; use Traversable; @@ -84,12 +88,7 @@ public function evaluate($other, string $description = '', bool $returnResult = return null; } - // Support use of this library when running PHPUnit as a Phar. - if (class_exists(Phar_ComparisonFailure::class) === true) { - $class = Phar_ComparisonFailure::class; - } else { - $class = ComparisonFailure::class; - } + $class = self::getPHPUnitComparisonFailure(); $f = new $class( $patched, @@ -107,7 +106,51 @@ public function evaluate($other, string $description = '', bool $returnResult = */ public function toString(): string { - return 'has the subset ' . $this->exporter()->export($this->subset); + $exporter = self::getPHPUnitExporterObject(); + + return 'has the subset ' . $exporter->export($this->subset); + } + + /** + * Helper function to obtain an instance of the Exporter class. + * + * @return SebastianBergmann\Exporter\Exporter|PHPUnitPHAR\SebastianBergmann\Exporter\Exporter|PHPUnit\SebastianBergmann\Exporter\Exporter + */ + private static function getPHPUnitExporterObject() + { + if (class_exists('SebastianBergmann\Comparator\ComparisonFailure')) { + // Composer install or really old PHAR files. + return new Exporter(); + } + + if (class_exists('PHPUnitPHAR\SebastianBergmann\Comparator\ComparisonFailure')) { + // PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+. + return new Exporter_In_Phar(); + } + + // PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10. + return new Exporter_In_Phar_Old(); + } + + /** + * Helper function to obtain the class name of the ComparisonFailure class. + * + * @return string; + */ + private static function getPHPUnitComparisonFailure() + { + if (class_exists('SebastianBergmann\Exporter\Exporter')) { + // Composer install or really old PHAR files. + return ComparisonFailure::class; + } + + if (class_exists('PHPUnitPHAR\SebastianBergmann\Exporter\Exporter')) { + // PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+. + return ComparisonFailure_In_Phar::class; + } + + // PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10. + return ComparisonFailure_In_Phar_Old::class; } /** From 6b1adb89020ef2541b8d34588fe49d2cafb40be1 Mon Sep 17 00:00:00 2001 From: Pieter Oliver Date: Mon, 27 Jan 2025 18:01:55 +0000 Subject: [PATCH 2/2] Add github actions which should get the suite running using phar's so this version works with most/all phpunit installations Credit to jrfnl for the suggestion and the phar test setup from Yoast Co-authored-by: jrfnl --- .github/workflows/run-tests.yml | 138 +++++++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index f2a778a..ead66fc 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,4 +1,4 @@ -name: Unit Tests +name: Tests on: push: @@ -99,3 +99,139 @@ jobs: - name: Execute Unit Tests run: vendor/bin/phpunit + + test-phar: + runs-on: ubuntu-latest + + strategy: + matrix: + include: + # Test against the high/low supported PHP-PHPUnit combinations. + + # PHPUnit 4 is only supported at the latest version. + - php: '5.4' + phpunit: '4' + - php: '5.5' + phpunit: '4' + + # PHPUnit 5 is only supported for PHPUnit 5.7.21-latest. + - php: '5.6' + phpunit: '5.7.21' + - php: '5.6' + phpunit: '5' + - php: '7.1' + phpunit: '5.7.21' + + # PHPUnit 6 is fully supported for the officially supported PHP versions. + - php: '7.0' + phpunit: '6.4' + - php: '7.0' + phpunit: '6' + - php: '7.2' + phpunit: '6.4' + + # PHPUnit 7 is fully supported for the officially supported PHP versions. + # Caveats: + # - PHPUnit 7.0 seems to have an issue with something related to TestListeners, so using PHPUnit 7.1 instead for "low". + # - PHPUnit 7 supports PHP 7.3 as of PHPUnit 7.3.0 (for our purposes). + - php: '7.1' + phpunit: '7.1' + - php: '7.1' + phpunit: '7' + - php: '7.3' + phpunit: '7.3' + + # PHPUnit 8 is fully supported for the officially supported PHP versions. + # Caveats: + # - PHPUnit 8 supports PHP 8.0 as of PHPUnit 8.5.12 (for our purposes). + # - PHPUnit 8 supports PHP 8.1 as of PHPUnit 8.5.19 (for our purposes). + # - PHPUnit 8 supports PHP 8.2 as of PHPUnit 8.5.19 (for our purposes). + # - PHPUnit 8 supports PHP 8.3 as of PHPUnit 8.5.19 (for our purposes). + # - PHPUnit 8 supports PHP 8.4 as of PHPUnit 8.5.40 (for our purposes). + - php: '7.2' + phpunit: '8' + - php: '8.0' + phpunit: '8.5.12' + - php: '8.4' + phpunit: '8.5.40' + - php: '8.4' + phpunit: '8' + + # PHPUnit 9 is fully supported for the officially supported PHP versions. + # Caveats: + # - PHPUnit 9 supports PHP 8.0 as of PHPUnit 9.3.0 (for our purposes). + # - PHPUnit 9 supports PHP 8.1 as of PHPUnit 9.5.8 (for our purposes). + # - PHPUnit 9 supports PHP 8.2 as of PHPUnit 9.5.8 (for our purposes). + # - PHPUnit 9 supports PHP 8.3 as of PHPUnit 9.5.8 (for our purposes). + # - PHPUnit 9 supports PHP 8.4 as of PHPUnit 9.6.21 (for our purposes). + - php: '7.3' + phpunit: '9' + - php: '8.0' + phpunit: '9.3.0' + - php: '8.0' + phpunit: '9' + - php: '8.3' + phpunit: '9.5.8' + - php: '8.3' + phpunit: '9' + - php: '8.4' + phpunit: '9.6.21' + + # PHPUnit 10 is NOT supported in PHPUnit Polyfills 3.x. + + # PHPUnit 11 is fully supported for the officially supported PHP versions. + - php: '8.2' + phpunit: '11' + - php: '8.3' + phpunit: '11.0' + - php: '8.4' + phpunit: '11.3.6' + + # Experimental builds. + - php: 'nightly' + phpunit: '9' + - php: 'nightly' + phpunit: '11' + + name: "PHAR test: PHP ${{ matrix.php }} - PHPUnit: ${{matrix.phpunit}}" + + continue-on-error: ${{ matrix.php == '8.4' }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + ini-values: zend.assertions=1, error_reporting=-1, display_errors=On + tools: phpunit:${{ matrix.phpunit }} + env: + fail-fast: true + + # Remove PHPUnit from the Composer install as we want to be sure the PHAR file is used. + - name: 'Composer: remove PHPUnit' + run: composer remove phpunit/phpunit --no-update --no-interaction + + # Install dependencies and handle caching in one go. + # @link https://github.com/marketplace/actions/install-php-dependencies-with-composer + - name: Install Composer dependencies - normal + if: matrix.php < '8.3' + uses: "ramsey/composer-install@v3" + with: + composer-options: "--no-dev" + # Bust the cache at least once a month - output format: YYYY-MM. + custom-cache-suffix: $(date -u "+%Y-%m") + + - name: Install Composer dependencies - ignore PHP restrictions + if: matrix.php >= '8.3' + uses: "ramsey/composer-install@v3" + with: + composer-options: "--no-dev --ignore-platform-req=php+" + # Bust the cache at least once a month - output format: YYYY-MM. + custom-cache-suffix: $(date -u "+%Y-%m") + + - name: Run the unit tests + if: ${{ ! matrix.coverage }} + run: phpunit --no-coverage