Skip to content

Commit 9909487

Browse files
committed
Fix tests
1 parent 7f0b413 commit 9909487

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

tests/event/listener_test.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function setUp(): void
5959

6060
global $phpbb_root_path, $phpEx, $user;
6161

62-
$db = $this->new_dbal();
62+
$this->db = $this->new_dbal();
6363

6464
$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
6565
$lang_loader->set_extension_manager(new \phpbb_mock_extension_manager($phpbb_root_path));
@@ -73,7 +73,7 @@ protected function setUp(): void
7373
$this->user->data['user_form_salt'] = '';
7474
$this->user->data['is_bot'] = false;
7575
$this->user->data['user_type'] = USER_NORMAL;
76-
$user_loader = new \phpbb\user_loader($db, $phpbb_root_path, $phpEx, 'phpbb_users');
76+
$user_loader = new \phpbb\user_loader($this->db, $phpbb_root_path, $phpEx, 'phpbb_users');
7777

7878
$this->controller_helper = $this->getMockBuilder('\phpbb\controller\helper')
7979
->disableOriginalConstructor()
@@ -109,7 +109,7 @@ protected function setUp(): void
109109

110110
$this->notification_method_webpush = new \phpbb\webpushnotifications\notification\method\webpush(
111111
$this->config,
112-
$db,
112+
$this->db,
113113
new \phpbb\log\dummy(),
114114
$user_loader,
115115
$this->user,
@@ -123,6 +123,29 @@ protected function setUp(): void
123123
$this->root_path = $phpbb_root_path;
124124
}
125125

126+
/**
127+
* Remove entries from subscriptions table to avoid conflicts with other tests
128+
* that have fixtures with the same table name
129+
*
130+
* @return void
131+
*/
132+
protected function tearDown(): void
133+
{
134+
$sql_layer = $this->db->get_sql_layer();
135+
$query = ($sql_layer === 'sqlite3')
136+
? 'DELETE FROM phpbb_wpn_push_subscriptions'
137+
: 'TRUNCATE TABLE phpbb_wpn_push_subscriptions';
138+
139+
$this->db->sql_query($query);
140+
141+
if ($sql_layer === 'postgres')
142+
{
143+
$this->db->sql_query('ALTER SEQUENCE phpbb_wpn_push_subscriptions_seq RESTART WITH 1');
144+
}
145+
146+
parent::tearDown();
147+
}
148+
126149
protected function set_listener()
127150
{
128151
$this->listener = new \phpbb\webpushnotifications\event\listener(

tests/notification/controller_webpush_test.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -402,37 +402,32 @@ public function test_sub_unsubscribe_success()
402402
$this->assertInstanceOf(JsonResponse::class, $response);
403403
$this->assertEquals(['success' => true, 'form_tokens' => $this->form_helper->get_form_tokens(webpush::FORM_TOKEN_UCP)], json_decode($response->getContent(), true));
404404

405-
// Get subscription data from database
406-
$sql = 'SELECT *
407-
FROM phpbb_wpn_push_subscriptions
408-
WHERE user_id = 2';
409-
$result = $this->db->sql_query($sql);
410-
$row = $this->db->sql_fetchrow($result);
411-
$this->db->sql_freeresult($result);
412-
413405
$this->assertEquals([
414406
'user_id' => '2',
415407
'endpoint' => 'test_endpoint',
416408
'p256dh' => 'test_p256dh',
417409
'auth' => 'test_auth',
418-
'expiration_time' => 0,
410+
'expiration_time' => '0',
419411
'subscription_id' => '1',
420-
], $row);
412+
], $this->get_subscription_data());
421413

422414
// Now unsubscribe
423415
$response = $this->controller->unsubscribe($symfony_request);
424416

425417
$this->assertInstanceOf(JsonResponse::class, $response);
426418
$this->assertEquals(['success' => true, 'form_tokens' => $this->form_helper->get_form_tokens(webpush::FORM_TOKEN_UCP)], json_decode($response->getContent(), true));
427419

428-
// Get subscription data from database
420+
$this->assertEmpty($this->get_subscription_data());
421+
}
422+
423+
private function get_subscription_data()
424+
{
429425
$sql = 'SELECT *
430426
FROM phpbb_wpn_push_subscriptions
431427
WHERE user_id = 2';
432428
$result = $this->db->sql_query($sql);
433429
$row = $this->db->sql_fetchrow($result);
434430
$this->db->sql_freeresult($result);
435-
436-
$this->assertEmpty($row);
431+
return $row;
437432
}
438433
}

0 commit comments

Comments
 (0)