DisasterLandStatusTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Tests\Unit\Farm;
  3. use PHPUnit\Framework\TestCase;
  4. /**
  5. * 测试灾害生成时土地状态不应该被修改
  6. *
  7. * 这是一个简单的单元测试,验证修复后的代码逻辑
  8. */
  9. class DisasterLandStatusTest extends TestCase
  10. {
  11. /**
  12. * 测试applyDisastersToCrop方法不再修改土地状态
  13. *
  14. * 这个测试验证修复后的代码确实移除了对土地状态的修改
  15. */
  16. public function test_apply_disasters_to_crop_method_exists_and_is_modified()
  17. {
  18. // 检查DisasterLogic类是否存在
  19. $this->assertTrue(class_exists('\App\Module\Farm\Logics\DisasterLogic'), 'DisasterLogic类应该存在');
  20. // 检查applyDisastersToCrop方法是否存在
  21. $reflection = new \ReflectionClass('\App\Module\Farm\Logics\DisasterLogic');
  22. $this->assertTrue($reflection->hasMethod('applyDisastersToCrop'), 'applyDisastersToCrop方法应该存在');
  23. // 获取方法并检查其源码
  24. $method = $reflection->getMethod('applyDisastersToCrop');
  25. $filename = $reflection->getFileName();
  26. $startLine = $method->getStartLine();
  27. $endLine = $method->getEndLine();
  28. // 读取方法源码
  29. $lines = file($filename);
  30. $methodCode = implode('', array_slice($lines, $startLine - 1, $endLine - $startLine + 1));
  31. // 验证修复:不应该包含设置土地状态为DISASTER的代码
  32. $this->assertStringNotContainsString('LAND_STATUS::DISASTER', $methodCode,
  33. '方法中不应该包含设置土地状态为DISASTER的代码');
  34. $this->assertStringNotContainsString('$land->status = ', $methodCode,
  35. '方法中不应该包含直接设置土地状态的代码');
  36. // 验证修复:应该包含说明注释
  37. $this->assertStringContainsString('作物灾害与土地无关', $methodCode,
  38. '方法中应该包含说明作物灾害与土地无关的注释');
  39. // 验证修复:应该只保存作物,不保存土地
  40. $this->assertStringContainsString('$crop->save()', $methodCode,
  41. '方法中应该保存作物');
  42. $this->assertStringNotContainsString('$land->save()', $methodCode,
  43. '方法中不应该保存土地');
  44. }
  45. /**
  46. * 测试方法签名没有改变
  47. */
  48. public function test_method_signature_unchanged()
  49. {
  50. $reflection = new \ReflectionClass('\App\Module\Farm\Logics\DisasterLogic');
  51. $method = $reflection->getMethod('applyDisastersToCrop');
  52. // 检查方法是否为私有方法
  53. $this->assertTrue($method->isPrivate(), 'applyDisastersToCrop应该是私有方法');
  54. // 检查参数数量
  55. $this->assertEquals(2, $method->getNumberOfParameters(), '方法应该有2个参数');
  56. // 检查返回类型
  57. $returnType = $method->getReturnType();
  58. $this->assertEquals('array', $returnType->getName(), '方法应该返回数组');
  59. }
  60. /**
  61. * 测试相关的导入语句已被清理
  62. */
  63. public function test_unused_imports_removed()
  64. {
  65. $filename = (new \ReflectionClass('\App\Module\Farm\Logics\DisasterLogic'))->getFileName();
  66. $content = file_get_contents($filename);
  67. // 验证LAND_STATUS导入已被移除(因为不再使用)
  68. $this->assertStringNotContainsString('use App\Module\Farm\Enums\LAND_STATUS;', $content,
  69. 'LAND_STATUS导入应该被移除,因为不再使用');
  70. }
  71. }