102 lines
2.4 KiB
PHP
102 lines
2.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
define('PLUGIN_ROOT', dirname(__DIR__));
|
|
define('PROJECT_ROOT', dirname(PLUGIN_ROOT));
|
|
define('TEST_TMP', sys_get_temp_dir() . '/mail-login-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 (!str_contains($haystack, $needle)) {
|
|
throw new RuntimeException($message !== '' ? $message : 'Missing text: ' . $needle);
|
|
}
|
|
}
|
|
|
|
function assert_not_contains(string $needle, string $haystack, string $message = ''): void
|
|
{
|
|
if (str_contains($haystack, $needle)) {
|
|
throw new RuntimeException($message !== '' ? $message : 'Unexpected 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;
|
|
}
|
|
if (is_file($path) || is_link($path)) {
|
|
unlink($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());
|
|
}
|
|
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);
|
|
}
|
|
|