簡體   English   中英

如何對依賴於日期的 Symfony 命令進行 Behat 測試?

[英]How to Behat test for date dependent Symfony command?

我運行了一個適用於上個月交易的 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 服務是一種矯枉過正。

最好編寫自己的步驟定義

/**
 * @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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM