Skip to content

Commit 56c3b48

Browse files
committed
Test file updates
1 parent 7e3039b commit 56c3b48

5 files changed

Lines changed: 159 additions & 66 deletions

File tree

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
44
backupGlobals="true"
55
backupStaticAttributes="false"
66
colors="true"

tests/unit/acp_module_test.php

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ protected function setUp(): void
5757
$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
5858
}
5959

60-
public function test_module_info()
60+
public function test_module_info(): void
6161
{
62-
self::assertEquals([
62+
$expected = [
6363
'\\phpbb\\pwakit\\acp\\pwa_acp_module' => [
6464
'filename' => '\\phpbb\\pwakit\\acp\\pwa_acp_module',
6565
'title' => 'ACP_PWA_KIT_TITLE',
@@ -71,7 +71,12 @@ public function test_module_info()
7171
],
7272
],
7373
],
74-
], $this->module_manager->get_module_infos('acp', 'pwa_acp_module'));
74+
];
75+
76+
$this->assertSame(
77+
$expected,
78+
$this->module_manager->get_module_infos('acp', 'pwa_acp_module')
79+
);
7580
}
7681

7782
public function module_auth_test_data(): array
@@ -85,9 +90,9 @@ public function module_auth_test_data(): array
8590
/**
8691
* @dataProvider module_auth_test_data
8792
*/
88-
public function test_module_auth($module_auth, $expected)
93+
public function test_module_auth(string $module_auth, bool $expected): void
8994
{
90-
self::assertEquals($expected, p_master::module_auth($module_auth, 0));
95+
$this->assertEquals($expected, p_master::module_auth($module_auth, 0));
9196
}
9297

9398
public function main_module_test_data(): array
@@ -100,7 +105,7 @@ public function main_module_test_data(): array
100105
/**
101106
* @dataProvider main_module_test_data
102107
*/
103-
public function test_main_module($mode)
108+
public function test_main_module(string $mode): void
104109
{
105110
global $phpbb_container, $request, $template;
106111

@@ -123,18 +128,56 @@ public function test_main_module($mode)
123128
->getMock();
124129

125130
$phpbb_container
126-
->expects(self::once())
131+
->expects($this->once())
127132
->method('get')
128133
->with('phpbb.pwakit.admin.controller')
129134
->willReturn($admin_controller);
130135

131136
$admin_controller
132-
->expects(self::once())
137+
->expects($this->once())
133138
->method('main');
134139

135140
$p_master = new p_master();
136141
$p_master->module_ary[0]['is_duplicate'] = 0;
137142
$p_master->module_ary[0]['url_extra'] = '';
138143
$p_master->load('acp', pwa_acp_module::class, $mode);
139144
}
145+
146+
public function test_main_module_with_missing_controller(): void
147+
{
148+
global $phpbb_container, $template, $request;
149+
150+
$this->expectException(\RuntimeException::class);
151+
$this->expectExceptionMessage('Service not found: phpbb.pwakit.admin.controller');
152+
153+
if (!defined('IN_ADMIN')) {
154+
define('IN_ADMIN', true);
155+
}
156+
157+
// Set up template mock
158+
$template = $this->getMockBuilder(template::class)
159+
->disableOriginalConstructor()
160+
->getMock();
161+
162+
// Set up request mock
163+
$request = $this->getMockBuilder(request::class)
164+
->disableOriginalConstructor()
165+
->getMock();
166+
167+
// Set up container mock
168+
$phpbb_container = $this->getMockBuilder(ContainerInterface::class)
169+
->disableOriginalConstructor()
170+
->getMock();
171+
172+
$phpbb_container
173+
->expects($this->once())
174+
->method('get')
175+
->with('phpbb.pwakit.admin.controller')
176+
->willThrowException(new \RuntimeException('Service not found: phpbb.pwakit.admin.controller'));
177+
178+
$p_master = new p_master();
179+
$p_master->module_ary[0]['is_duplicate'] = 0;
180+
$p_master->module_ary[0]['url_extra'] = '';
181+
$p_master->load('acp', pwa_acp_module::class, 'settings');
182+
}
140183
}

tests/unit/admin_controller_test.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use phpbb\db\driver\driver_interface as dbal;
1515
use phpbb_mock_cache;
1616
use phpbb_mock_event_dispatcher;
17-
use PHPUnit\DbUnit\DataSet\DefaultDataSet;
18-
use PHPUnit\DbUnit\DataSet\IDataSet;
1917
use PHPUnit\DbUnit\DataSet\XmlDataSet;
2018
use PHPUnit\Framework\MockObject\MockObject;
2119
use phpbb\config\config;
@@ -48,7 +46,7 @@ protected static function setup_extensions(): array
4846
return ['phpbb/pwakit'];
4947
}
5048

