flo's avatar
Level 5

How to test commands

Currently I'm trying to find out what the best way would be to test a command.

I have tried creating an example below. But I'm clueless on how to test this whole process to make sure this functionality will always work.

// SubscribersController@store
$this->execute(SignupSubscriberCommand::class);
// SignupSubscriberCommandHandler@handle
$subscriber = Subscriber::signup($command->mailinglistId, $command->email);

$this->dispatchEventsFor($subscriber);

return $subscriber;
// Subscriber@signup (Eloquent model)
$confirmation_code = str_random(32);

$subscriber = static::create(compact('mailinglist_id', 'email', 'confirmation_code'));

$subscriber->raise(new SubscriberWasSignedUp($subscriber));

return $subscriber;
// EmailNotifier@whenSubscriberWasSignedUp
$subscriber = $event->subscriber;

$data = [
      'subscriber' => $subscriber,
];

$mail = Mail::send('emails.subscribers.signup', $data, function($m) use ($subscriber){
    $m->to($subscriber->email)
    ->subject('Confirm your subscription');
});
0 likes
4 replies
leitom's avatar

I think that If you use a functional test It should cover it.

flo's avatar
Level 5

@leitom Thanks, but wouldn't I then only be testing if the response is correct according to the input? Nothing in between?

Maybe you could give me one or two examples on how to make sure this is tested safe.

flo's avatar
Level 5

@faisal_arbain but would you suggest using something like Codeception to test this full functionality? (and maybe even test parts of it with separate tests)?

Please or to participate in this conversation.