ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Symfony 命令行测试如何写:LiipFunctionalTestBundle 的 runCommand 实战速成

Symfony 命令行测试如何写:LiipFunctionalTestBundle 的 runCommand 实战速成 Symfony 命令行测试如何写LiipFunctionalTestBundle 的 runCommand 实战速成【免费下载链接】nuclei-templatesCommunity curated list of templates for the nuclei engine to find security vulnerabilities.项目地址: https://gitcode.com/GitHub_Trending/nu/nuclei-templatesLiipFunctionalTestBundle 是 Symfony 功能测试的辅助库它继承WebTestCase用一行runCommand()就能执行控制台命令并拿到可断言的结果正好解决命令行测试启动内核、查命令、跑测试的繁琐样板。从痛点到解法别再手动拼 CommandTester 了不用这个库时你在测试里要做一整套手工活new Application($kernel)启动内核、$command $application-find(...)查命令、再CommandTester-execute()执行、还得自己处理输出捕获。任何一步写错都是排查成本。LiipFunctionalTestBundle 把这整条链路封装进了runCommand()内核启动、命令查找、执行、结果捕获全部自动完成。你只需要传命令名拿回一个CommandTester对象去断言。命令测试文档见 doc/command.md。一行 runCommand 快速上手use Liip\FunctionalTestBundle\Test\WebTestCase; class MyCommandTest extends WebTestCase { public function testMyCommand(): void { $tester $this-runCommand(app:report, [--force true]); $this-assertStringContainsString(Report generated, $tester-getDisplay()); $this-assertSame(0, $tester-getStatusCode()); } }三个参数各司其职命令名如app:report必填参数数组选项、位置参数都行[--force true]这种写法即可可省略$reuseKernel同一测试内多次执行命令时传true复用已启动的内核速度明显变快五档 verbosity 对照表quiet 到 debug 怎么选命令输出的详细程度由verbosity级别控制runCommand()默认以normal执行。Symfony Console 共五档级别常量对应参数适用场景quietVERBOSITY_QUIET-q只看成败输出全静音normalVERBOSITY_NORMAL默认常规信息日常断言够用verboseVERBOSITY_VERBOSE-v需要额外说明信息时very_verboseVERBOSITY_VERY_VERBOSE-vv输出更细粒度的日志debugVERBOSITY_DEBUG-vvv排障拿全部调试输出 很多命令的关键日志SQL、请求体等只在verbose以上才打印。测试断言不到内容时先想是不是级别不够。团队级默认在 test 环境 YAML 里配一次当整个团队希望命令测试默认输出更详细或更安静时改一处全局生效# config/packages/test/liip_functional_test.yaml liip_functional_test: command_verbosity: debugcommand_verbosity默认值是normal配置节点定义在 Configuration.php。合法取值只有上面表格里那五个写错会出问题见文末避坑清单。单条测试微调verbosityLevel 与 isDecorated 局部覆盖全局配置管面单条测试管点。只想让某一条测试开调试输出时在方法内设置verbosityLevel属性或调用更规范的setVerbosityLevel()public function testDebugOutput(): void { $this-setVerbosityLevel(debug); $tester $this-runCommand(app:report); $this-assertStringContainsString(debug detail, $tester-getDisplay()); }颜色输出是另一个常见干扰项。runCommand()默认带 ANSI 装饰command_decoration默认为true在测试日志里就是一堆转义字符。想关掉全局配command_decoration: false或单条测试调isDecorated(false)。模拟交互输入setInputs 代替键盘命令里若有QuestionHelper提问确认删除、填参数测试可以按顺序模拟键盘输入public function testInteractiveCommand(): void { $this-setInputs([yes, adminexample.com]); $tester $this-runCommand(app:setup); $this-assertStringContainsString(initialized, $tester-getDisplay()); }设置了输入后runCommand()会自动把命令置为interactive模式。完整示例可参考 tests/Command/CommandTest.php 中的testRunCommandWithInputs。断言最佳实践退出码 输出内容两个都查runCommand()返回的CommandTester有两个高频断言方法getStatusCode()命令退出码0成功、非零失败getDisplay()命令全部输出文本只断言输出内容有个隐蔽的坑命令失败时也可能打印看起来正常的部分文本测试照样通过。而退出码是最廉价的正确性信号。所以推荐组合拳$this-assertSame(0, $tester-getStatusCode()); $this-assertStringContainsString(Report generated, $tester-getDisplay());退出码防悄悄失败输出内容防失败了但恰好有同文案双保险缺一不可。避坑清单⚠️非法级别直接抛异常verbosityLevel传入quiet/normal/verbose/very_verbose/debug之外的值比如foobargetVerbosityLevel()会抛OutOfBoundsException。示例测试testRunCommandVerbosityOutOfBound就是在验证这一点拼写务必核对。复用内核要传第三个参数同一测试里连跑多条命令时$this-runCommand(..., [...], true)省去重复启动内核的开销。交互输入用完即清setInputs()的输入在首次执行后自动清空下一条runCommand()回到默认值。连续两条交互命令测试要各自重新setInputs别指望续上。CI 环境关掉装饰配command_decoration: false否则日志文件里全是 ANSI 转义乱码。别整文件粘贴测试CommandTester只给当前这一次执行的结果每条测试自己调runCommand()靠$reuseKernel提速而不是共享 tester。小结runCommand()把 Symfony 命令行测试压缩成调命令、断退出码和输出三步command_verbosity/command_decoration管全局setVerbosityLevel()/isDecorated()/setInputs()管局部组合起来就覆盖了绝大多数场景。有疑问时直接看这三处源码比翻文档更快WebTestCase 实现、Configuration 配置节点、示例测试。【免费下载链接】nuclei-templatesCommunity curated list of templates for the nuclei engine to find security vulnerabilities.项目地址: https://gitcode.com/GitHub_Trending/nu/nuclei-templates创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表