293 lines
8.9 KiB
PHP
293 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace tests\flow;
|
|
|
|
use app\flow\ProcessEngine;
|
|
use app\flow\config\ProcessConfig;
|
|
|
|
/**
|
|
* ProcessEngine 集成测试
|
|
*/
|
|
class ProcessEngineTest extends TestCase
|
|
{
|
|
protected ProcessEngine $engine;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->engine = ProcessEngine::createStandard();
|
|
}
|
|
|
|
/**
|
|
* 测试创建标准流程引擎
|
|
*/
|
|
public function testCreateStandardEngine(): void
|
|
{
|
|
$engine = ProcessEngine::createStandard();
|
|
|
|
$this->assertInstanceOf(ProcessEngine::class, $engine);
|
|
$this->assertNotNull($engine->getNode('清洗'));
|
|
$this->assertNotNull($engine->getNode('消毒'));
|
|
$this->assertNotNull($engine->getNode('结束'));
|
|
}
|
|
|
|
/**
|
|
* 测试创建无晨洗流程引擎
|
|
*/
|
|
public function testCreateNoMorningWashEngine(): void
|
|
{
|
|
$engine = ProcessEngine::createNoMorningWash();
|
|
|
|
$this->assertFalse($engine->getNode('晨洗')->isEnabled());
|
|
}
|
|
|
|
/**
|
|
* 测试创建简化流程引擎
|
|
*/
|
|
public function testCreateSimpleEngine(): void
|
|
{
|
|
$engine = ProcessEngine::createSimple();
|
|
|
|
$this->assertTrue($engine->getNode('清洗')->isEnabled());
|
|
$this->assertFalse($engine->getNode('漂洗')->isEnabled());
|
|
$this->assertFalse($engine->getNode('消毒')->isEnabled());
|
|
$this->assertTrue($engine->getNode('结束')->isEnabled());
|
|
}
|
|
|
|
/**
|
|
* 测试完整清洗流程
|
|
*/
|
|
public function testCompleteWashProcess(): void
|
|
{
|
|
// 1. 开始清洗
|
|
$context1 = $this->createContext([
|
|
'readerType' => '清洗',
|
|
'currentStep' => '',
|
|
'morningWashed' => true,
|
|
'endoscopeId' => '', // 避免触发 DB 查询
|
|
]);
|
|
$result1 = $this->engine->execute($context1);
|
|
$this->assertSuccess($result1);
|
|
$this->assertStep($result1, '清洗');
|
|
$batchNo = $result1->batchNo;
|
|
|
|
// 2. 漂洗
|
|
$context2 = $this->createContext([
|
|
'readerType' => '漂洗',
|
|
'currentStep' => '清洗',
|
|
'batchNo' => $batchNo,
|
|
'morningWashed' => true,
|
|
]);
|
|
$this->setStepTime($context2, '清洗', 400); // 超过5分钟
|
|
$result2 = $this->engine->execute($context2);
|
|
$this->assertSuccess($result2);
|
|
$this->assertStep($result2, '漂洗');
|
|
$this->assertEquals($batchNo, $result2->batchNo); // 批次号一致
|
|
|
|
// 3. 消毒
|
|
$context3 = $this->createContext([
|
|
'readerType' => '消毒',
|
|
'currentStep' => '漂洗',
|
|
'batchNo' => $batchNo,
|
|
'morningWashed' => true,
|
|
]);
|
|
$this->setStepTime($context3, '漂洗', 200); // 超过2分钟
|
|
$result3 = $this->engine->execute($context3);
|
|
$this->assertSuccess($result3);
|
|
$this->assertStep($result3, '消毒');
|
|
|
|
// 4. 终末漂洗
|
|
$context4 = $this->createContext([
|
|
'readerType' => '终末漂洗',
|
|
'currentStep' => '消毒',
|
|
'batchNo' => $batchNo,
|
|
'morningWashed' => true,
|
|
]);
|
|
$this->setStepTime($context4, '消毒', 400); // 超过5分钟
|
|
$result4 = $this->engine->execute($context4);
|
|
$this->assertSuccess($result4);
|
|
$this->assertStep($result4, '终末漂洗');
|
|
|
|
// 5. 干燥
|
|
$context5 = $this->createContext([
|
|
'readerType' => '干燥',
|
|
'currentStep' => '终末漂洗',
|
|
'batchNo' => $batchNo,
|
|
'morningWashed' => true,
|
|
]);
|
|
$this->setStepTime($context5, '终末漂洗', 200); // 超过2分钟
|
|
$result5 = $this->engine->execute($context5);
|
|
$this->assertSuccess($result5);
|
|
$this->assertStep($result5, '干燥');
|
|
|
|
// 6. 结束
|
|
$context6 = $this->createContext([
|
|
'readerType' => '结束',
|
|
'currentStep' => '干燥',
|
|
'batchNo' => $batchNo,
|
|
'morningWashed' => true,
|
|
]);
|
|
$this->setStepTime($context6, '干燥', 300); // 超过3分钟
|
|
$result6 = $this->engine->execute($context6);
|
|
$this->assertSuccess($result6);
|
|
$this->assertStep($result6, '结束');
|
|
}
|
|
|
|
/**
|
|
* 测试时间验证失败
|
|
*/
|
|
public function testTimeValidationFailure(): void
|
|
{
|
|
// 显式设置清洗时长为 300s
|
|
$strategy = new \app\flow\strategies\TimeValidationStrategy(['durations' => ['清洗' => 300]]);
|
|
$node = new \app\flow\nodes\WashNode();
|
|
|
|
$context = $this->createContext([
|
|
'readerType' => '清洗',
|
|
'currentStep' => '清洗',
|
|
'morningWashed' => true,
|
|
]);
|
|
|
|
// 设置2分钟前的时间(不足5分钟)
|
|
$this->setStepTime($context, '清洗', 120);
|
|
|
|
$result = $strategy->execute($context, $node);
|
|
|
|
$this->assertFailure($result, '刷错,清洗剩余');
|
|
}
|
|
|
|
/**
|
|
* 测试标准流程:清洗 -> 漂洗 -> 消毒
|
|
*/
|
|
public function testStandardWashRinseDisinfectFlow(): void
|
|
{
|
|
// 1. 清洗
|
|
$context1 = $this->createContext([
|
|
'readerType' => '清洗',
|
|
'currentStep' => '',
|
|
'morningWashed' => true,
|
|
'needMorningWash' => false,
|
|
'endoscopeId' => '',
|
|
]);
|
|
$result1 = $this->engine->execute($context1);
|
|
$this->assertSuccess($result1);
|
|
$this->assertEquals('清洗', $result1->currentStep);
|
|
|
|
// 2. 漂洗
|
|
$context2 = $this->createContext([
|
|
'readerType' => '漂洗',
|
|
'currentStep' => '清洗',
|
|
'batchNo' => $result1->batchNo,
|
|
'morningWashed' => true,
|
|
'needMorningWash' => false,
|
|
]);
|
|
$this->setStepTime($context2, '清洗', 400);
|
|
$result2 = $this->engine->execute($context2);
|
|
$this->assertSuccess($result2);
|
|
$this->assertEquals('漂洗', $result2->currentStep);
|
|
|
|
// 3. 消毒
|
|
$context3 = $this->createContext([
|
|
'readerType' => '消毒',
|
|
'currentStep' => '漂洗',
|
|
'batchNo' => $result1->batchNo,
|
|
'morningWashed' => true,
|
|
'needMorningWash' => false,
|
|
]);
|
|
$this->setStepTime($context3, '漂洗', 200);
|
|
$result3 = $this->engine->execute($context3);
|
|
$this->assertSuccess($result3);
|
|
$this->assertEquals('消毒', $result3->currentStep);
|
|
}
|
|
|
|
/**
|
|
* 测试禁用节点
|
|
*/
|
|
public function testDisableNode(): void
|
|
{
|
|
$this->engine->disableNode('干燥');
|
|
|
|
$this->assertFalse($this->engine->getNode('干燥')->isEnabled());
|
|
|
|
// 恢复
|
|
$this->engine->enableNode('干燥');
|
|
$this->assertTrue($this->engine->getNode('干燥')->isEnabled());
|
|
}
|
|
|
|
/**
|
|
* 测试获取所有节点
|
|
*/
|
|
public function testGetNodes(): void
|
|
{
|
|
$nodes = $this->engine->getNodes();
|
|
|
|
$this->assertArrayHasKey('清洗', $nodes);
|
|
$this->assertArrayHasKey('消毒', $nodes);
|
|
$this->assertArrayHasKey('结束', $nodes);
|
|
}
|
|
|
|
/**
|
|
* 测试获取启用的节点
|
|
*/
|
|
public function testGetEnabledNodes(): void
|
|
{
|
|
$this->engine->disableNode('干燥');
|
|
|
|
$enabledNodes = $this->engine->getEnabledNodes();
|
|
|
|
$this->assertArrayNotHasKey('干燥', $enabledNodes);
|
|
$this->assertArrayHasKey('清洗', $enabledNodes);
|
|
}
|
|
|
|
/**
|
|
* 测试更新配置
|
|
*/
|
|
public function testUpdateConfig(): void
|
|
{
|
|
$newConfig = ProcessConfig::createNoMorningWash();
|
|
|
|
$this->engine->updateConfig($newConfig);
|
|
|
|
$this->assertFalse($this->engine->getNode('晨洗')->isEnabled());
|
|
}
|
|
|
|
/**
|
|
* 测试机洗流程
|
|
*/
|
|
public function testMachineWashProcess(): void
|
|
{
|
|
$engine = ProcessEngine::createMachineWash();
|
|
|
|
// 1. 清洗
|
|
$context1 = $this->createContext([
|
|
'readerType' => '清洗',
|
|
'currentStep' => '',
|
|
'endoscopeId' => '',
|
|
]);
|
|
$result1 = $engine->execute($context1);
|
|
$this->assertSuccess($result1);
|
|
$this->assertStep($result1, '清洗');
|
|
|
|
// 2. 机洗
|
|
$context2 = $this->createContext([
|
|
'readerType' => '机洗',
|
|
'currentStep' => '清洗',
|
|
'batchNo' => $result1->batchNo,
|
|
]);
|
|
$this->setStepTime($context2, '清洗', 400);
|
|
$result2 = $engine->execute($context2);
|
|
$this->assertSuccess($result2);
|
|
$this->assertStep($result2, '机洗');
|
|
$this->assertEquals('机洗', $result2->processType);
|
|
|
|
// 3. 终末漂洗
|
|
$context3 = $this->createContext([
|
|
'readerType' => '终末漂洗',
|
|
'currentStep' => '机洗',
|
|
'batchNo' => $result1->batchNo,
|
|
]);
|
|
$result3 = $engine->execute($context3);
|
|
$this->assertSuccess($result3);
|
|
$this->assertStep($result3, '终末漂洗');
|
|
}
|
|
}
|