我运行一个处理上个月事务的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服务总是返回当前月份,这是一种夸张的做法。
发布于 2020-11-19 17:08:29
更好的write your own step definition
/**
* @Given /^There is a transaction "([^"]*)" for last month$/
*/
public function thereIsATransaction(string $transactionId) {}这样,您可以简单地执行以下操作:
Given there is a transaction "7ccf7387" for last month
And there is a transaction "39d7f278" for last monthhttps://stackoverflow.com/questions/64893707
复制相似问题