Skip to content
Snippets Groups Projects
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
Branches
Tags
No related merge requests found
......@@ -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:
......
......@@ -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"
}
}
......@@ -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.
......
......@@ -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()
/**
......
<?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
<?php
require 'vendor/autoload.php';
require 'tests/Drupal/PhpunitCompatibilityTestCase.php';
require 'tests/Drupal/CoderSniffUnitTest.php';
require 'vendor/squizlabs/php_codesniffer/autoload.php';
......@@ -2,13 +2,13 @@
namespace DrupalPractice\Test\ProjectDetection;
use Drupal\Test\PhpunitCompatibilityTestCase;
use DrupalPractice\Project;
use PHPUnit\Framework\TestCase;
/**
* Tests that project and version detection works.
*/
class ProjectUnitTest extends TestCase
class ProjectUnitTest extends PhpunitCompatibilityTestCase
{
/**
......@@ -24,14 +24,13 @@ class ProjectUnitTest extends TestCase
*
* @return void
*/
public function setUp()
protected function compatibleSetUp()
{
parent::setUp();
$this->phpcsFile = $this->getMockBuilder('\PHP_CodeSniffer\Files\File')
->disableOriginalConstructor()
->getMock();
}//end setUp()
}//end compatibleSetUp()
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment