From 44358e503a0deff584fe6bb96c32783434007c24 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 13 Feb 2023 17:11:32 +0100 Subject: [PATCH] Resolve warnings in PHPUnit tests due to deprecation --- tests/unit/acp/move_module_test.php | 30 ++++++++++++++------ tests/unit/functions/check_file_src_test.php | 13 +++++---- tests/unit/modules/birthday_list_test.php | 1 + tests/unit/modules/calendar_test.php | 17 +++++++---- tests/unit/modules/welcome_test.php | 14 +++++++-- tests/unit/portal/modules_manager_test.php | 5 +++- 6 files changed, 58 insertions(+), 22 deletions(-) diff --git a/tests/unit/acp/move_module_test.php b/tests/unit/acp/move_module_test.php index 0990fe0b..08cf16dc 100644 --- a/tests/unit/acp/move_module_test.php +++ b/tests/unit/acp/move_module_test.php @@ -9,6 +9,8 @@ namespace board3\portal\portal\modules; +use PHPUnit\Framework\Exception; + class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\database_test_case { static public $redirected = false; @@ -238,8 +240,9 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data $this->modules_manager->move_module_vertical(2, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_UP); $this->assertTrue(self::$redirected); - $this->setExpectedTriggerError(E_USER_NOTICE, $this->language->lang('UNABLE_TO_MOVE_ROW')); self::$redirected = false; + $this->expectException(Exception::class); + $this->expectExceptionMessage($this->language->lang('UNABLE_TO_MOVE_ROW')); $this->modules_manager->move_module_vertical(2, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_UP); $this->assertFalse(self::$redirected); } @@ -250,8 +253,9 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data $this->modules_manager->move_module_vertical(3, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_DOWN); $this->assertTrue(self::$redirected); - $this->setExpectedTriggerError(E_USER_NOTICE, $this->language->lang('UNABLE_TO_MOVE_ROW')); self::$redirected = false; + $this->expectException(Exception::class); + $this->expectExceptionMessage($this->language->lang('UNABLE_TO_MOVE_ROW')); $this->modules_manager->move_module_vertical(3, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_DOWN); $this->assertFalse(self::$redirected); } @@ -272,7 +276,8 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data { if ($error) { - $this->setExpectedTriggerError(E_USER_NOTICE, $this->language->lang($error)); + $this->expectException(Exception::class); + $this->expectExceptionMessage($this->language->lang($error)); } else { @@ -306,7 +311,8 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data { if ($column_start > 3) { - $this->setExpectedTriggerError(E_USER_ERROR, 'CLASS_NOT_FOUND'); + $this->expectException(Exception::class); + $this->expectExceptionMessage('CLASS_NOT_FOUND'); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_RIGHT); return; } @@ -325,7 +331,9 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data $column_start++; $this->update_portal_modules(); } - $this->setExpectedTriggerError(E_USER_NOTICE, $this->language->lang('UNABLE_TO_MOVE')); + + $this->expectException(Exception::class); + $this->expectExceptionMessage($this->language->lang('UNABLE_TO_MOVE')); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_RIGHT); } @@ -348,7 +356,8 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data { if ($column_start > 3) { - $this->setExpectedTriggerError(E_USER_ERROR, 'CLASS_NOT_FOUND'); + $this->expectException(Exception::class); + $this->expectExceptionMessage('CLASS_NOT_FOUND'); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_LEFT); return; } @@ -378,7 +387,9 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data $this->update_portal_modules(); $column_start--; } - $this->setExpectedTriggerError(E_USER_NOTICE, $this->language->lang('UNABLE_TO_MOVE')); + + $this->expectException(Exception::class); + $this->expectExceptionMessage($this->language->lang('UNABLE_TO_MOVE')); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_LEFT); } @@ -422,7 +433,8 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data public function test_main_wrong_mode() { - $this->setExpectedTriggerError(E_USER_ERROR, 'NO_MODE'); + $this->expectException(Exception::class); + $this->expectExceptionMessage('NO_MODE'); $this->portal_module->main(5, 'foobar'); } @@ -461,7 +473,7 @@ function trigger_error($input, $type = E_USER_NOTICE) { if (!phpbb_acp_move_module_test::$override_trigger_error) { - \trigger_error($input, $type); + throw new Exception($input, $type); } phpbb_acp_move_module_test::$error = $input; phpbb_acp_move_module_test::$error_type = $type; diff --git a/tests/unit/functions/check_file_src_test.php b/tests/unit/functions/check_file_src_test.php index b7ca1ef8..00922734 100644 --- a/tests/unit/functions/check_file_src_test.php +++ b/tests/unit/functions/check_file_src_test.php @@ -7,11 +7,14 @@ * */ -require_once(dirname(__FILE__) . '/../../../../../../includes/functions_acp.php'); -require_once(dirname(__FILE__) . '/../../../../../../includes/functions.php'); +namespace board3\portal\tests\unit\functions; -class phpbb_functions_check_file_src_test extends \board3\portal\tests\testframework\database_test_case +use PHPUnit\Framework\Exception; + +class check_file_src_test extends \board3\portal\tests\testframework\database_test_case { + public static $redirected = false; + public function setUp(): void { global $phpbb_root_path, $portal_root_path; @@ -44,8 +47,8 @@ class phpbb_functions_check_file_src_test extends \board3\portal\tests\testframe { global $phpbb_dispatcher; - $phpbb_dispatcher = new phpbb_mock_event_dispatcher; - $this->setExpectedTriggerError(E_USER_WARNING); + $phpbb_dispatcher = new \phpbb_mock_event_dispatcher; + $this->expectException(Exception::class); $this->assertEquals(': styles/all/theme/images/portal/portal_foobar.png
', check_file_src('portal_foobar.png', '', 15)); } } diff --git a/tests/unit/modules/birthday_list_test.php b/tests/unit/modules/birthday_list_test.php index 8130646e..0968315c 100644 --- a/tests/unit/modules/birthday_list_test.php +++ b/tests/unit/modules/birthday_list_test.php @@ -46,6 +46,7 @@ class phpbb_unit_modules_birthday_list_test extends \board3\portal\tests\testfra ]); $this->user = new \phpbb\user($this->language, '\phpbb\datetime'); $this->user->timezone = new \DateTimeZone('UTC'); + $this->user->data['user_id'] = 2; $this->user->add_lang('common'); $user = $this->user; $this->birthday_list = new \board3\portal\modules\birthday_list($this->config, $this->template, $this->new_dbal(), $this->user); diff --git a/tests/unit/modules/calendar_test.php b/tests/unit/modules/calendar_test.php index 8b6a7059..0d89becf 100644 --- a/tests/unit/modules/calendar_test.php +++ b/tests/unit/modules/calendar_test.php @@ -9,6 +9,8 @@ namespace board3\portal\modules; +use PHPUnit\Framework\Exception; + require_once dirname(__FILE__) . '/../../mock/check_form_key.php'; require_once dirname(__FILE__) . '/../../../../../../includes/functions_compatibility.php'; @@ -357,12 +359,9 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor { if (!empty($expected_error_message)) { - $this->setExpectedTriggerError($expected_error, $expected_error_message); - } - else - { - $this->setExpectedTriggerError($expected_error); + $this->expectExceptionMessage($expected_error_message); } + $this->expectException(Exception::class); } $this->calendar->update_events('foobar', 5); @@ -373,3 +372,11 @@ function set_config($config_name, $config_value, $is_dynamic = false) { phpbb_unit_modules_calendar_test::$config->set($config_name, $config_value, !$is_dynamic); } + +if (!function_exists('trigger_error')) +{ + function trigger_error($input, $type = E_USER_NOTICE) + { + throw new Exception($input, $type); + } +} diff --git a/tests/unit/modules/welcome_test.php b/tests/unit/modules/welcome_test.php index 9c4fbf5e..fdeef016 100644 --- a/tests/unit/modules/welcome_test.php +++ b/tests/unit/modules/welcome_test.php @@ -9,6 +9,8 @@ namespace board3\portal\modules; +use PHPUnit\Framework\Exception; + require_once dirname(__FILE__) . '/../../mock/check_form_key.php'; class phpbb_unit_modules_welcome_test extends \board3\portal\tests\testframework\database_test_case @@ -197,7 +199,7 @@ class phpbb_unit_modules_welcome_test extends \board3\portal\tests\testframework $this->welcome = new \board3\portal\modules\welcome($this->config, $this->request, $this->template, $this->user, '', ''); $this->request->overwrite('submit', true, \phpbb\request\request_interface::POST); $this->request->overwrite('welcome_message', 'foobar101'); - $this->setExpectedTriggerError(E_USER_WARNING); + $this->expectException(Exception::class); $this->welcome->update_welcome('foobar', 5); } @@ -207,8 +209,16 @@ class phpbb_unit_modules_welcome_test extends \board3\portal\tests\testframework $this->welcome = new \board3\portal\modules\welcome($this->config, $this->request, $this->template, $this->user, '', ''); $this->request->overwrite('submit', true, \phpbb\request\request_interface::POST); $this->request->overwrite('welcome_message', ''); - $this->setExpectedTriggerError(version_compare(PHP_VERSION, '8', '>=') ? E_WARNING : E_USER_WARNING); + $this->expectException(Exception::class); check_form_key::$form_key_valid = true; $this->welcome->update_welcome('foobar', 5); } } + +if (!function_exists('trigger_error')) +{ + function trigger_error($input, $type = E_USER_NOTICE) + { + throw new Exception($input, $type); + } +} diff --git a/tests/unit/portal/modules_manager_test.php b/tests/unit/portal/modules_manager_test.php index 19c2ee10..9fe677ce 100644 --- a/tests/unit/portal/modules_manager_test.php +++ b/tests/unit/portal/modules_manager_test.php @@ -9,6 +9,8 @@ namespace board3\portal\portal\modules; +use PHPUnit\Framework\Exception; + class board3_portal_modules_manager_test extends \board3\portal\tests\testframework\database_test_case { protected $portal_columns; @@ -147,7 +149,8 @@ class board3_portal_modules_manager_test extends \board3\portal\tests\testframew public function test_get_horizontal_move_action() { - $this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE'); + $this->expectException(Exception::class); + $this->expectExceptionMessage('UNABLE_TO_MOVE'); $this->modules_manager->get_horizontal_move_action(array(), 6); }