123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- <?php
- namespace Illuminate\Tests\Integration\Notifications;
- use Illuminate\Contracts\Mail\Factory as MailFactory;
- use Illuminate\Contracts\Mail\Mailable;
- use Illuminate\Contracts\Mail\Mailer;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Mail\Markdown;
- use Illuminate\Mail\Message;
- use Illuminate\Notifications\Channels\MailChannel;
- use Illuminate\Notifications\Messages\MailMessage;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Notifications\Notification;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Support\Facades\View;
- use Illuminate\Support\Str;
- use Mockery as m;
- use Orchestra\Testbench\TestCase;
- class SendingMailNotificationsTest extends TestCase
- {
- public $mailer;
- public $markdown;
- protected function tearDown(): void
- {
- parent::tearDown();
- m::close();
- }
- protected function getEnvironmentSetUp($app)
- {
- $this->mailFactory = m::mock(MailFactory::class);
- $this->mailer = m::mock(Mailer::class);
- $this->mailFactory->shouldReceive('mailer')->andReturn($this->mailer);
- $this->markdown = m::mock(Markdown::class);
- $app->extend(Markdown::class, function () {
- return $this->markdown;
- });
- $app->extend(Mailer::class, function () {
- return $this->mailer;
- });
- $app->extend(MailFactory::class, function () {
- return $this->mailFactory;
- });
- View::addLocation(__DIR__.'/Fixtures');
- }
- protected function setUp(): void
- {
- parent::setUp();
- Schema::create('users', function (Blueprint $table) {
- $table->increments('id');
- $table->string('email');
- $table->string('name')->nullable();
- });
- }
- public function testMailIsSent()
- {
- $notification = new TestMailNotification;
- $notification->id = Str::uuid()->toString();
- $user = NotifiableUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- ]);
- $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
- $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
- $this->mailer->shouldReceive('send')->once()->with(
- ['html' => 'htmlContent', 'text' => 'textContent'],
- array_merge($notification->toMail($user)->toArray(), [
- '__laravel_notification_id' => $notification->id,
- '__laravel_notification' => get_class($notification),
- '__laravel_notification_queued' => false,
- ]),
- m::on(function ($closure) {
- $message = m::mock(Message::class);
- $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
- $message->shouldReceive('cc')->once()->with('cc@deepblue.com', 'cc');
- $message->shouldReceive('bcc')->once()->with('bcc@deepblue.com', 'bcc');
- $message->shouldReceive('from')->once()->with('jack@deepblue.com', 'Jacques Mayol');
- $message->shouldReceive('replyTo')->once()->with('jack@deepblue.com', 'Jacques Mayol');
- $message->shouldReceive('subject')->once()->with('Test Mail Notification');
- $message->shouldReceive('setPriority')->once()->with(1);
- $closure($message);
- return true;
- })
- );
- $user->notify($notification);
- }
- public function testMailIsSentToNamedAddress()
- {
- $notification = new TestMailNotification;
- $notification->id = Str::uuid()->toString();
- $user = NotifiableUserWithNamedAddress::forceCreate([
- 'email' => 'taylor@laravel.com',
- 'name' => 'Taylor Otwell',
- ]);
- $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
- $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
- $this->mailer->shouldReceive('send')->once()->with(
- ['html' => 'htmlContent', 'text' => 'textContent'],
- array_merge($notification->toMail($user)->toArray(), [
- '__laravel_notification_id' => $notification->id,
- '__laravel_notification' => get_class($notification),
- '__laravel_notification_queued' => false,
- ]),
- m::on(function ($closure) {
- $message = m::mock(Message::class);
- $message->shouldReceive('to')->once()->with(['taylor@laravel.com' => 'Taylor Otwell', 'foo_taylor@laravel.com']);
- $message->shouldReceive('cc')->once()->with('cc@deepblue.com', 'cc');
- $message->shouldReceive('bcc')->once()->with('bcc@deepblue.com', 'bcc');
- $message->shouldReceive('from')->once()->with('jack@deepblue.com', 'Jacques Mayol');
- $message->shouldReceive('replyTo')->once()->with('jack@deepblue.com', 'Jacques Mayol');
- $message->shouldReceive('subject')->once()->with('Test Mail Notification');
- $message->shouldReceive('setPriority')->once()->with(1);
- $closure($message);
- return true;
- })
- );
- $user->notify($notification);
- }
- public function testMailIsSentWithSubject()
- {
- $notification = new TestMailNotificationWithSubject;
- $notification->id = Str::uuid()->toString();
- $user = NotifiableUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- ]);
- $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
- $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
- $this->mailer->shouldReceive('send')->once()->with(
- ['html' => 'htmlContent', 'text' => 'textContent'],
- array_merge($notification->toMail($user)->toArray(), [
- '__laravel_notification_id' => $notification->id,
- '__laravel_notification' => get_class($notification),
- '__laravel_notification_queued' => false,
- ]),
- m::on(function ($closure) {
- $message = m::mock(Message::class);
- $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
- $message->shouldReceive('subject')->once()->with('mail custom subject');
- $closure($message);
- return true;
- })
- );
- $user->notify($notification);
- }
- public function testMailIsSentToMultipleAddresses()
- {
- $notification = new TestMailNotificationWithSubject;
- $notification->id = Str::uuid()->toString();
- $user = NotifiableUserWithMultipleAddresses::forceCreate([
- 'email' => 'taylor@laravel.com',
- ]);
- $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');
- $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');
- $this->mailer->shouldReceive('send')->once()->with(
- ['html' => 'htmlContent', 'text' => 'textContent'],
- array_merge($notification->toMail($user)->toArray(), [
- '__laravel_notification_id' => $notification->id,
- '__laravel_notification' => get_class($notification),
- '__laravel_notification_queued' => false,
- ]),
- m::on(function ($closure) {
- $message = m::mock(Message::class);
- $message->shouldReceive('to')->once()->with(['foo_taylor@laravel.com', 'bar_taylor@laravel.com']);
- $message->shouldReceive('subject')->once()->with('mail custom subject');
- $closure($message);
- return true;
- })
- );
- $user->notify($notification);
- }
- public function testMailIsSentUsingMailable()
- {
- $notification = new TestMailNotificationWithMailable;
- $user = NotifiableUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- ]);
- $user->notify($notification);
- }
- public function testMailIsSentUsingMailMessageWithHtmlAndPlain()
- {
- $notification = new TestMailNotificationWithHtmlAndPlain;
- $notification->id = Str::uuid()->toString();
- $user = NotifiableUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- ]);
- $this->mailer->shouldReceive('send')->once()->with(
- ['html', 'plain'],
- array_merge($notification->toMail($user)->toArray(), [
- '__laravel_notification_id' => $notification->id,
- '__laravel_notification' => get_class($notification),
- '__laravel_notification_queued' => false,
- ]),
- m::on(function ($closure) {
- $message = m::mock(Message::class);
- $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
- $message->shouldReceive('subject')->once()->with('Test Mail Notification With Html And Plain');
- $closure($message);
- return true;
- })
- );
- $user->notify($notification);
- }
- public function testMailIsSentUsingMailMessageWithHtmlOnly()
- {
- $notification = new TestMailNotificationWithHtmlOnly;
- $notification->id = Str::uuid()->toString();
- $user = NotifiableUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- ]);
- $this->mailer->shouldReceive('send')->once()->with(
- 'html',
- array_merge($notification->toMail($user)->toArray(), [
- '__laravel_notification_id' => $notification->id,
- '__laravel_notification' => get_class($notification),
- '__laravel_notification_queued' => false,
- ]),
- m::on(function ($closure) {
- $message = m::mock(Message::class);
- $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
- $message->shouldReceive('subject')->once()->with('Test Mail Notification With Html Only');
- $closure($message);
- return true;
- })
- );
- $user->notify($notification);
- }
- public function testMailIsSentUsingMailMessageWithPlainOnly()
- {
- $notification = new TestMailNotificationWithPlainOnly;
- $notification->id = Str::uuid()->toString();
- $user = NotifiableUser::forceCreate([
- 'email' => 'taylor@laravel.com',
- ]);
- $this->mailer->shouldReceive('send')->once()->with(
- [null, 'plain'],
- array_merge($notification->toMail($user)->toArray(), [
- '__laravel_notification_id' => $notification->id,
- '__laravel_notification' => get_class($notification),
- '__laravel_notification_queued' => false,
- ]),
- m::on(function ($closure) {
- $message = m::mock(Message::class);
- $message->shouldReceive('to')->once()->with(['taylor@laravel.com']);
- $message->shouldReceive('subject')->once()->with('Test Mail Notification With Plain Only');
- $closure($message);
- return true;
- })
- );
- $user->notify($notification);
- }
- }
- class NotifiableUser extends Model
- {
- use Notifiable;
- public $table = 'users';
- public $timestamps = false;
- }
- class NotifiableUserWithNamedAddress extends NotifiableUser
- {
- public function routeNotificationForMail($notification)
- {
- return [
- $this->email => $this->name,
- 'foo_'.$this->email,
- ];
- }
- }
- class NotifiableUserWithMultipleAddresses extends NotifiableUser
- {
- public function routeNotificationForMail($notification)
- {
- return [
- 'foo_'.$this->email,
- 'bar_'.$this->email,
- ];
- }
- }
- class TestMailNotification extends Notification
- {
- public function via($notifiable)
- {
- return [MailChannel::class];
- }
- public function toMail($notifiable)
- {
- return (new MailMessage)
- ->priority(1)
- ->cc('cc@deepblue.com', 'cc')
- ->bcc('bcc@deepblue.com', 'bcc')
- ->from('jack@deepblue.com', 'Jacques Mayol')
- ->replyTo('jack@deepblue.com', 'Jacques Mayol')
- ->line('The introduction to the notification.')
- ->mailer('foo');
- }
- }
- class TestMailNotificationWithSubject extends Notification
- {
- public function via($notifiable)
- {
- return [MailChannel::class];
- }
- public function toMail($notifiable)
- {
- return (new MailMessage)
- ->subject('mail custom subject')
- ->line('The introduction to the notification.');
- }
- }
- class TestMailNotificationWithMailable extends Notification
- {
- public function via($notifiable)
- {
- return [MailChannel::class];
- }
- public function toMail($notifiable)
- {
- $mailable = m::mock(Mailable::class);
- $mailable->shouldReceive('send')->once();
- return $mailable;
- }
- }
- class TestMailNotificationWithHtmlAndPlain extends Notification
- {
- public function via($notifiable)
- {
- return [MailChannel::class];
- }
- public function toMail($notifiable)
- {
- return (new MailMessage)
- ->view(['html', 'plain']);
- }
- }
- class TestMailNotificationWithHtmlOnly extends Notification
- {
- public function via($notifiable)
- {
- return [MailChannel::class];
- }
- public function toMail($notifiable)
- {
- return (new MailMessage)
- ->view('html');
- }
- }
- class TestMailNotificationWithPlainOnly extends Notification
- {
- public function via($notifiable)
- {
- return [MailChannel::class];
- }
- public function toMail($notifiable)
- {
- return (new MailMessage)
- ->view([null, 'plain']);
- }
- }
|