1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-01-29 20:49:48 +01:00

[ticket/16549] Avoid deprecated properties/methods in newer PHPUnit versions

PHPBB3-16549
This commit is contained in:
rxu 2020-07-13 22:23:55 +07:00
parent fa9bfce4f6
commit 3444e8816b
No known key found for this signature in database
GPG Key ID: 955F0567380E586A
6 changed files with 190 additions and 29 deletions

View File

@ -107,7 +107,10 @@ class phpbb_controller_controller_test extends phpbb_test_case
array(new foo\controller(), array(), array()),
array(array(new foo\controller(), 'handle_fail'), array(), array(), '\phpbb\controller\exception', 'CONTROLLER_ARGUMENT_VALUE_MISSING'),
array('', array(), array(), '\ReflectionException', 'Function () does not exist'),
array(new phpbb\controller\foo, array(), array(), '\ReflectionException', 'Method __invoke does not exist'),
// Before PHP 8: 'Method __invoke does not exist'
// As of PHP 8: 'Method phpbb\controller\foo::__invoke() does not exist'
array(new phpbb\controller\foo, array(), array(), '\ReflectionException',
'Method ' . (version_compare(PHP_VERSION, '8', '>=') ? 'phpbb\controller\foo::__invoke()' : '__invoke') . ' does not exist'),
);
}

View File

@ -30,7 +30,9 @@ class phpbb_functional_feed_test extends phpbb_functional_test_case
{
parent::__construct($name, $data, $dataName);
$this->backupStaticAttributesBlacklist['phpbb_functional_feed_test'] = array('init_values');
$this->excludeBackupStaticAttributes([
'phpbb_functional_feed_test' => ['init_values'],
]);
$this->purge_cache();
}

View File

@ -22,9 +22,9 @@ class phpbb_functional_ucp_allow_pm_test extends phpbb_functional_test_case
{
parent::__construct();
$this->backupStaticAttributesBlacklist += array(
'phpbb_functional_ucp_allow_pm_test' => array('data'),
);
$this->excludeBackupStaticAttributes([
'phpbb_functional_ucp_allow_pm_test' => ['data'],
]);
}
// user A sends a PM to user B where B accepts PM

View File

@ -29,21 +29,35 @@ abstract class phpbb_database_test_case extends TestCase
static protected $install_schema_file;
public function __construct($name = NULL, array $data = array(), $dataName = '')
static protected $phpunit_version;
public function __construct($name = NULL, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->backupStaticAttributesBlacklist += array(
'SebastianBergmann\CodeCoverage\CodeCoverage' => array('instance'),
'SebastianBergmann\CodeCoverage\Filter' => array('instance'),
'SebastianBergmann\CodeCoverage\Util' => array('ignoredLines', 'templateMethods'),
'SebastianBergmann\Timer\Timer' => array('startTimes',),
'PHP_Token_Stream' => array('customTokens'),
'PHP_Token_Stream_CachingFactory' => array('cache'),
'phpbb_database_test_case' => array('already_connected'),
);
self::$phpunit_version = PHPUnit\Runner\Version::id();
$this->db_connections = array();
$backupStaticAttributesBlacklist = [
'SebastianBergmann\CodeCoverage\CodeCoverage' => ['instance'],
'SebastianBergmann\CodeCoverage\Filter' => ['instance'],
'SebastianBergmann\CodeCoverage\Util' => ['ignoredLines', 'templateMethods'],
'SebastianBergmann\Timer\Timer' => ['startTimes'],
'PHP_Token_Stream' => ['customTokens'],
'PHP_Token_Stream_CachingFactory' => ['cache'],
'phpbb_database_test_case' => ['already_connected'],
];
if (version_compare(self::$phpunit_version, '9.0', '>='))
{
$this->backupStaticAttributesExcludeList += $backupStaticAttributesBlacklist;
}
else
{
$this->backupStaticAttributesBlacklist += $backupStaticAttributesBlacklist;
}
$this->db_connections = [];
}
/**
@ -371,4 +385,56 @@ abstract class phpbb_database_test_case extends TestCase
return $core_tables;
}
/**
* PHPUnit deprecates several methods and properties in its recent versions
* Provide BC layer to be able to test in multiple environment settings
*/
public function expectException(string $exception): void
{
if (version_compare(self::$phpunit_version, '9.0', '>='))
{
switch ($exception) {
case PHPUnit\Framework\Error\Deprecated::class:
parent::expectDeprecation();
break;
case PHPUnit\Framework\Error\Error::class:
parent::expectError();
break;
case PHPUnit\Framework\Error\Notice::class:
parent::expectNotice();
break;
case PHPUnit\Framework\Error\Warning::class:
parent::expectWarning();
break;
default:
parent::expectException($exception);
break;
}
}
else
{
parent::expectException($exception);
}
}
/**
* PHPUnit deprecates several methods and properties in its recent versions
* Provide BC layer to be able to test in multiple environment settings
*/
public static function assertFileNotExists(string $filename, string $message = ''): void
{
if (version_compare(self::$phpunit_version, '9.0', '>='))
{
parent::assertFileDoesNotExist($filename, $message);
}
else
{
parent::assertFileNotExists($filename, $message);
}
}
}

