diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cd8d13a..6f6f695 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,27 +1,13 @@ - - - ./ - - - ./language/ - ./migrations/ - ./tests/ - - + backupStaticProperties="false"> ./tests @@ -31,4 +17,16 @@ ./tests/functional/ + + + ./ + + + ./language/ + ./migrations/ + ./tests/ + + diff --git a/tests/unit/acp_module_test.php b/tests/unit/acp_module_test.php index 7e8866e..ebd3e3e 100644 --- a/tests/unit/acp_module_test.php +++ b/tests/unit/acp_module_test.php @@ -57,9 +57,9 @@ protected function setUp(): void $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); } - public function test_module_info() + public function test_module_info(): void { - self::assertEquals([ + $expected = [ '\\phpbb\\pwakit\\acp\\pwa_acp_module' => [ 'filename' => '\\phpbb\\pwakit\\acp\\pwa_acp_module', 'title' => 'ACP_PWA_KIT_TITLE', @@ -71,10 +71,15 @@ public function test_module_info() ], ], ], - ], $this->module_manager->get_module_infos('acp', 'pwa_acp_module')); + ]; + + $this->assertSame( + $expected, + $this->module_manager->get_module_infos('acp', 'pwa_acp_module') + ); } - public function module_auth_test_data(): array + public static function module_auth_test_data(): array { return [ 'invalid auth' => ['ext_foo/bar', false], @@ -85,12 +90,12 @@ public function module_auth_test_data(): array /** * @dataProvider module_auth_test_data */ - public function test_module_auth($module_auth, $expected) + public function test_module_auth(string $module_auth, bool $expected): void { - self::assertEquals($expected, p_master::module_auth($module_auth, 0)); + $this->assertEquals($expected, p_master::module_auth($module_auth, 0)); } - public function main_module_test_data(): array + public static function main_module_test_data(): array { return [ 'valid mode' => ['settings'], @@ -100,7 +105,7 @@ public function main_module_test_data(): array /** * @dataProvider main_module_test_data */ - public function test_main_module($mode) + public function test_main_module(string $mode): void { global $phpbb_container, $request, $template; @@ -123,13 +128,13 @@ public function test_main_module($mode) ->getMock(); $phpbb_container - ->expects(self::once()) + ->expects($this->once()) ->method('get') ->with('phpbb.pwakit.admin.controller') ->willReturn($admin_controller); $admin_controller - ->expects(self::once()) + ->expects($this->once()) ->method('main'); $p_master = new p_master(); @@ -137,4 +142,42 @@ public function test_main_module($mode) $p_master->module_ary[0]['url_extra'] = ''; $p_master->load('acp', pwa_acp_module::class, $mode); } + + public function test_main_module_with_missing_controller(): void + { + global $phpbb_container, $template, $request; + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Service not found: phpbb.pwakit.admin.controller'); + + if (!defined('IN_ADMIN')) { + define('IN_ADMIN', true); + } + + // Set up template mock + $template = $this->getMockBuilder(template::class) + ->disableOriginalConstructor() + ->getMock(); + + // Set up request mock + $request = $this->getMockBuilder(request::class) + ->disableOriginalConstructor() + ->getMock(); + + // Set up container mock + $phpbb_container = $this->getMockBuilder(ContainerInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $phpbb_container + ->expects($this->once()) + ->method('get') + ->with('phpbb.pwakit.admin.controller') + ->willThrowException(new \RuntimeException('Service not found: phpbb.pwakit.admin.controller')); + + $p_master = new p_master(); + $p_master->module_ary[0]['is_duplicate'] = 0; + $p_master->module_ary[0]['url_extra'] = ''; + $p_master->load('acp', pwa_acp_module::class, 'settings'); + } } diff --git a/tests/unit/admin_controller_test.php b/tests/unit/admin_controller_test.php index c4db19b..bcd2660 100644 --- a/tests/unit/admin_controller_test.php +++ b/tests/unit/admin_controller_test.php @@ -14,9 +14,6 @@ use phpbb\db\driver\driver_interface as dbal; use phpbb_mock_cache; use phpbb_mock_event_dispatcher; -use PHPUnit\DbUnit\DataSet\DefaultDataSet; -use PHPUnit\DbUnit\DataSet\IDataSet; -use PHPUnit\DbUnit\DataSet\XmlDataSet; use PHPUnit\Framework\MockObject\MockObject; use phpbb\config\config; use phpbb\exception\runtime_exception; @@ -42,13 +39,14 @@ class admin_controller_test extends phpbb_database_test_case protected helper $helper; protected upload $upload; protected string $phpbb_root_path; + protected admin_controller $admin_controller; protected static function setup_extensions(): array { return ['phpbb/pwakit']; } - protected function getDataSet(): IDataSet|XmlDataSet|DefaultDataSet + protected function getDataSet() { return $this->createXMLDataSet(__DIR__ . '/../fixtures/styles.xml'); } @@ -112,7 +110,7 @@ protected function setUp(): void ); } - public function module_access_test_data(): array + public static function module_access_test_data(): array { return [ 'correct mode' => ['settings', true], @@ -135,7 +133,7 @@ public function test_module_access($mode, $expected) $this->call_admin_controller($mode); } - public function form_checks_data(): array + public static function form_checks_data(): array { return [ 'submit test' => ['submit'], @@ -159,7 +157,7 @@ public function test_form_checks($action) $this->call_admin_controller(); } - public function display_settings_test_data(): array + public static function display_settings_test_data(): array { return [ 'site name and short name' => [ @@ -260,7 +258,7 @@ public function test_display_settings($configs, $expected) $this->call_admin_controller(); } - public function submit_test_data(): array + public static function submit_test_data(): array { return [ 'all good inputs' => [ @@ -443,7 +441,7 @@ public function test_resync() $this->call_admin_controller(); } - public function delete_test_data(): array + public static function delete_test_data(): array { return [ 'not confirmed' => [ diff --git a/tests/unit/event_listener_test.php b/tests/unit/event_listener_test.php index 930e7f2..d28aa2b 100644 --- a/tests/unit/event_listener_test.php +++ b/tests/unit/event_listener_test.php @@ -23,7 +23,7 @@ class event_listener_test extends phpbb_test_case { protected user|MockObject $user; protected template|MockObject $template; - protected helper $helper; + protected helper $pwa_helper; /** * Setup test environment @@ -32,8 +32,12 @@ protected function setUp(): void { parent::setUp(); - $this->user = $this->createMock(user::class); - $this->user->optionset('user_id', 2); + $this->user = $this->getMockBuilder(user::class) + ->disableOriginalConstructor() + ->getMock(); + $this->user->method('optionset') + ->with('user_id', 2) + ->willReturn(null); $this->user->data['user_id'] = 2; $this->user->style['pwa_bg_color'] = ''; $this->user->style['pwa_theme_color'] = ''; @@ -41,7 +45,7 @@ protected function setUp(): void $this->template = $this->getMockBuilder(template::class) ->getMock(); - $this->helper = $this->getMockBuilder(helper::class) + $this->pwa_helper = $this->getMockBuilder(helper::class) ->disableOriginalConstructor() ->getMock(); } @@ -54,7 +58,7 @@ protected function setUp(): void protected function get_listener(): main_listener { return new main_listener( - $this->helper, + $this->pwa_helper, $this->template, $this->user ); @@ -73,42 +77,44 @@ public function test_construct() */ public function test_getSubscribedEvents() { + $events = main_listener::getSubscribedEvents(); static::assertEquals([ - 'core.page_header', - 'core.modify_manifest', - ], array_keys(\phpbb\pwakit\event\main_listener::getSubscribedEvents())); + 'core.page_header' => 'header_updates', + 'core.modify_manifest' => 'manifest_updates', + ], $events); } - public function header_updates_test_data(): array + public static function header_updates_test_data(): array { return [ - 'header with data' => [ - [ - 'pwa_theme_color' => '#foobar', - 'pwa_bg_color' => '#barfoo', - ], + 'valid hex colors' => [ [ - [ - 'src' => 'images/site_icons/touch-icon-192.png', - 'sizes' => '192x192', - 'type' => 'image/png' - ], - [ - 'src' => 'images/site_icons/touch-icon-512.png', - 'sizes' => '512x512', - 'type' => 'image/png' - ], + 'pwa_theme_color' => '#ffffff', + 'pwa_bg_color' => '#000000', ], + self::getValidIcons(), [ - 'pwa_theme_color' => '#foobar', - 'pwa_bg_color' => '#barfoo', + 'pwa_theme_color' => '#ffffff', + 'pwa_bg_color' => '#000000', 'icons' => [ 'images/site_icons/touch-icon-192.png', 'images/site_icons/touch-icon-512.png', ] ], ], - 'header without data' => [ + 'invalid hex colors' => [ + [ + 'pwa_theme_color' => '#gggggg', + 'pwa_bg_color' => 'invalid', + ], + [], + [ + 'pwa_theme_color' => '#gggggg', + 'pwa_bg_color' => 'invalid', + 'icons' => [], + ], + ], + 'empty values' => [ [ 'pwa_theme_color' => '', 'pwa_bg_color' => '', @@ -123,6 +129,22 @@ public function header_updates_test_data(): array ]; } + private static function getValidIcons(): array + { + return [ + [ + 'src' => 'images/site_icons/touch-icon-192.png', + 'sizes' => '192x192', + 'type' => 'image/png' + ], + [ + 'src' => 'images/site_icons/touch-icon-512.png', + 'sizes' => '512x512', + 'type' => 'image/png' + ], + ]; + } + /** * @param $configs * @param $icons @@ -132,27 +154,35 @@ public function header_updates_test_data(): array */ public function test_header_updates($configs, $icons, $expected) { - foreach ($configs as $key => $value) - { - $this->user->style[$key] = $value; - } - - $this->helper->expects(static::once()) + // Setup expectations + $this->pwa_helper->expects(static::once()) ->method('get_icons') ->willReturn($icons); + $templateVars = [ + 'PWA_THEME_COLOR' => $expected['pwa_theme_color'], + 'PWA_BG_COLOR' => $expected['pwa_bg_color'], + 'U_TOUCH_ICONS' => $expected['icons'], + ]; + $this->template->expects(static::once()) ->method('assign_vars') - ->with([ - 'PWA_THEME_COLOR' => $expected['pwa_theme_color'], - 'PWA_BG_COLOR' => $expected['pwa_bg_color'], - 'U_TOUCH_ICONS' => $expected['icons'], - ]); + ->with(static::identicalTo($templateVars)); + + // Apply configurations + foreach ($configs as $key => $value) { + $this->user->style[$key] = $value; + } - $this->get_listener()->header_updates(); + $listener = $this->get_listener(); + $listener->header_updates(); + + // Verify the final state + $this->assertEquals($configs['pwa_theme_color'], $this->user->style['pwa_theme_color']); + $this->assertEquals($configs['pwa_bg_color'], $this->user->style['pwa_bg_color']); } - public function manifest_updates_test_data(): array + public static function manifest_updates_test_data(): array { return [ 'root path, no color options' => [ @@ -213,32 +243,56 @@ public function manifest_updates_test_data(): array */ public function test_manifest_updates($board_path, $configs, $expected) { + $initialManifest = [ + 'name' => 'Test Site', + 'short_name' => 'TestSite', + 'display' => 'standalone', + 'orientation' => 'portrait', + 'start_url' => './', + 'scope' => './', + ]; + $event = new data([ - 'manifest' => [ - 'name' => 'Test Site', - 'short_name' => 'TestSite', - 'display' => 'standalone', - 'orientation' => 'portrait', - 'start_url' => './', - 'scope' => './', - ], + 'manifest' => $initialManifest, 'board_path' => $board_path, ]); - foreach ($configs as $key => $value) - { + // Set up and verify the initial state + $this->assertSame($initialManifest, $event['manifest']); + + foreach ($configs as $key => $value) { $this->user->style[$key] = $value; } - $expected = array_merge($event['manifest'], $expected); + $expected = array_merge($initialManifest, $expected); - $this->helper->expects(static::once()) + // Verify helper method call + $this->pwa_helper->expects(static::once()) ->method('get_icons') ->with($board_path) ->willReturn($expected['icons'] ?? []); - $this->get_listener()->manifest_updates($event); + // Execute test + $listener = $this->get_listener(); + $listener->manifest_updates($event); + // Verify the final state $this->assertSame($expected, $event['manifest']); + + // Verify manifest structure + if (!empty($event['manifest'])) { + $this->assertArrayHasKey('name', $event['manifest']); + $this->assertArrayHasKey('short_name', $event['manifest']); + $this->assertArrayHasKey('display', $event['manifest']); + $this->assertArrayHasKey('orientation', $event['manifest']); + } + + // Verify color format if present + if (isset($event['manifest']['theme_color'])) { + $this->assertMatchesRegularExpression('/^#[0-9a-f]{6}$/i', $event['manifest']['theme_color']); + } + if (isset($event['manifest']['background_color'])) { + $this->assertMatchesRegularExpression('/^#[0-9a-f]{6}$/i', $event['manifest']['background_color']); + } } } diff --git a/tests/unit/ext_test.php b/tests/unit/ext_test.php index 8abfb2c..262ee5d 100644 --- a/tests/unit/ext_test.php +++ b/tests/unit/ext_test.php @@ -47,7 +47,7 @@ protected function setUp(): void * * @return array */ - public function ext_test_data(): array + public static function ext_test_data(): array { return [ 'current version' => [ @@ -93,7 +93,7 @@ public function test_ext($version, $expected) self::assertSame($expected, $ext->is_enableable()); } - public function enable_test_data(): array + public static function enable_test_data(): array { return [ 'dir exists' => [ diff --git a/tests/unit/helper_test.php b/tests/unit/helper_test.php index 69295cd..8c0473c 100644 --- a/tests/unit/helper_test.php +++ b/tests/unit/helper_test.php @@ -11,9 +11,6 @@ namespace phpbb\pwakit\tests\unit; use FastImageSize\FastImageSize; -use PHPUnit\DbUnit\DataSet\DefaultDataSet; -use PHPUnit\DbUnit\DataSet\IDataSet; -use PHPUnit\DbUnit\DataSet\XmlDataSet; use PHPUnit\Framework\MockObject\MockObject; use phpbb\cache\driver\driver_interface as cache; use phpbb\config\config; @@ -51,7 +48,7 @@ protected static function setup_extensions(): array return ['phpbb/pwakit']; } - protected function getDataSet(): IDataSet|XmlDataSet|DefaultDataSet + protected function getDataSet() { return $this->createXMLDataSet(self::FIXTURES . 'storage.xml'); } @@ -178,7 +175,7 @@ public function test_get_icons_empty() $this->assertCount(0, array_column($this->helper->get_icons(), 'src')); } - public function delete_icon_test_data(): array + public static function delete_icon_test_data(): array { return [ 'empty icon name' => [ diff --git a/tests/unit/upload_test.php b/tests/unit/upload_test.php index 5ff8605..0f5069f 100644 --- a/tests/unit/upload_test.php +++ b/tests/unit/upload_test.php @@ -49,7 +49,7 @@ public function get_upload(): upload ); } - public function upload_data(): array + public static function upload_data(): array { return [ 'upload failed' => [false],