【发布时间】:2020-11-24 20:27:31
【问题描述】:
我运行了一个适用于上个月交易的 CLI Symfony 命令。这是 CLI - 我不能只是模拟系统时钟,那么如何使用 Behat 进行测试?现在是 11 月,以下场景将在 12 月失败:
Scenario: Do something with last month transactions
Given there is a transaction "7ccf7387" for 2020-10
And there is a transaction "39d7f278" for 2020-10
When I execute "bin/console billing:last-month" from project root
Then command should succeed
And output should contain "Closed 2 transactions"
这是当前的命令实现:
final class CloseLastMonth extends Command
{
/* ... */
protected function execute(InputInterface $input, OutputInterface $output): int
{
$month = (GregorianCalendar::UTC())->currentMonth()->previous();
$trxs = $this->bus->dispatch(new GetActiveTrxsByMonthQuery($month));
foreach ($trxs as $trx) {
$this->bus->dispatch(new CloseTransactionCommand($trx->id));
}
$this->io->success(sprintf('Closed %d transactions', count($trxs)));
return 0;
}
/* ... */
}
我可以强制用户总是在命令参数中传递当前月份,但这并不优雅。总是返回当前月份的新 Symfony 服务太过分了。
【问题讨论】:
-
为什么这在 12 月行不通?
-
@Nico 因为场景中的日期是 10 月。
-
啊,傻我.....那么,为什么不动态插入夹具(这样它们总是根据执行情况在最后一个月插入,而不是在 2020 年 10 月固定)?