Files
tcpserver-flow/tests/flow/config/ProcessConfigTest.php
T
2026-03-08 22:58:56 +08:00

279 lines
8.1 KiB
PHP

<?php
namespace tests\flow\config;
use PHPUnit\Framework\TestCase;
use app\flow\config\ProcessConfig;
/**
* ProcessConfig 配置类单元测试
*/
class ProcessConfigTest extends TestCase
{
/**
* 测试创建默认配置
*/
public function testCreateDefaultConfig(): void
{
$config = new ProcessConfig();
$steps = $config->getSteps();
$this->assertNotEmpty($steps);
$this->assertArrayHasKey('清洗', array_column($steps, null, 'code'));
$this->assertArrayHasKey('消毒', array_column($steps, null, 'code'));
}
/**
* 测试从数组加载配置
*/
public function testFromArray(): void
{
$data = [
'steps' => [
['code' => '清洗', 'class' => 'WashNode', 'enabled' => true],
['code' => '结束', 'class' => 'EndNode', 'enabled' => true],
],
'morning_wash' => ['mode' => 'none'],
'time_validation' => [
'durations' => ['清洗' => 600],
],
];
$config = ProcessConfig::fromArray($data);
$this->assertEquals('none', $config->getMorningWashConfig()['mode']);
$this->assertEquals(600, $config->getTimeValidationConfig()['durations']['清洗']);
}
/**
* 测试获取启用的步骤
*/
public function testGetEnabledSteps(): void
{
$config = new ProcessConfig();
$config->skipStep('漂洗');
$config->skipStep('干燥');
$enabledSteps = $config->getEnabledSteps();
$stepCodes = array_column($enabledSteps, 'code');
$this->assertNotContains('漂洗', $stepCodes);
$this->assertNotContains('干燥', $stepCodes);
$this->assertContains('清洗', $stepCodes);
$this->assertContains('消毒', $stepCodes);
}
/**
* 测试添加步骤
*/
public function testAddStep(): void
{
$config = new ProcessConfig();
$config->addStep('自定义步骤', 'CustomNode');
$step = $config->getStep('自定义步骤');
$this->assertNotNull($step);
$this->assertEquals('CustomNode', $step['class']);
$this->assertTrue($step['enabled']);
}
/**
* 测试移除步骤
*/
public function testRemoveStep(): void
{
$config = new ProcessConfig();
$config->removeStep('漂洗');
$this->assertNull($config->getStep('漂洗'));
}
/**
* 测试设置节点启用状态
*/
public function testSetNodeEnabled(): void
{
$config = new ProcessConfig();
$this->assertTrue($config->isNodeEnabled('清洗'));
$config->setNodeEnabled('清洗', false);
$this->assertFalse($config->isNodeEnabled('清洗'));
$config->setNodeEnabled('清洗', true);
$this->assertTrue($config->isNodeEnabled('清洗'));
}
/**
* 测试晨洗配置
*/
public function testMorningWashConfig(): void
{
$config = new ProcessConfig();
// 默认配置
$defaultConfig = $config->getMorningWashConfig();
$this->assertEquals('daily_first', $defaultConfig['mode']);
// 修改配置
$config->setMorningWashMode('all');
$this->assertEquals('all', $config->getMorningWashConfig()['mode']);
// 设置完整配置
$config->setMorningWashConfig([
'mode' => 'specific_types',
'specific_types' => ['胃镜', '肠镜'],
]);
$morningConfig = $config->getMorningWashConfig();
$this->assertEquals('specific_types', $morningConfig['mode']);
$this->assertEquals(['胃镜', '肠镜'], $morningConfig['specific_types']);
}
/**
* 测试时间验证配置
*/
public function testTimeValidationConfig(): void
{
$config = new ProcessConfig();
$config->setStepDuration('清洗', 600);
$durations = $config->getTimeValidationConfig()['durations'];
$this->assertEquals(600, $durations['清洗']);
}
/**
* 测试语音模板配置
*/
public function testVoiceTemplateConfig(): void
{
$config = new ProcessConfig();
$config->setStepVoice('normal_wash', '清洗', '自定义清洗语音');
$config->setStepVoice('normal_wash', '消毒', '自定义消毒语音');
$templates = $config->getVoiceTemplates();
$this->assertEquals('自定义清洗语音', $templates['normal_wash']['清洗']);
$this->assertEquals('自定义消毒语音', $templates['normal_wash']['消毒']);
}
/**
* 测试设置步骤自定义语音
*/
public function testSetStepVoice(): void
{
$config = new ProcessConfig();
$config->setStepVoice('normal_wash', '清洗', '请开始清洗操作');
$templates = $config->getVoiceTemplates();
$this->assertEquals('请开始清洗操作', $templates['normal_wash']['清洗']);
}
/**
* 测试转换为数组
*/
public function testToArray(): void
{
$config = new ProcessConfig();
$array = $config->toArray();
$this->assertArrayHasKey('steps', $array);
$this->assertArrayHasKey('strategies', $array);
$this->assertArrayHasKey('voice_templates', $array);
$this->assertArrayHasKey('morning_wash', $array);
$this->assertArrayHasKey('time_validation', $array);
}
/**
* 测试创建标准流程配置
*/
public function testCreateStandard(): void
{
$config = ProcessConfig::createStandard();
$this->assertTrue($config->isNodeEnabled('晨洗'));
$this->assertTrue($config->isNodeEnabled('清洗'));
$this->assertTrue($config->isNodeEnabled('消毒'));
$this->assertTrue($config->isNodeEnabled('结束'));
}
/**
* 测试创建无晨洗流程配置
*/
public function testCreateNoMorningWash(): void
{
$config = ProcessConfig::createNoMorningWash();
$this->assertFalse($config->isNodeEnabled('晨洗'));
$this->assertEquals('none', $config->getMorningWashConfig()['mode']);
}
/**
* 测试创建简化流程配置
*/
public function testCreateSimple(): void
{
$config = ProcessConfig::createSimple();
$this->assertTrue($config->isNodeEnabled('清洗'));
$this->assertFalse($config->isNodeEnabled('漂洗'));
$this->assertFalse($config->isNodeEnabled('消毒'));
$this->assertTrue($config->isNodeEnabled('结束'));
}
/**
* 测试创建机洗流程配置
*/
public function testCreateMachineWash(): void
{
$config = ProcessConfig::createMachineWash();
$this->assertTrue($config->isNodeEnabled('清洗'));
$this->assertTrue($config->isNodeEnabled('机洗'));
$this->assertFalse($config->isNodeEnabled('漂洗'));
$this->assertFalse($config->isNodeEnabled('消毒'));
$this->assertTrue($config->isNodeEnabled('终末漂洗'));
}
/**
* 测试创建无干燥流程配置
*/
public function testCreateNoDry(): void
{
$config = ProcessConfig::createNoDry();
$this->assertFalse($config->isNodeEnabled('干燥'));
$this->assertTrue($config->isNodeEnabled('清洗'));
}
/**
* 测试创建仅干燥流程配置
*/
public function testCreateDryOnly(): void
{
$config = ProcessConfig::createDryOnly();
$this->assertTrue($config->isNodeEnabled('干燥'));
$this->assertFalse($config->isNodeEnabled('清洗'));
$this->assertFalse($config->isNodeEnabled('消毒'));
}
/**
* 测试链式调用
*/
public function testChaining(): void
{
$config = new ProcessConfig();
$result = $config
->setNodeEnabled('漂洗', false)
->setMorningWashMode('all')
->setStepDuration('清洗', 600)
->setStepVoice('normal_wash', '清洗', '测试语音');
$this->assertSame($config, $result);
$this->assertFalse($config->isNodeEnabled('漂洗'));
$this->assertEquals('all', $config->getMorningWashConfig()['mode']);
}
}