mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-03 05:41:52 +09:00
* Unify database install and upgrade paths * Keep SQLite installer sessions working and fix CI checks * Address database portability review findings * Isolate upgrade tests and share DBAL settings access * Preserve upgrade error reporting at the call sites * Consolidate portable database queries and listings * Verify live database routes and previous release upgrades * Keep test subprocesses in their parent runtime * Fix database portability review findings Connect to existing installer databases directly, bind and filter order IDs and metadata without multiplying rows, and serialize SQLite requests by individual session. Add regressions for PR #4749. * Document the completed Doctrine migration Record the removal of RedBeanPHP in #4152 for PR #4749. * Fix dashboard, order search, and date portability Exercise the authenticated client dashboard, calendar arithmetic, and database concurrency on all four engines in PR #4749. * Address database portability review feedback Preserve SQLite session metadata and bound lock waits. Isolate the PostgreSQL privilege fixture, cover both client order lists, and make the invoice due-date regression deterministic in PR #4749.
304 lines
10 KiB
PHP
304 lines
10 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Test Case
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| The closure you provide to your test functions is always bound to a specific
|
|
| PHPUnit test case class. By default, that class is "PHPUnit\Framework\TestCase".
|
|
|
|
|
| You can define custom test case classes in this file and use them via the
|
|
| `uses()` function.
|
|
|
|
|
*/
|
|
|
|
// Global test configuration
|
|
putenv('APP_ENV=test');
|
|
define('PATH_TESTS', __DIR__);
|
|
|
|
// Bump the memory limit. The strict-variables render-everything harness
|
|
// parses every FOSSBilling template and allocates many stubs and AST nodes,
|
|
// which exceeds the default 128M limit and causes the pest process to exit
|
|
// with 255 mid-suite. 2G is comfortably above peak usage (~185M).
|
|
ini_set('memory_limit', '2G');
|
|
|
|
// Disable DEBUG mode to prevent FOSSBilling\Core\Exception\BaseException from logging stack traces
|
|
// Must be defined BEFORE load.php is included
|
|
if (!defined('DEBUG')) {
|
|
define('DEBUG', false);
|
|
}
|
|
|
|
// Pre-declare translation functions before the application bootstrap loads them.
|
|
// These stubs will be used if the full translation system isn't initialized
|
|
if (!function_exists('__trans')) {
|
|
function __trans(string $msgid, ?array $values = null, ?string $from = null): string
|
|
{
|
|
return $values ? strtr($msgid, $values) : $msgid;
|
|
}
|
|
}
|
|
if (!function_exists('__pluralTrans')) {
|
|
function __pluralTrans(string $msgid, string $msgidPlural, int $number, ?array $values = null, ?string $from = null): string
|
|
{
|
|
return $values ? strtr($msgid, $values) : $msgid;
|
|
}
|
|
}
|
|
|
|
// Give each Pest process its own config and runtime state.
|
|
require_once __DIR__ . '/../src/vendor/autoload.php';
|
|
$testFilesystem = new Filesystem();
|
|
$testRuntime = sys_get_temp_dir() . '/fossbilling-test-' . bin2hex(random_bytes(8));
|
|
$testFilesystem->mkdir($testRuntime, 0o700);
|
|
// Forked workers inherit shutdown callbacks.
|
|
$testProcessId = getmypid();
|
|
register_shutdown_function(static function () use ($testFilesystem, $testRuntime, $testProcessId): void {
|
|
if (getmypid() === $testProcessId) {
|
|
$testFilesystem->remove($testRuntime);
|
|
}
|
|
});
|
|
define('PATH_TEST_RUNTIME', $testRuntime);
|
|
|
|
$testConfigFile = __DIR__ . '/../src/config.php';
|
|
if (!$testFilesystem->exists($testConfigFile)) {
|
|
$testConfigFile = __DIR__ . '/../src/config-sample.php';
|
|
}
|
|
$testConfig = require $testConfigFile;
|
|
$testFilesystem->dumpFile($testRuntime . '/config.php', '<?php return ' . var_export($testConfig, true) . ';');
|
|
|
|
require_once __DIR__ . '/../src/load.php';
|
|
|
|
// Load test helpers
|
|
require_once __DIR__ . '/Helpers/Container.php';
|
|
require_once __DIR__ . '/Helpers/Database.php';
|
|
require_once __DIR__ . '/Helpers/Factories.php';
|
|
require_once __DIR__ . '/Helpers/Api.php';
|
|
require_once __DIR__ . '/Helpers/Zip.php';
|
|
require_once __DIR__ . '/Support/CombinedTwigLoader.php';
|
|
require_once __DIR__ . '/Support/PermissiveStub.php';
|
|
require_once __DIR__ . '/Support/PermissiveContainer.php';
|
|
require_once __DIR__ . '/Support/VariableCollectorVisitor.php';
|
|
require_once __DIR__ . '/Support/StrictTemplateRenderer.php';
|
|
require_once __DIR__ . '/Support/TwigAstLinter.php';
|
|
require_once __DIR__ . '/Support/ToApiArrayAuditor.php';
|
|
|
|
// Load test datasets
|
|
require_once __DIR__ . '/Datasets/PeriodCodes.php';
|
|
require_once __DIR__ . '/Datasets/ValidationData.php';
|
|
require_once __DIR__ . '/Datasets/GeographicData.php';
|
|
|
|
/**
|
|
* Whether a Redis client extension is available. Cache-backend tests that need an
|
|
* environment without it (to exercise the connection-failure/fallback paths) skip
|
|
* themselves when this is true.
|
|
*/
|
|
function hasRedisExtension(): bool
|
|
{
|
|
return class_exists(Redis::class) || class_exists(Relay\Relay::class) || class_exists(RedisCluster::class);
|
|
}
|
|
|
|
/**
|
|
* Construct an API endpoint with the default test container.
|
|
*
|
|
* @template T of FOSSBilling\Core\Api\AbstractApi
|
|
*
|
|
* @param T $api
|
|
*
|
|
* @return T
|
|
*/
|
|
function apiEndpoint(FOSSBilling\Core\Api\AbstractApi $api): FOSSBilling\Core\Api\AbstractApi
|
|
{
|
|
$api->setDi(Tests\Helpers\container());
|
|
|
|
try {
|
|
$api->getIdentity();
|
|
} catch (Throwable) {
|
|
$realClass = $api::class;
|
|
// Mockery mocks have names like Mockery_2_Box_Mod_..._Api_Admin; unwrap to the mocked class.
|
|
if (str_starts_with($realClass, 'Mockery_') && ($parent = get_parent_class($api)) !== false) {
|
|
$realClass = $parent;
|
|
}
|
|
// Normalize both namespace separators for checks (mocks use underscores).
|
|
$normalized = str_replace(['\\', '_'], '\\', $realClass);
|
|
$api->setIdentity(match (true) {
|
|
str_contains($normalized, '\\Api\\Admin') => \Tests\Helpers\admin(),
|
|
str_contains($normalized, '\\Api\\Client') => \Tests\Helpers\client(),
|
|
default => new FOSSBilling\Core\Identity\Guest(),
|
|
});
|
|
}
|
|
|
|
return $api;
|
|
}
|
|
|
|
/**
|
|
* Runs $callback with APP_ENV set to $value (or unset entirely when $value is null),
|
|
* restoring the previous value afterward regardless of outcome.
|
|
*/
|
|
function withAppEnv(?string $value, callable $callback): mixed
|
|
{
|
|
$previous = getenv('APP_ENV');
|
|
|
|
if ($value === null) {
|
|
putenv('APP_ENV');
|
|
} else {
|
|
putenv('APP_ENV=' . $value);
|
|
}
|
|
|
|
try {
|
|
return $callback();
|
|
} finally {
|
|
putenv($previous === false ? 'APP_ENV' : 'APP_ENV=' . $previous);
|
|
}
|
|
}
|
|
|
|
/** Run an operation without PHP warnings, including warnings swallowed by its error handling. */
|
|
function withoutPhpWarnings(callable $callback): mixed
|
|
{
|
|
$warnings = [];
|
|
set_error_handler(static function (int $severity, string $message, string $file, int $line) use (&$warnings): bool {
|
|
if ((error_reporting() & $severity) !== 0) {
|
|
$warnings[] = sprintf('%s in %s:%d', $message, $file, $line);
|
|
}
|
|
|
|
return true;
|
|
}, E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE);
|
|
|
|
try {
|
|
return $callback();
|
|
} finally {
|
|
restore_error_handler();
|
|
expect($warnings)->toBe([], implode("\n", $warnings));
|
|
}
|
|
}
|
|
|
|
// Define TestLogger class after autoloader is registered.
|
|
// This is deferred because FOSSBilling\Core\Logging\Logger is loaded via the autoloader.
|
|
// @phpstan-ignore-next-line
|
|
if (!class_exists(Tests\Helpers\TestLogger::class)) {
|
|
// @phpstan-ignore-next-line
|
|
eval('
|
|
namespace Tests\Helpers;
|
|
|
|
class TestLogger extends \Psr\Log\AbstractLogger
|
|
{
|
|
public array $calls = [];
|
|
private string $channel = "application";
|
|
private array $context = [];
|
|
|
|
public function __construct()
|
|
{
|
|
$this->calls = [];
|
|
}
|
|
|
|
public function log($level, string|\\Stringable $message, array $context = []): void
|
|
{
|
|
$effectiveContext = [...$this->context, ...$context];
|
|
$params = [$message];
|
|
if ($effectiveContext !== []) {
|
|
$params[] = $effectiveContext;
|
|
}
|
|
|
|
$call = ["method" => $level, "params" => $params];
|
|
if ($this->channel !== "application") {
|
|
$call["channel"] = $this->channel;
|
|
}
|
|
|
|
$this->calls[] = $call;
|
|
}
|
|
|
|
public function withChannel(string $channel): static
|
|
{
|
|
$logger = clone $this;
|
|
$logger->calls =& $this->calls;
|
|
$logger->channel = $channel;
|
|
|
|
return $logger;
|
|
}
|
|
|
|
public function withContext(array $context): static
|
|
{
|
|
$logger = clone $this;
|
|
$logger->calls =& $this->calls;
|
|
$logger->context = [...$this->context, ...$context];
|
|
|
|
return $logger;
|
|
}
|
|
}
|
|
');
|
|
}
|
|
|
|
// Redirect error_log to /dev/null during tests to prevent "PHPUnit controlled exception" clutter
|
|
ini_set('error_log', '/dev/null');
|
|
|
|
// Configure Unit tests base with Mockery integration
|
|
uses(MockeryPHPUnitIntegration::class)
|
|
->beforeEach(function (): void {
|
|
// Unit test setup
|
|
})
|
|
->in('Unit');
|
|
|
|
// Configure E2E tests - requires live instance with API access
|
|
// Run with: ./vendor/bin/pest --testsuite=E2E
|
|
// Requires APP_URL and TEST_API_KEY environment variables
|
|
$appUrl = getenv('APP_URL');
|
|
$testApiKey = getenv('TEST_API_KEY');
|
|
|
|
if ($appUrl && $testApiKey) {
|
|
uses()
|
|
->beforeEach(function () use ($appUrl, $testApiKey): void {
|
|
Tests\Helpers\ApiClient::setBaseUrl($appUrl);
|
|
Tests\Helpers\ApiClient::setApiKey($testApiKey);
|
|
})
|
|
->in('E2E');
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Expectations
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| When you're writing tests, you often need to check that values meet certain
|
|
| conditions. The "expect()" function gives you access to a set of useful
|
|
| expectation methods.
|
|
|
|
|
*/
|
|
|
|
expect()->extend('toBeDomain', function (): void {
|
|
$this->not->toBeEmpty();
|
|
expect($this->value)->toBeString();
|
|
expect(filter_var($this->value, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME))->not->toBeFalse();
|
|
});
|
|
|
|
expect()->extend('toBeEmail', function (): void {
|
|
$this->not->toBeEmpty();
|
|
expect($this->value)->toBeString();
|
|
expect(filter_var($this->value, FILTER_VALIDATE_EMAIL))->not->toBeFalse();
|
|
});
|
|
|
|
expect()->extend('toBeUrl', function (): void {
|
|
$this->not->toBeEmpty();
|
|
expect($this->value)->toBeString();
|
|
expect(filter_var($this->value, FILTER_VALIDATE_URL))->not->toBeFalse();
|
|
});
|
|
|
|
expect()->extend('toBeUuid', function (): void {
|
|
$this->not->toBeEmpty();
|
|
expect($this->value)->toBeString();
|
|
expect(preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i', $this->value))->toBe(1);
|
|
});
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Functions
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| While Pest is very powerful out-of-the-box, you may have some testing code
|
|
| specific to your project that you don't want to repeat in every file.
|
|
| Here you can define custom helper functions and expect() extensions.
|
|
|
|
|
*/
|