CaiWuXiTong/tests/Unit/Deployment/FinanceImportRuntimeTest.php
WuYongJie 36af0e211a 修复生产内存保护:网页限额与重型导入隔离 (#430)
网页设置 2GiB 内存边界;生产重型导入统一进入 1CPU/1GiB 独立容器,保留只读文件、互斥、超时清理和发布保护。相关回归与资源隔离测试通过。
2026-10-21 14:30:06 +08:00

80 lines
2.9 KiB
PHP

<?php
namespace Tests\Unit\Deployment;
use App\Listeners\Common\RequireImportBatchContainer;
use Illuminate\Config\Repository;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Container\Container;
use Illuminate\Foundation\Application;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
class FinanceImportRuntimeTest extends TestCase
{
private $previousContainer;
protected function setUp(): void
{
$this->previousContainer = Container::getInstance();
$app = new Application(dirname(__DIR__, 3));
$app['env'] = 'production';
$app->instance('config', new Repository([
'finance_batch' => ['container' => false, 'import_commands' => ['product:monthly-weighted-average:base']],
]));
Container::setInstance($app);
}
protected function tearDown(): void
{
Container::setInstance($this->previousContainer);
parent::tearDown();
}
private function starting(string $name): CommandStarting
{
return new CommandStarting($name, new ArrayInput([]), new BufferedOutput());
}
public function test_production_imports_cannot_start_in_web_container(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('run-finance-batch.sh');
(new RequireImportBatchContainer())->handle($this->starting('order-data:import-after-sales-cost'));
}
public function test_registered_import_without_import_in_its_name_is_also_protected(): void
{
$this->expectException(RuntimeException::class);
(new RequireImportBatchContainer())->handle($this->starting('product:monthly-weighted-average:base'));
}
public function test_production_batch_runtime_can_import_and_health_checks_stay_available(): void
{
$listener = new RequireImportBatchContainer();
$listener->handle($this->starting('app:ready'));
config(['finance_batch.container' => true]);
$listener->handle($this->starting('order-data:import-after-sales-cost'));
$this->addToAssertionCount(2);
}
public function test_local_and_test_imports_keep_their_existing_usage(): void
{
app()['env'] = 'testing';
(new RequireImportBatchContainer())->handle($this->starting('order-data:import-after-sales-cost'));
$this->addToAssertionCount(1);
}
public function test_registry_includes_every_existing_import_command(): void
{
$root = dirname(__DIR__, 3);
$commands = json_decode(file_get_contents($root . '/deploy/finance-batch-imports.json'), true, 512, JSON_THROW_ON_ERROR);
foreach (glob($root . '/app/Console/Commands/Import*.php') as $file) {
preg_match('/\$signature\s*=\s*\'([^\s\']+)/', file_get_contents($file), $match);
$this->assertContains($match[1], $commands, basename($file));
}
}
}