| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace Tests\Unit\Farm;
- use PHPUnit\Framework\TestCase;
- /**
- * 测试灾害生成时土地状态不应该被修改
- *
- * 这是一个简单的单元测试,验证修复后的代码逻辑
- */
- class DisasterLandStatusTest extends TestCase
- {
- /**
- * 测试applyDisastersToCrop方法不再修改土地状态
- *
- * 这个测试验证修复后的代码确实移除了对土地状态的修改
- */
- public function test_apply_disasters_to_crop_method_exists_and_is_modified()
- {
- // 检查DisasterLogic类是否存在
- $this->assertTrue(class_exists('\App\Module\Farm\Logics\DisasterLogic'), 'DisasterLogic类应该存在');
- // 检查applyDisastersToCrop方法是否存在
- $reflection = new \ReflectionClass('\App\Module\Farm\Logics\DisasterLogic');
- $this->assertTrue($reflection->hasMethod('applyDisastersToCrop'), 'applyDisastersToCrop方法应该存在');
- // 获取方法并检查其源码
- $method = $reflection->getMethod('applyDisastersToCrop');
- $filename = $reflection->getFileName();
- $startLine = $method->getStartLine();
- $endLine = $method->getEndLine();
- // 读取方法源码
- $lines = file($filename);
- $methodCode = implode('', array_slice($lines, $startLine - 1, $endLine - $startLine + 1));
- // 验证修复:不应该包含设置土地状态为DISASTER的代码
- $this->assertStringNotContainsString('LAND_STATUS::DISASTER', $methodCode,
- '方法中不应该包含设置土地状态为DISASTER的代码');
- $this->assertStringNotContainsString('$land->status = ', $methodCode,
- '方法中不应该包含直接设置土地状态的代码');
- // 验证修复:应该包含说明注释
- $this->assertStringContainsString('作物灾害与土地无关', $methodCode,
- '方法中应该包含说明作物灾害与土地无关的注释');
- // 验证修复:应该只保存作物,不保存土地
- $this->assertStringContainsString('$crop->save()', $methodCode,
- '方法中应该保存作物');
- $this->assertStringNotContainsString('$land->save()', $methodCode,
- '方法中不应该保存土地');
- }
- /**
- * 测试方法签名没有改变
- */
- public function test_method_signature_unchanged()
- {
- $reflection = new \ReflectionClass('\App\Module\Farm\Logics\DisasterLogic');
- $method = $reflection->getMethod('applyDisastersToCrop');
- // 检查方法是否为私有方法
- $this->assertTrue($method->isPrivate(), 'applyDisastersToCrop应该是私有方法');
- // 检查参数数量
- $this->assertEquals(2, $method->getNumberOfParameters(), '方法应该有2个参数');
- // 检查返回类型
- $returnType = $method->getReturnType();
- $this->assertEquals('array', $returnType->getName(), '方法应该返回数组');
- }
- /**
- * 测试相关的导入语句已被清理
- */
- public function test_unused_imports_removed()
- {
- $filename = (new \ReflectionClass('\App\Module\Farm\Logics\DisasterLogic'))->getFileName();
- $content = file_get_contents($filename);
- // 验证LAND_STATUS导入已被移除(因为不再使用)
- $this->assertStringNotContainsString('use App\Module\Farm\Enums\LAND_STATUS;', $content,
- 'LAND_STATUS导入应该被移除,因为不再使用');
- }
- }
|