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.
Hi Kryten0807, Thanks for the reply. That really helps.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community