【发布时间】:2021-03-23 21:03:51
【问题描述】:
我想从工匠命令输出到控制台。这在很长的调用堆栈中很深,因此不适合使用 $this->error('My warning');。
网上有一些解决方案建议这样做。
$out = new ConsoleOutput();
$out->writeln('My warning');
这解决了问题,但是当我运行phpunit 时,我的命令输出会干扰phpunit 的输出
vendor/bin/phpunit --filter=MyCmdTest
PHPUnit 9.3.10 by Sebastian Bergmann and contributors.
Warning: Your XML configuration validates against a deprecated schema.
Suggestion: Migrate your XML configuration using "--migrate-configuration"!
My warning
.
Time: 00:17.182, Memory: 56.50 MB
OK (1 test, 3 assertions)
在工匠命令深处将输出写入 cmd 行的最佳方式是什么。不影响PHPUnit的输出?
编辑
我是这样调用命令的。
$this->artisan('my:cmd')
->assertExitCode(0);
使用通常的$this->error() 的问题是我在 artisan 命令中做了类似的事情。
class MyCmd extends Command {
public function handle(MyService $service) {
$service->doStuff();
}
}
class MyService {
public function doStuff() {
if (true) {
// print a warning when the state is bad
}
}
}
服务被过度简化,因为服务内部的逻辑可能非常复杂,并且传递 artisan 命令的引用似乎是错误的方法。这也意味着我必须将它传递给多个服务和/或模型逻辑等。
【问题讨论】:
-
我假设在工匠 cmd 中使用 $this->error() 不会写入 PHPUnit,所以我想一定有办法吗?
-
它不会在 PHPUnit 中打印输出,但是当您运行 artisan 命令时会这样做,我想这可以回答吗?这也是一个问题,我如何在我的代码中相当深入地使用像 $this->error 这样的工匠助手而不通过整个调用堆栈传递引用
-
与您在命令上下文中所做的完全一样,但在应用程序中逻辑更深,希望它有意义
-
您能否编辑您的问题以包括您如何在测试中调用该命令,并扩展您认为
$this->error()或$this->info()会产生哪些问题? -
我已经编辑了这个问题,这是一个相当小众和复杂的案例,我只是觉得这一定是以前出现过的。
标签: php laravel laravel-artisan