Skip to content

Instantly share code, notes, and snippets.

@deividaspetraitis
Created November 16, 2017 18:26
Show Gist options
  • Save deividaspetraitis/1ca48867aededff73812c72b834b1a48 to your computer and use it in GitHub Desktop.
Save deividaspetraitis/1ca48867aededff73812c72b834b1a48 to your computer and use it in GitHub Desktop.
Laravel Fake Mailable Fails After Few Emails Inside Application Are Sent.
<?php
class Test
{
/**
* Api Helper
*
* @var \Helper\Api
*/
protected $api;
/**
* Module repository
*
* @var $repository
*/
protected $repository;
/**
* Load required helpers for cest
*
* @param \Helper\Api $api
*/
protected function _inject(\Helper\Api $api)
{
$this->api = $api;
}
/**
* Return module repository
*
* @return UserRepository
*/
public function _getRepository() : UserRepository
{
return $this->api->getApplication()->make('Modules\Users\Services\User\UserService')->getUserRepository();
}
/**
* Actions executed before cest
*
* @param ApiTester $I
*/
public function _before(ApiTester $I)
{
}
/**
* Actions executed after cest
*
* @param ApiTester $I
*/
public function _after(ApiTester $I)
{
}
/**
* Test user activation
*
* @param ApiTester $I
*
* @return void
*/
public function test(ApiTester $I) : void
{
// Prevent actual email being sent
Mail::fake();
// Do some stuff
// Email 1 inside application is sent..
Mail::assertSent(Action1Mail::class, function (Action1Maill $mail) {
return $mail->getVal() == 1;
});
// Do some stuff
// Email 2 inside application is sent..
// Boom, error: [BadMethodCallException] Method assertSent does not exist.
// Because: get_class(app('mailer')) == Illuminate\Mail\Mailer
// And no more: get_class(app('mailer')) == Illuminate\Support\Testing\Fakes\MailFake
Mail::assertSent(Action2Mail::class, function (Action2Mail $mail) {
return $mail->getVal() == 2;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment