diff --git a/tests/controller/controller_test.php b/tests/controller/controller_test.php index ddc1921b0e..37df094d40 100644 --- a/tests/controller/controller_test.php +++ b/tests/controller/controller_test.php @@ -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'), ); } diff --git a/tests/functional/feed_test.php b/tests/functional/feed_test.php index 9dde6ffcb0..2f3ae4e39a 100644 --- a/tests/functional/feed_test.php +++ b/tests/functional/feed_test.php @@ -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(); } diff --git a/tests/functional/ucp_allow_pm_test.php b/tests/functional/ucp_allow_pm_test.php index 2d41296ddf..01418b25b1 100644 --- a/tests/functional/ucp_allow_pm_test.php +++ b/tests/functional/ucp_allow_pm_test.php @@ -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 diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index 04324e0df1..ecfc948793 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -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 = []; } /** @@ -353,4 +367,56 @@ abstract class phpbb_database_test_case extends TestCase $this->assertTrue(true); } } + + /** + * 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); + } + } } diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 10e444dee5..f21e49b65b 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -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() diff --git a/tests/test_framework/phpbb_test_case.php b/tests/test_framework/phpbb_test_case.php index 8e09f17ede..cb48de23d4 100644 --- a/tests/test_framework/phpbb_test_case.php +++ b/tests/test_framework/phpbb_test_case.php @@ -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); + } + } }