首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对与日期相关的Symfony命令进行测试?

如何对与日期相关的Symfony命令进行测试?
EN

Stack Overflow用户
提问于 2020-11-18 21:05:25
回答 1查看 66关注 0票数 0

我运行一个处理上个月事务的CLI Symfony命令。它是CLI -我不能只是模拟系统时钟,所以我如何用Behat测试它?现在是11月份,下面的场景将在12月份失败:

代码语言:javascript
复制
  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"

这是当前的命令实现:

代码语言:javascript
复制
    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服务总是返回当前月份,这是一种夸张的做法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-19 17:08:29

更好的write your own step definition

代码语言:javascript
复制
/**
 * @Given /^There is a transaction "([^"]*)" for last month$/
 */
public function thereIsATransaction(string $transactionId) {}

这样,您可以简单地执行以下操作:

代码语言:javascript
复制
Given there is a transaction "7ccf7387" for last month
    And there is a transaction "39d7f278" for last month
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64893707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档