Skip to content

Commit fa23844

Browse files
committed
add getMessage, deleteMessage
put back test function with regex
1 parent 64f6836 commit fa23844

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
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/phpunit-bootstrap.php

Lines changed: 1 addition & 0 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;

0 commit comments

Comments
 (0)