【问题标题】:How to Behat test for date dependent Symfony command?如何对依赖日期的 Symfony 命令进行 Behat 测试?
【发布时间】: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 月固定)?

标签: php symfony testing behat


【解决方案1】:

更好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 month

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 2017-12-02
    相关资源
    最近更新 更多