51-
protected function getDataSet(): IDataSet|XmlDataSet|DefaultDataSet
49+
protected function getDataSet(): XmlDataSet
5250
{
5351
return $this->createXMLDataSet(__DIR__ . '/../fixtures/styles.xml');
5452
}

tests/unit/event_listener_test.php

Lines changed: 105 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class event_listener_test extends phpbb_test_case
2323
{
2424
protected user|MockObject $user;
2525
protected template|MockObject $template;
26-
protected helper $helper;
26+
protected helper $pwa_helper;
2727

2828
/**
2929
* Setup test environment
@@ -32,16 +32,20 @@ protected function setUp(): void
3232
{
3333
parent::setUp();
3434

35-
$this->user = $this->createMock(user::class);
36-
$this->user->optionset('user_id', 2);
35+
$this->user = $this->getMockBuilder(user::class)
36+
->disableOriginalConstructor()
37+
->getMock();
38+
$this->user->method('optionset')
39+
->with('user_id', 2)
40+
->willReturn(null);
3741
$this->user->data['user_id'] = 2;
3842
$this->user->style['pwa_bg_color'] = '';
3943
$this->user->style['pwa_theme_color'] = '';
4044

4145
$this->template = $this->getMockBuilder(template::class)
4246
->getMock();
4347

44-
$this->helper = $this->getMockBuilder(helper::class)
48+
$this->pwa_helper = $this->getMockBuilder(helper::class)
4549
->disableOriginalConstructor()
4650
->getMock();
4751
}
@@ -54,7 +58,7 @@ protected function setUp(): void
5458
protected function get_listener(): main_listener
5559
{
5660
return new main_listener(
57-
$this->helper,
61+
$this->pwa_helper,
5862
$this->template,
5963
$this->user
6064
);
@@ -73,42 +77,44 @@ public function test_construct()
7377
*/
7478
public function test_getSubscribedEvents()
7579
{
80+
$events = main_listener::getSubscribedEvents();
7681
static::assertEquals([
77-
'core.page_header',
78-
'core.modify_manifest',
79-
], array_keys(\phpbb\pwakit\event\main_listener::getSubscribedEvents()));
82+
'core.page_header' => 'header_updates',
83+
'core.modify_manifest' => 'manifest_updates',
84+
], $events);
8085
}
8186

