schellingerht
schellingerht

Reputation: 5806

Assert Laravel Command is Called in Test

I call Laravel commands from a Laravel command. It works, but I want to test if the subcommands A and/or B are called.

Below a simplified version:

final class HandleActions extends Command
{
    public function handle()
    {
        foreach ($items as $item) {
            if ($item->price * 2.24 > 100) {
                $this->call('order:reprocess'); // command A
            }

            if (!$item->stock) {
                $this->call('order:cleanup'); // command B
            }
        }
    }
}

I test this command like so (simplified), so I know that the main command is excited successfully.

    $this->artisan('command')->assertExitCode(0);
    // How do I assert command A is fired?
    // How do I assert command B is fired?

I want to assert that subcommand 1 is fired and/or subcommand 2 fired. But how to do this?

Upvotes: 0

Views: 321

Answers (1)

levi
levi

Reputation: 25141

You could fire an event from the subcommands, and then assert in your test that the event(s) have fired, via Event::assertDispatched

Upvotes: 1

Related Questions