vendredi 17 mars 2017

Mockery: check argument is passed at least once?

I'm looking to do something that seems rather usual but can't find a built-in solution in Mockery.

An example test would be something like this:

<?php
$user = factory(App\User::class)->create(['birthday' => Carbon::now()->subYear(20)]);
$this->mailer->shouldReceive('sendBirthdayEvent')
             ->with(hasEntry("id", $user->id)); // Will fail if DB is seeded
$this->command->sendBirthdayEventToAllUSers();

I'd like to check that the sendBirthdayEvent will be call for the used I just created once. Problem is I can't use with because my database might be seeded at that point and other users could also have birthdays on the same day.

The 'smarter' solution I've thought of until now is to do something like this:

<?php
$success = false;
$this->mailer->shouldReceive('sendBirthdayEvent')
             ->with(function($user_id) use($success) {
    if($user_id) === $user->id { 
        $success = true:
    }
    return true;
});
assert($success,true);

But I feel like I'm really missing something here.

Aucun commentaire:

Enregistrer un commentaire