8287
public function header_updates_test_data(): array
8388
{
8489
return [
85-
'header with data' => [
86-
[
87-
'pwa_theme_color' => '#foobar',
88-
'pwa_bg_color' => '#barfoo',
89-
],
90+
'valid hex colors' => [
9091
[
91-
[
92-
'src' => 'images/site_icons/touch-icon-192.png',
93-
'sizes' => '192x192',
94-
'type' => 'image/png'
95-
],
96-
[
97-
'src' => 'images/site_icons/touch-icon-512.png',
98-
'sizes' => '512x512',
99-
'type' => 'image/png'
100-
],
92+
'pwa_theme_color' => '#ffffff',
93+
'pwa_bg_color' => '#000000',
10194
],
95+
self::getValidIcons(),
10296
[
103-
'pwa_theme_color' => '#foobar',
104-
'pwa_bg_color' => '#barfoo',
97+
'pwa_theme_color' => '#ffffff',
98+
'pwa_bg_color' => '#000000',
10599
'icons' => [
106100
'images/site_icons/touch-icon-192.png',
107101
'images/site_icons/touch-icon-512.png',
108102
]
109103
],
110104
],
111-
'header without data' => [
105+
'invalid hex colors' => [
106+
[
107+
'pwa_theme_color' => '#gggggg',
108+
'pwa_bg_color' => 'invalid',
109+
],
110+
[],
111+
[
112+
'pwa_theme_color' => '#gggggg',
113+
'pwa_bg_color' => 'invalid',
114+
'icons' => [],
115+
],
116+
],
117+
'empty values' => [
112118
[
113119
'pwa_theme_color' => '',
114120
'pwa_bg_color' => '',
@@ -123,6 +129,22 @@ public function header_updates_test_data(): array
123129
];
124130
}
125131

132+
private static function getValidIcons(): array
133+
{
134+
return [
135+
[
136+
'src' => 'images/site_icons/touch-icon-192.png',
137+
'sizes' => '192x192',
138+
'type' => 'image/png'
139+
],
140+
[
141+
'src' => 'images/site_icons/touch-icon-512.png',
142+
'sizes' => '512x512',
143+
'type' => 'image/png'
144+
],
145+
];
146+
}
147+
126148
/**
127149
* @param $configs
128150
* @param $icons
@@ -132,24 +154,32 @@ public function header_updates_test_data(): array
132154
*/
133155
public function test_header_updates($configs, $icons, $expected)
134156
{
135-
foreach ($configs as $key => $value)
136-
{
137-
$this->user->style[$key] = $value;
138-
}
139-
140-
$this->helper->expects(static::once())
157+
// Setup expectations
158+
$this->pwa_helper->expects(static::once())
141159
->method('get_icons')
142160
->willReturn($icons);
143161

162+
$templateVars = [
163+
'PWA_THEME_COLOR' => $expected['pwa_theme_color'],
164+
'PWA_BG_COLOR' => $expected['pwa_bg_color'],
165+
'U_TOUCH_ICONS' => $expected['icons'],
166+
];
167+
144168
$this->template->expects(static::once())
145169
->method('assign_vars')
146-
->with([
147-
'PWA_THEME_COLOR' => $expected['pwa_theme_color'],
148-
'PWA_BG_COLOR' => $expected['pwa_bg_color'],
149-
'U_TOUCH_ICONS' => $expected['icons'],
150-
]);
170+
->with(static::identicalTo($templateVars));
171+
172+
// Apply configurations
173+
foreach ($configs as $key => $value) {
174+
$this->user->style[$key] = $value;
175+
}
151176

152-
$this->get_listener()->header_updates();
177+
$listener = $this->get_listener();
178+
$listener->header_updates();
179+
180+
// Verify the final state
181+
$this->assertEquals($configs['pwa_theme_color'], $this->user->style['pwa_theme_color']);
182+
$this->assertEquals($configs['pwa_bg_color'], $this->user->style['pwa_bg_color']);
153183
}
154184

155185
public function manifest_updates_test_data(): array
@@ -213,32 +243,56 @@ public function manifest_updates_test_data(): array
213243
*/
214244
public function test_manifest_updates($board_path, $configs, $expected)
215245
{
246+
$initialManifest = [
247+
'name' => 'Test Site',
248+
'short_name' => 'TestSite',
249+
'display' => 'standalone',
250+
'orientation' => 'portrait',
251+
'start_url' => './',
252+
'scope' => './',
253+
];
254+
216255
$event = new data([
217-
'manifest' => [
218-
'name' => 'Test Site',
219-
'short_name' => 'TestSite',
220-
'display' => 'standalone',
221-
'orientation' => 'portrait',
222-
'start_url' => './',
223-
'scope' => './',
224-
],
256+
'manifest' => $initialManifest,
225257
'board_path' => $board_path,
226258
]);
227259

228-
foreach ($configs as $key => $value)
229-
{
260+
// Set up and verify the initial state
261+
$this->assertSame($initialManifest, $event['manifest']);
262+
263+
foreach ($configs as $key => $value) {
230264
$this->user->style[$key] = $value;
231265
}
232266

233-
$expected = array_merge($event['manifest'], $expected);
267+
$expected = array_merge($initialManifest, $expected);
234268

235-
$this->helper->expects(static::once())
269+
// Verify helper method call
270+
$this->pwa_helper->expects(static::once())
236271
->method('get_icons')
237272
->with($board_path)
238273
->willReturn($expected['icons'] ?? []);
239274

240-
$this->get_listener()->manifest_updates($event);
275+
// Execute test
276+
$listener = $this->get_listener();
277+
$listener->manifest_updates($event);
241278

279+
// Verify the final state
242280
$this->assertSame($expected, $event['manifest']);
281+
282+
// Verify manifest structure
283+
if (!empty($event['manifest'])) {
284+
$this->assertArrayHasKey('name', $event['manifest']);
285+
$this->assertArrayHasKey('short_name', $event['manifest']);
286+
$this->assertArrayHasKey('display', $event['manifest']);
287+
$this->assertArrayHasKey('orientation', $event['manifest']);
288+
}
289+
290+
// Verify color format if present
291+
if (isset($event['manifest']['theme_color'])) {
292+
$this->assertMatchesRegularExpression('/^#[0-9a-f]{6}$/i', $event['manifest']['theme_color']);
293+
}
294+
if (isset($event['manifest']['background_color'])) {
295+
$this->assertMatchesRegularExpression('/^#[0-9a-f]{6}$/i', $event['manifest']['background_color']);
296+
}
243297
}
244298
}

tests/unit/helper_test.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
namespace phpbb\pwakit\tests\unit;
1212

1313
use FastImageSize\FastImageSize;
14-
use PHPUnit\DbUnit\DataSet\DefaultDataSet;
15-
use PHPUnit\DbUnit\DataSet\IDataSet;
1614
use PHPUnit\DbUnit\DataSet\XmlDataSet;
1715
use PHPUnit\Framework\MockObject\MockObject;
1816
use phpbb\cache\driver\driver_interface as cache;
@@ -51,7 +49,7 @@ protected static function setup_extensions(): array
5149
return ['phpbb/pwakit'];
5250
}
5351

54-
protected function getDataSet(): IDataSet|XmlDataSet|DefaultDataSet
52+
protected function getDataSet(): XmlDataSet
5553
{
5654
return $this->createXMLDataSet(self::FIXTURES . 'storage.xml');
5755
}

0 commit comments

Comments
 (0)