Skip to content

Commit d86b6bc

Browse files
committed
add getMessage, deleteMessage
1 parent 64f6836 commit d86b6bc

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed

resources/autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
require_once __DIR__ . "/lib/exceptions/EnsureException.php";
3333
require_once __DIR__ . "/lib/exceptions/EncodingUnknownException.php";
3434
require_once __DIR__ . "/lib/exceptions/EncodingConversionException.php";
35+
require_once __DIR__ . "/lib/exceptions/UnityHTTPDMessageNotFoundException.php";
3536

3637
require_once __DIR__ . "/config.php";
3738
require __DIR__ . "/init.php";

resources/lib/UnityHTTPD.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use UnityWebPortal\lib\exceptions\NoDieException;
66
use UnityWebPortal\lib\exceptions\ArrayKeyException;
7+
use UnityWebPortal\lib\exceptions\UnityHTTPDMessageNotFoundException;
78
use RuntimeException;
89

910
enum UnityHTTPDMessageLevel: string
@@ -323,8 +324,49 @@ public static function getMessages()
323324
return $_SESSION["messages"];
324325
}
325326

326-
public static function clearMessages()
327-
{
328-
$_SESSION["messages"] = [];
327+
private static function getMessageIndex(
328+
UnityHTTPDMessageLevel $level,
329+
string $title_regex,
330+
string $body_regex,
331+
) {
332+
$messages = self::getMessages();
333+
$error_msg = sprintf(
334+
"message(level='%s' title_regex='%s' body_regex='%s'), not found. found messages: %s",
335+
$level->value,
336+
$title_regex,
337+
$body_regex,
338+
jsonEncode($messages),
339+
);
340+
foreach ($messages as $i => $message) {
341+
if (
342+
preg_match($title_regex, $message[0]) &&
343+
preg_match($body_regex, $message[1]) &&
344+
$level == $message[2]
345+
) {
346+
return $i;
347+
}
348+
}
349+
throw new UnityHTTPDMessageNotFoundException($error_msg);
350+
}
351+
352+
/* returns the 1st message that matches criteria or throws UnityHTTPDMessageNotFoundException */
353+
public static function getMessage(
354+
UnityHTTPDMessageLevel $level,
355+
string $title_regex,
356+
string $body_regex,
357+
) {
358+
$index = self::getMessageIndex($level, $title_regex, $body_regex);
359+
return $_SESSION["messages"][$index];
360+
}
361+
362+
/* deletes the 1st message that matches criteria or throws UnityHTTPDMessageNotFoundException */
363+
public static function deleteMessage(
364+
UnityHTTPDMessageLevel $level,
365+
string $title_regex,
366+
string $body_regex,
367+
) {
368+
$index = self::getMessageIndex($level, $title_regex, $body_regex);
369+
unset($_SESSION["messages"][$index]);
370+
$_SESSION["messages"] = array_values($_SESSION["messages"]);
329371
}
330372
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace UnityWebPortal\lib\exceptions;
4+
5+
class UnityHTTPDMessageNotFoundException extends \Exception {}

test/functional/PIMemberRequestTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public function testRequestMembership()
4747
$this->assertFalse($SQL->requestExists($uid, $gid));
4848
UnityHTTPD::clearMessages();
4949
$this->requestMembership("asdlkjasldkj");
50-
assertMessageExists(
51-
$this,
50+
UnityHTTPD::getMessage(
5251
UnityHTTPDMessageLevel::ERROR,
5352
"/.*/",
5453
"/^This PI doesn't exist$/",

test/phpunit-bootstrap.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
require_once __DIR__ . "/../resources/lib/exceptions/EnsureException.php";
2424
require_once __DIR__ . "/../resources/lib/exceptions/EncodingUnknownException.php";
2525
require_once __DIR__ . "/../resources/lib/exceptions/EncodingConversionException.php";
26+
require_once __DIR__ . "/../resources/lib/exceptions/UnityHTTPDMessageNotFoundException.php";
2627

2728
use UnityWebPortal\lib\UnityGroup;
2829
use UnityWebPortal\lib\UnityHTTPD;
@@ -322,29 +323,3 @@ function getAdminUser()
322323
{
323324
return ["[email protected]", "foo", "bar", "[email protected]"];
324325
}
325-
326-
function assertMessageExists(
327-
TestCase $test_case,
328-
UnityHTTPDMessageLevel $level,
329-
string $title_regex,
330-
string $body_regex,
331-
) {
332-
$messages = UnityHTTPD::getMessages();
333-
$error_msg = sprintf(
334-
"message(level='%s' title_regex='%s' body_regex='%s'), not found. found messages: %s",
335-
$level->value,
336-
$title_regex,
337-
$body_regex,
338-
jsonEncode($messages),
339-
);
340-
$messages_with_title = array_filter($messages, fn($x) => preg_match($title_regex, $x[0]));
341-
$messages_with_title_and_body = array_filter(
342-
$messages_with_title,
343-
fn($x) => preg_match($body_regex, $x[1]),
344-
);
345-
$messages_with_title_and_body_and_level = array_filter(
346-
$messages_with_title_and_body,
347-
fn($x) => $x[2] == $level,
348-
);
349-
$test_case->assertNotEmpty($messages_with_title_and_body_and_level, $error_msg);
350-
}

0 commit comments

Comments
 (0)