168 lines
4.5 KiB
PHP
168 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace tests\flow\strategies;
|
|
|
|
use tests\flow\TestCase;
|
|
use app\flow\strategies\TimeValidationStrategy;
|
|
use app\flow\nodes\WashNode;
|
|
|
|
/**
|
|
* 时间验证策略单元测试
|
|
*/
|
|
class TimeValidationStrategyTest extends TestCase
|
|
{
|
|
protected TimeValidationStrategy $strategy;
|
|
protected WashNode $node;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->strategy = new TimeValidationStrategy();
|
|
$this->node = new WashNode();
|
|
}
|
|
|
|
/**
|
|
* 测试首次执行没有时间限制
|
|
*/
|
|
public function testNoTimeLimitForFirstTime(): void
|
|
{
|
|
$context = $this->createContext([
|
|
'readerType' => '清洗',
|
|
'currentStep' => '',
|
|
]);
|
|
|
|
// 没有设置上次时间,应该通过
|
|
$result = $this->strategy->execute($context, $this->node);
|
|
|
|
$this->assertSuccess($result);
|
|
}
|
|
|
|
/**
|
|
* 测试时间满足要求
|
|
*/
|
|
public function testTimeRequirementMet(): void
|
|
{
|
|
$context = $this->createContext([
|
|
'readerType' => '清洗',
|
|
'currentStep' => '清洗',
|
|
]);
|
|
|
|
// 设置6分钟前的时间(要求5分钟)
|
|
$this->setStepTime($context, '清洗', 360);
|
|
|
|
$result = $this->strategy->execute($context, $this->node);
|
|
|
|
$this->assertSuccess($result);
|
|
}
|
|
|
|
/**
|
|
* 测试时间未满足要求
|
|
*/
|
|
public function testTimeRequirementNotMet(): void
|
|
{
|
|
// 显式设置清洗时长为 300s,模拟不同医院配置
|
|
$strategy = new TimeValidationStrategy(['durations' => ['清洗' => 300]]);
|
|
$context = $this->createContext([
|
|
'readerType' => '清洗',
|
|
'currentStep' => '清洗',
|
|
]);
|
|
|
|
// 设置2分钟前的时间(要扨5分钟)
|
|
$this->setStepTime($context, '清洗', 120);
|
|
|
|
$result = $strategy->execute($context, $this->node);
|
|
|
|
$this->assertFailure($result, '刷错,清洗剩余');
|
|
$this->assertStringContainsString('180秒', $result->errorMessage);
|
|
}
|
|
|
|
/**
|
|
* 测试自定义时长配置
|
|
*/
|
|
public function testCustomDuration(): void
|
|
{
|
|
$strategy = new TimeValidationStrategy([
|
|
'durations' => [
|
|
'清洗' => 600, // 10分钟
|
|
],
|
|
]);
|
|
|
|
$context = $this->createContext([
|
|
'readerType' => '清洗',
|
|
'currentStep' => '清洗',
|
|
]);
|
|
|
|
// 设置8分钟前的时间(要求10分钟)
|
|
$this->setStepTime($context, '清洗', 480);
|
|
|
|
$result = $strategy->execute($context, $this->node);
|
|
|
|
$this->assertFailure($result);
|
|
$this->assertStringContainsString('120秒', $result->errorMessage);
|
|
}
|
|
|
|
/**
|
|
* 测试策略适用性
|
|
*/
|
|
public function testIsApplicable(): void
|
|
{
|
|
// 清洗步骤适用
|
|
$this->assertTrue($this->strategy->isApplicable(
|
|
$this->createContext(),
|
|
$this->node
|
|
));
|
|
|
|
// 结束节点不适用(不在 stepDurations 中)
|
|
$endNode = new \app\flow\nodes\EndNode();
|
|
$this->assertFalse($this->strategy->isApplicable(
|
|
$this->createContext(),
|
|
$endNode
|
|
));
|
|
}
|
|
|
|
/**
|
|
* 测试不在 stepDurations 中的步骤策略不验证时间
|
|
*/
|
|
public function testNonDurationStepIsSkipped(): void
|
|
{
|
|
$endNode = new \app\flow\nodes\EndNode();
|
|
$context = $this->createContext([
|
|
'currentStep' => '结束',
|
|
]);
|
|
// 结束节点不在 stepDurations,策略应该跳过
|
|
$result = $this->strategy->execute($context, $endNode);
|
|
$this->assertSuccess($result);
|
|
}
|
|
|
|
/**
|
|
* 测试手动设置步骤时长
|
|
*/
|
|
public function testSetStepDuration(): void
|
|
{
|
|
$this->strategy->setStepDuration('清洗', 999);
|
|
|
|
$context = $this->createContext(['currentStep' => '清洗']);
|
|
$this->setStepTime($context, '清洗', 900); // 900s < 999s
|
|
|
|
$result = $this->strategy->execute($context, $this->node);
|
|
|
|
$this->assertFailure($result);
|
|
$this->assertStringContainsString('99秒', $result->errorMessage); // 999-900=99
|
|
}
|
|
|
|
/**
|
|
* 测试策略名称
|
|
*/
|
|
public function testStrategyName(): void
|
|
{
|
|
$this->assertEquals('时间验证策略', $this->strategy->getName());
|
|
}
|
|
|
|
/**
|
|
* 测试策略执行阶段
|
|
*/
|
|
public function testStrategyPhase(): void
|
|
{
|
|
$this->assertEquals('before', $this->strategy->getPhase());
|
|
}
|
|
}
|