Unverified Commit 7d5af4fa authored by Klaus Purer's avatar Klaus Purer Committed by GitHub
Browse files

test(phpunit): Enable PHP 8 testing with a PHPUnit compatibility layer (#/3222962 by klausi)

parent d3286d57
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ jobs:
    strategy:
      fail-fast: false
      matrix:
        php-versions: ['7.1', '7.2', '7.3', '7.4']
        php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0']
        phpstan: ['1']
        # Extra run to also test on PHP 7.0 without PHPStan.
        include:
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^6.0 || ^7.0",
        "phpunit/phpunit": "^6.0 || ^7.0 || ^8.0",
        "phpstan/phpstan": "^0.12.63"
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@ parameters:
        - 'tests/*/good.php'
        - 'tests/*/bad.php'
        - 'tests/*/drupal[678]/*.php'
        # Compatibilty layer class for PHPUnit that should not be checked.
        - 'tests/Drupal/PhpunitCompatibilityTestCase.php'
    bootstrapFiles:
        - tests/Drupal/phpunit-bootstrap.php
        # PHPStan does not find the constants in this file, so load it manually.
+3 −4
Original line number Diff line number Diff line
@@ -19,9 +19,8 @@ use PHP_CodeSniffer\Files\LocalFile;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Tokens;
use PHPUnit\Framework\TestCase;

abstract class CoderSniffUnitTest extends TestCase
abstract class CoderSniffUnitTest extends PhpunitCompatibilityTestCase
{

    /**
@@ -60,7 +59,7 @@ abstract class CoderSniffUnitTest extends TestCase
     *
     * @return void
     */
    protected function setUp()
    protected function compatibleSetUp()
    {
        $class = get_class($this);

@@ -76,7 +75,7 @@ abstract class CoderSniffUnitTest extends TestCase
            define('PHP_CODESNIFFER_CBF', 0);
        }

    }//end setUp()
    }//end compatibleSetUp()


    /**
+76 −0
Original line number Diff line number Diff line
<?php
/**
 * Helper class to be compatible with different PHPUnit major versions.
 *
 * We want to support PHP 7.0 which does not have the "void" return type hint.
 * For PHP 8 we need a higher PHPUnit version that enforces the type hint. Our
 * solution is to define a compatibility class depending on PHP version. It
 * routes around the setup method with a compatibleSetUp() method that child
 * classes use.
 */

namespace Drupal\Test;

use PHPUnit\Framework\TestCase;

if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
    class PhpunitCompatibilityTestCase extends TestCase
    {


        /**
         * {@inheritdoc}
         *
         * @return void
         */
        protected function setUp(): void
        {
            $this->compatibleSetUp();

        }//end setUp()


        /**
         * Helper method that is used in place of ::setUp().
         *
         * @return void
         */
        protected function compatibleSetUp()
        {

        }//end compatibleSetUp()


    }//end class

} else {
    class PhpunitCompatibilityTestCase extends TestCase
    {


        /**
         * {@inheritdoc}
         *
         * @return void
         */
        protected function setUp()
        {
            $this->compatibleSetUp();

        }//end setUp()


        /**
         * Helper method that is used in place of ::setUp().
         *
         * @return void
         */
        protected function compatibleSetUp()
        {

        }//end compatibleSetUp()


    }//end class

}//end if
Loading