How to Behat test for date dependent Symfony command

  behat, date, php, symfony, testing

I run a CLI Symfony command that works on last month transactions. I can’t think of a way to test it with Behat. Following scenario works but won’t work in December:

  Scenario: Do something with last month transactions
    Given there is a transaction "7ccf7387-d45b-4aab-ba52-f25357e4cf75" for 2020-10
    And there is a transaction "39d7f278-0687-4ded-a283-4263e81ec09e" for 2020-10
    When I execute "bin/console bank:billings:close-last-month" from project root
    Then command should succeed
    And output should contain "Found 2 transactions"
    And output should contain "[OK] Done"

This is current command implementation:


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));
        $this->io->comment(sprintf('Found %d active billing periods.', count($trxs)));
        foreach ($trxs as $trx) {
            $this->bus->dispatch(new CloseTransactionCommand($trx->id));

        return 0;

    /* ... */

I could rewrite it so it’d take an argument and always pass it a current month but I find it inelegant.

I could register a new Symfony service that always returns current month, inject it in the constructor and mock it in behat context but it seems somehow too complicated for such a trivial task.

Any better solution?

Source: Symfony Questions