Skip to content

Commit 7f2c521

Browse files
committed
Merge remote-tracking branch 'upstream/master' into feature/manual-migrations
2 parents b7aba53 + 563c22a commit 7f2c521

8 files changed

+108
-4
lines changed

.github/workflows/run-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Run tests
22

33
on:
4-
push:
4+
pull_request:
55
schedule:
66
- cron: '0 0 * * *'
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddQueuedAtToEmailsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
if (Schema::hasColumn('emails', 'queued_at')) {
17+
return;
18+
}
19+
20+
Schema::table('emails', function (Blueprint $table) {
21+
$table->timestamp('queued_at')->after('encrypted')->nullable();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
//
33+
}
34+
}

src/Email.php

+25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @property $failed
2424
* @property $error
2525
* @property $encrypted
26+
* @property $queued_at
2627
* @property $scheduled_at
2728
* @property $sent_at
2829
* @property $delivered_at
@@ -277,6 +278,30 @@ public function getAttempts()
277278
return $this->attempts;
278279
}
279280

281+
/**
282+
* Get the queued date.
283+
*
284+
* @return mixed
285+
*/
286+
public function getQueuedDate()
287+
{
288+
return $this->queued_at;
289+
}
290+
291+
/**
292+
* Get the queued date as a Carbon instance.
293+
*
294+
* @return Carbon
295+
*/
296+
public function getQueuedDateAsCarbon()
297+
{
298+
if ($this->queued_at instanceof Carbon) {
299+
return $this->queued_at;
300+
}
301+
302+
return Carbon::parse($this->queued_at);
303+
}
304+
280305
/**
281306
* Get the scheduled date.
282307
*

src/Preparer.php

+17
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function prepare(EmailComposer $composer)
3636
$this->prepareScheduled($composer);
3737

3838
$this->prepareImmediately($composer);
39+
40+
$this->prepareQueued($composer);
3941
}
4042

4143
/**
@@ -235,4 +237,19 @@ private function prepareImmediately(EmailComposer $composer)
235237
$composer->getEmail()->fill(['sending' => 1]);
236238
}
237239
}
240+
241+
/**
242+
* Prepare the queued date.
243+
*
244+
* @param EmailComposer $composer
245+
*/
246+
private function prepareQueued(EmailComposer $composer)
247+
{
248+
if ($composer->getData('queued', false) === true) {
249+
$composer->getEmail()->fill([
250+
'queued_at' => Carbon::now()->toDateTimeString(),
251+
]);
252+
}
253+
}
254+
238255
}

src/Store.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function getQueue()
1919
return $query
2020
->whereNull('deleted_at')
2121
->whereNull('sent_at')
22+
->whereNull('queued_at')
2223
->where(function ($query) {
2324
$query->whereNull('scheduled_at')
2425
->orWhere('scheduled_at', '<=', Carbon::now()->toDateTimeString());

tests/MailableReaderTest.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,15 @@ public function it_extracts_the_from_address_and_or_name()
100100
->from(null, 'Marick')
101101
)->send();
102102

103-
$this->assertTrue($email->hasFrom());
104-
$this->assertEquals(config('mail.from.address'), $email->getFromAddress());
105-
$this->assertEquals('Marick', $email->getFromName());
103+
// 8.x no longer accepts an empty address.
104+
// https://github.com/laravel/framework/pull/39035
105+
if (version_compare(app()->version(), '8.0.0', '>=')) {
106+
$this->assertFalse($email->hasFrom());
107+
} else {
108+
$this->assertTrue($email->hasFrom());
109+
$this->assertEquals(config('mail.from.address'), $email->getFromAddress());
110+
$this->assertEquals('Marick', $email->getFromName());
111+
}
106112
}
107113
}
108114

tests/QueuedEmailsTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public function queueing_an_email_will_leave_sending_on_false()
2121
$this->assertEquals(0, $email->sending);
2222
}
2323

24+
/** @test */
25+
public function queueing_an_email_will_set_the_queued_at_column()
26+
{
27+
$email = $this->queueEmail();
28+
29+
$this->assertNotNull($email->queued_at);
30+
}
31+
2432
/** @test */
2533
public function queueing_an_email_will_dispatch_a_job()
2634
{

tests/SendEmailsCommandTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\Carbon;
66
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\Queue;
78
use Stackkit\LaravelDatabaseEmails\Store;
89

910
class SendEmailsCommandTest extends TestCase
@@ -45,6 +46,18 @@ public function an_email_should_not_be_sent_once_it_is_marked_as_sent()
4546
$this->assertEquals($firstSend, $email->fresh()->getSendDate());
4647
}
4748

49+
/** @test */
50+
public function an_email_should_not_be_sent_if_it_is_queued()
51+
{
52+
Queue::fake();
53+
54+
$email = $this->queueEmail();
55+
56+
$this->artisan('email:send');
57+
58+
$this->assertNull($email->fresh()->getSendDate());
59+
}
60+
4861
/** @test */
4962
public function if_an_email_fails_to_be_sent_it_should_be_logged_in_the_database()
5063
{

0 commit comments

Comments
 (0)