Skip to content

Commit b802b5f

Browse files
committed
Handle 1 second time discrepancies in test better
Signed-off-by: Matt Friedman <[email protected]>
1 parent 76b8f31 commit b802b5f

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

tests/event/listener_test.php

+36-10
Original file line numberDiff line numberDiff line change
@@ -253,24 +253,50 @@ public function test_load_template_data($user_id, $method_data, $subscriptions,
253253
'VAPID_PUBLIC_KEY' => $this->config['wpn_webpush_vapid_public'],
254254
'U_WEBPUSH_WORKER_URL' => $this->controller_helper->route('phpbb_webpushnotifications_ucp_push_worker_controller'),
255255
'SUBSCRIPTIONS' => $subscriptions,
256+
'WEBPUSH_FORM_TOKENS' => $this->form_helper->get_form_tokens(\phpbb\webpushnotifications\ucp\controller\webpush::FORM_TOKEN_UCP),
256257
];
257258

258-
foreach ($expectedValues as $key => $value)
259+
// Check all required keys exist first
260+
$missingKeys = array_diff(array_keys($expectedValues), array_keys($arg));
261+
if (!empty($missingKeys))
259262
{
260-
if (!array_key_exists($key, $arg))
261-
{
262-
$this->fail("Expected key '$key' is missing from the argument array");
263-
}
264-
if ($arg[$key] !== $value)
263+
$this->fail("Expected key(s) '" . implode("', '", $missingKeys) . "' missing from argument array");
264+
}
265+
266+
// Handle WEBPUSH_FORM_TOKENS separately
267+
if (isset($arg['WEBPUSH_FORM_TOKENS']))
268+
{
269+
$tokenArg = $arg['WEBPUSH_FORM_TOKENS'];
270+
$tokenExpected = $expectedValues['WEBPUSH_FORM_TOKENS'];
271+
272+
// Check creation_time separately, allow for 1 second discrepancies during test run
273+
$timeDiff = abs($tokenArg['creation_time'] - $tokenExpected['creation_time']);
274+
if ($timeDiff > 1)
265275
{
266-
$this->fail("Mismatch for key '$key'. Expected: " . var_export($value, true) . ", Actual: " . var_export($arg[$key], true));
276+
$this->fail(sprintf(
277+
"Creation time difference too large. Expected: %d, Actual: %d",
278+
$tokenExpected['creation_time'],
279+
$tokenArg['creation_time']
280+
));
267281
}
282+
// Remove creation_time after checking to allow other fields comparison
283+
unset($tokenArg['creation_time'], $tokenExpected['creation_time']);
284+
$arg['WEBPUSH_FORM_TOKENS'] = $tokenArg;
285+
$expectedValues['WEBPUSH_FORM_TOKENS'] = $tokenExpected;
268286
}
269287

270-
// Check if WEBPUSH_FORM_TOKENS exists, but don't check its value
271-
if (!array_key_exists('WEBPUSH_FORM_TOKENS', $arg))
288+
// Compare values individually
289+
foreach ($expectedValues as $key => $value)
272290
{
273-
$this->fail("Expected key 'WEBPUSH_FORM_TOKENS' is missing from the argument array");
291+
if ($arg[$key] !== $value)
292+
{
293+
$this->fail(sprintf(
294+
"Mismatch for key '%s'. Expected: %s, Actual: %s",
295+
$key,
296+
var_export($value, true),
297+
var_export($arg[$key], true)
298+
));
299+
}
274300
}
275301

276302
return true;

0 commit comments

Comments
 (0)