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

183 lines
5.3 KiB
PHP

<?php
namespace tests\flow;
use app\flow\ProcessContext;
/**
* ProcessContext 单元测试
*/
class ProcessContextTest extends TestCase
{
/**
* 测试创建上下文
*/
public function testCreateContext(): void
{
$context = ProcessContext::create([
'endoscopeId' => '123',
'endoscopeName' => '测试内镜',
'currentStep' => '清洗',
]);
$this->assertEquals('123', $context->endoscopeId);
$this->assertEquals('测试内镜', $context->endoscopeName);
$this->assertEquals('清洗', $context->currentStep);
$this->assertTrue($context->success);
}
/**
* 测试设置错误状态
*/
public function testSetError(): void
{
$context = $this->createContext();
$context->setError('测试错误');
$this->assertFalse($context->success);
$this->assertEquals('测试错误', $context->errorMessage);
}
/**
* 测试设置语音
*/
public function testSetVoice(): void
{
$context = $this->createContext();
$context->setVoice('清洗完成');
$this->assertEquals('清洗完成', $context->voiceMessage);
}
/**
* 测试获取完整语音
*/
public function testGetFullVoice(): void
{
$context = $this->createContext([
'endoscopeName' => '北院电子胃镜001',
]);
$context->setVoice('清洗完成');
$fullVoice = $context->getFullVoice();
// 当前实现返回的是语音消息本身,不包含内镜名称
$this->assertEquals('清洗完成', $fullVoice);
}
/**
* 测试步骤时间管理
*/
public function testStepTimeManagement(): void
{
$context = $this->createContext();
$time = date('Y-m-d H:i:s');
$context->setStepLastTime('清洗', $time);
$this->assertEquals($time, $context->getStepLastTime('清洗'));
$this->assertNull($context->getStepLastTime('消毒'));
}
/**
* 测试步骤时长管理
*/
public function testStepDurationManagement(): void
{
$context = $this->createContext();
// 设置自定义时长
$context->setStepDuration('清洗', 600);
$this->assertEquals(600, $context->getStepDuration('清洗'));
}
/**
* 测试默认步骤时长
*/
public function testDefaultStepDuration(): void
{
$context = $this->createContext();
// 测试默认值(与 ect_meta_process 表数据对齐)
$this->assertEquals(120, $context->getStepDuration('清洗')); // 手工洗最短 60s,机洗最短 120s,取保守值
$this->assertEquals(60, $context->getStepDuration('漂洗')); // 60s
$this->assertEquals(300, $context->getStepDuration('消毒')); // 300s
$this->assertEquals(0, $context->getStepDuration('不存在的步骤'));
}
/**
* 测试批次号生成
*/
public function testGenerateBatchNo(): void
{
$context = $this->createContext([
'endoscopeId' => '123',
]);
$batchNo = $context->generateBatchNo();
// 验证格式:YYYYMMDDHHMMSS + 内镜ID(6位) + 随机数(4位)
$this->assertEquals(24, strlen($batchNo));
$this->assertMatchesRegularExpression('/^\d{14}\d{6}\d{4}$/', $batchNo);
}
/**
* 测试批次号唯一性
*/
public function testBatchNoUniqueness(): void
{
$context = $this->createContext();
$batchNo1 = $context->generateBatchNo();
$batchNo2 = $context->generateBatchNo();
$this->assertNotEquals($batchNo1, $batchNo2);
}
/**
* 测试是否可以开始新流程
*/
public function testCanStartNewProcess(): void
{
// 可以开始新流程的状态
$this->assertTrue($this->createContext(['currentStep' => ''])->canStartNewProcess());
$this->assertTrue($this->createContext(['currentStep' => '结束'])->canStartNewProcess());
$this->assertTrue($this->createContext(['currentStep' => '内镜取出'])->canStartNewProcess());
$this->assertTrue($this->createContext(['currentStep' => '测漏正常'])->canStartNewProcess());
// 不可以开始新流程的状态
$this->assertFalse($this->createContext(['currentStep' => '清洗'])->canStartNewProcess());
$this->assertFalse($this->createContext(['currentStep' => '消毒'])->canStartNewProcess());
}
/**
* 测试流程完成状态
*/
public function testIsWashProcessCompleted(): void
{
$this->assertTrue($this->createContext(['currentStep' => '结束'])->isWashProcessCompleted());
$this->assertFalse($this->createContext(['currentStep' => '清洗'])->isWashProcessCompleted());
$this->assertFalse($this->createContext(['currentStep' => ''])->isWashProcessCompleted());
}
/**
* 测试链式调用
*/
public function testChaining(): void
{
$context = $this->createContext();
$result = $context
->setVoice('测试')
->setStepLastTime('清洗', date('Y-m-d H:i:s'))
->setStepDuration('清洗', 300);
$this->assertSame($context, $result);
$this->assertEquals('测试', $context->voiceMessage);
}
}