feat: implement global autoresponder plugin
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
define('PLUGIN_ROOT', $root);
|
||||
define('TEST_TMP', sys_get_temp_dir() . '/global-autoresponder-tests-' . getmypid());
|
||||
|
||||
@mkdir(TEST_TMP, 0700, true);
|
||||
|
||||
$tests = [];
|
||||
|
||||
function test(string $name, callable $fn): void
|
||||
{
|
||||
global $tests;
|
||||
$tests[] = [$name, $fn];
|
||||
}
|
||||
|
||||
function assert_true(bool $condition, string $message = 'Expected true'): void
|
||||
{
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
}
|
||||
|
||||
function assert_false(bool $condition, string $message = 'Expected false'): void
|
||||
{
|
||||
if ($condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
}
|
||||
|
||||
function assert_same(mixed $expected, mixed $actual, string $message = ''): void
|
||||
{
|
||||
if ($expected !== $actual) {
|
||||
throw new RuntimeException($message !== '' ? $message : 'Expected ' . var_export($expected, true) . ', got ' . var_export($actual, true));
|
||||
}
|
||||
}
|
||||
|
||||
function assert_contains(string $needle, string $haystack, string $message = ''): void
|
||||
{
|
||||
if (strpos($haystack, $needle) === false) {
|
||||
throw new RuntimeException($message !== '' ? $message : 'Missing text: ' . $needle);
|
||||
}
|
||||
}
|
||||
|
||||
function test_write(string $path, string $content): void
|
||||
{
|
||||
$dir = dirname($path);
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0700, true);
|
||||
}
|
||||
file_put_contents($path, $content);
|
||||
}
|
||||
|
||||
function test_rm_rf(string $path): void
|
||||
{
|
||||
if (!is_dir($path) && !is_file($path)) {
|
||||
return;
|
||||
}
|
||||
$it = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
|
||||
RecursiveIteratorIterator::CHILD_FIRST
|
||||
);
|
||||
foreach ($it as $item) {
|
||||
$item->isDir() ? rmdir($item->getPathname()) : unlink($item->getPathname());
|
||||
}
|
||||
if (is_dir($path)) {
|
||||
rmdir($path);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (glob(__DIR__ . '/*_test.php') ?: [] as $file) {
|
||||
require $file;
|
||||
}
|
||||
|
||||
$failures = 0;
|
||||
foreach ($tests as [$name, $fn]) {
|
||||
try {
|
||||
$fn();
|
||||
echo "PASS {$name}\n";
|
||||
} catch (Throwable $e) {
|
||||
$failures++;
|
||||
echo "FAIL {$name}: " . $e->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
test_rm_rf(TEST_TMP);
|
||||
|
||||
if ($failures > 0) {
|
||||
exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user