Skip to content

Commit

Permalink
[8.x] Allow setting middleware on queued Mailables (#37568)
Browse files Browse the repository at this point in the history
* allow setting middleware on queued mailables

* add tests

* fix style
  • Loading branch information
themsaid authored Jun 2, 2021
1 parent 9e23c74 commit 3f41e97
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ public function later($delay, Queue $queue)
*/
protected function newQueuedJob()
{
return new SendQueuedMailable($this);
return (new SendQueuedMailable($this))
->through(
array_merge(
method_exists($this, 'middleware') ? $this->middleware() : [],
$this->middleware ?? []
)
);
}

/**
Expand Down
53 changes: 53 additions & 0 deletions tests/Integration/Mail/SendingQueuedMailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Illuminate\Tests\Integration\Mail;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\SendQueuedMailable;
use Illuminate\Queue\Middleware\RateLimited;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\View;
use Orchestra\Testbench\TestCase;

/**
* @group integration
*/
class SendingQueuedMailTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('mail.driver', 'array');

View::addLocation(__DIR__.'/Fixtures');
}

public function testMailIsSentWithDefaultLocale()
{
Queue::fake();

Mail::to('test@mail.com')->queue(new SendingQueuedMailTestMail);

Queue::assertPushed(SendQueuedMailable::class, function ($job) {
return $job->middleware[0] instanceof RateLimited;
});
}
}

class SendingQueuedMailTestMail extends Mailable
{
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('view');
}

public function middleware()
{
return [new RateLimited('limiter')];
}
}

0 comments on commit 3f41e97

Please sign in to comment.