View File

@ -183,13 +183,14 @@ class phpbb_functional_test_case extends phpbb_test_case
{
}
public function __construct($name = NULL, array $data = array(), $dataName = '')
public function __construct($name = NULL, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->backupStaticAttributesBlacklist += array(
'phpbb_functional_test_case' => array('config', 'already_installed'),
);
$backupStaticAttributesBlacklist = [
'phpbb_functional_test_case' => ['config', 'already_installed'],
];
$this->excludeBackupStaticAttributes($backupStaticAttributesBlacklist);
}
protected function get_db()

View File

@ -16,20 +16,25 @@ use PHPUnit\Framework\TestCase;
class phpbb_test_case extends TestCase
{
protected $test_case_helpers;
static protected $phpunit_version;
public function __construct($name = NULL, array $data = array(), $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->backupStaticAttributesBlacklist += array(
'SebastianBergmann\CodeCoverage\CodeCoverage' => array('instance'),
'SebastianBergmann\CodeCoverage\Filter' => array('instance'),
'SebastianBergmann\CodeCoverage\Util' => array('ignoredLines', 'templateMethods'),
'SebastianBergmann\Timer\Timer' => array('startTimes',),
'PHP_Token_Stream' => array('customTokens'),
'PHP_Token_Stream_CachingFactory' => array('cache'),
'phpbb_database_test_case' => array('already_connected', 'last_post_timestamp'),
);
self::$phpunit_version = PHPUnit\Runner\Version::id();
$backupStaticAttributesBlacklist = [
'SebastianBergmann\CodeCoverage\CodeCoverage' => ['instance'],
'SebastianBergmann\CodeCoverage\Filter' => ['instance'],
'SebastianBergmann\CodeCoverage\Util' => ['ignoredLines', 'templateMethods'],
'SebastianBergmann\Timer\Timer' => ['startTimes'],
'PHP_Token_Stream' => ['customTokens'],
'PHP_Token_Stream_CachingFactory' => ['cache'],
'phpbb_database_test_case' => ['already_connected', 'last_post_timestamp'],
];
$this->excludeBackupStaticAttributes($backupStaticAttributesBlacklist);
}
public function get_test_case_helpers()
@ -46,4 +51,88 @@ class phpbb_test_case extends TestCase
{
$this->get_test_case_helpers()->setExpectedTriggerError($errno, $message);
}
/**
* PHPUnit deprecates several methods and properties in its recent versions
* Provide BC layer to be able to test in multiple environment settings
*/
public function excludeBackupStaticAttributes($attributes_array)
{
if (version_compare(self::$phpunit_version, '9.0', '>='))
{
$this->backupStaticAttributesExcludeList += $attributes_array;
}
else
{
$this->backupStaticAttributesBlacklist += $attributes_array;
}
}
/**
* PHPUnit deprecates several methods and properties in its recent versions
* Provide BC layer to be able to test in multiple environment settings
*/
public static function assertRegExp(string $pattern, string $string, string $message = ''): void
{
if (version_compare(self::$phpunit_version, '9.0', '>='))
{
parent::assertMatchesRegularExpression($pattern, $string, $message);
}
else
{
parent::assertRegExp($pattern, $string, $message);
}
}
/**
* PHPUnit deprecates several methods and properties in its recent versions
* Provide BC layer to be able to test in multiple environment settings
*/
public function expectException(string $exception): void
{
if (version_compare(self::$phpunit_version, '9.0', '>='))
{
switch ($exception) {
case PHPUnit\Framework\Error\Deprecated::class:
parent::expectDeprecation();
break;
case PHPUnit\Framework\Error\Error::class:
parent::expectError();
break;
case PHPUnit\Framework\Error\Notice::class:
parent::expectNotice();
break;
case PHPUnit\Framework\Error\Warning::class:
parent::expectWarning();
break;
default:
parent::expectException($exception);
break;
}
}
else
{
parent::expectException($exception);
}
}
/**
* PHPUnit deprecates several methods and properties in its recent versions
* Provide BC layer to be able to test in multiple environment settings
*/
public static function assertFileNotExists(string $filename, string $message = ''): void
{
if (version_compare(self::$phpunit_version, '9.0', '>='))
{
parent::assertFileDoesNotExist($filename, $message);
}
else
{
parent::assertFileNotExists($filename, $message);
}
}
}