Support the ongoing development of Laravel.io →
posted 10 years ago
Testing
Last updated 2 years ago.
0

I did it with no problems. For example, I have a command that is run periodically via a CRON job and sends an email report

class SendEmailCommandTest extends TestCase {

    public function tearDown()
    {
        Mockery::close();
    }

    public function test_fire()
    {
        // given
        //
        $cmd = Mockery::mock("\MyApp\Commands\SendEmailCommand[argument, send]");

        $cmd->shouldReceive("argument")
            ->with("name")
            ->once()
            ->andReturn("my-email-name");

        $cmd->shouldReceive("send")
            ->with("my-email-name")
            ->once();

        // when
        //
        $cmd->fire();

        // then
        //
        $this->assertTrue(true);
    }

}

The test works by creating a partial mock of the command class, mocking the argument and send methods. The argument method is the Laravel method that returns a command line argument - here the mock just returns a value. The send method is a method I wrote to do the work (basically it just calls the send method on a library class that's tested elsewhere). The fire method doesn't return any values, so I included a "dummy" test at the end - the real test ensures that the appropriate methods are called with the right arguments.

Last updated 2 years ago.
0

Hi Kryten0807, Thanks for the reply. That really helps.

Last updated 2 years ago.
0

Sweet. A year later I found this and it really helped me. Thanks @Kryten0807.

0

Thank you very much. I was also searching for this :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

amalfra amalfra Joined 10 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.