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导入应该被移除,因为不再使用'); } }