From 4efa937d438ebe6b323a4210e433151df97b9cdf Mon Sep 17 00:00:00 2001 From: Victor Kislichenko Date: Thu, 2 Dec 2021 21:36:53 +0200 Subject: [PATCH 1/4] Email: queued_at field (populated if email is queued by email composer) --- ...02_21000_add_queued_at_to_emails_table.php | 34 +++++++++++++++++++ src/Email.php | 25 ++++++++++++++ src/Preparer.php | 17 ++++++++++ src/Store.php | 1 + 4 files changed, 77 insertions(+) create mode 100644 database/migrations/2021_12_02_21000_add_queued_at_to_emails_table.php diff --git a/database/migrations/2021_12_02_21000_add_queued_at_to_emails_table.php b/database/migrations/2021_12_02_21000_add_queued_at_to_emails_table.php new file mode 100644 index 0000000..080bf35 --- /dev/null +++ b/database/migrations/2021_12_02_21000_add_queued_at_to_emails_table.php @@ -0,0 +1,34 @@ +timestamp('queued_at')->after('encrypted')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/src/Email.php b/src/Email.php index f4a30bf..8dbcd0d 100644 --- a/src/Email.php +++ b/src/Email.php @@ -23,6 +23,7 @@ * @property $failed * @property $error * @property $encrypted + * @property $queued_at * @property $scheduled_at * @property $sent_at * @property $delivered_at @@ -277,6 +278,30 @@ public function getAttempts() return $this->attempts; } + /** + * Get the queued date. + * + * @return mixed + */ + public function getQueuedDate() + { + return $this->queued_at; + } + + /** + * Get the queued date as a Carbon instance. + * + * @return Carbon + */ + public function getQueuedDateAsCarbon() + { + if ($this->queued_at instanceof Carbon) { + return $this->queued_at; + } + + return Carbon::parse($this->queued_at); + } + /** * Get the scheduled date. * diff --git a/src/Preparer.php b/src/Preparer.php index e2fba6b..3185a6a 100644 --- a/src/Preparer.php +++ b/src/Preparer.php @@ -36,6 +36,8 @@ public function prepare(EmailComposer $composer) $this->prepareScheduled($composer); $this->prepareImmediately($composer); + + $this->prepareQueued($composer); } /** @@ -235,4 +237,19 @@ private function prepareImmediately(EmailComposer $composer) $composer->getEmail()->fill(['sending' => 1]); } } + + /** + * Prepare the queued date. + * + * @param EmailComposer $composer + */ + private function prepareQueued(EmailComposer $composer) + { + if ($this->getData('queued', false) === true) { + $composer->getEmail()->fill([ + 'queued_at' => Carbon::now()->toDateTimeString(), + ]); + } + } + } diff --git a/src/Store.php b/src/Store.php index 656040f..a6b3eb2 100644 --- a/src/Store.php +++ b/src/Store.php @@ -19,6 +19,7 @@ public function getQueue() return $query ->whereNull('deleted_at') ->whereNull('sent_at') + ->whereNull('queued_at') ->where(function ($query) { $query->whereNull('scheduled_at') ->orWhere('scheduled_at', '<=', Carbon::now()->toDateTimeString()); From c339a39494c1e6bb68b457de6d19eeade27d1b00 Mon Sep 17 00:00:00 2001 From: Victor Kislichenko Date: Thu, 2 Dec 2021 22:10:55 +0200 Subject: [PATCH 2/4] fix --- src/Preparer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Preparer.php b/src/Preparer.php index 3185a6a..60b5fec 100644 --- a/src/Preparer.php +++ b/src/Preparer.php @@ -245,7 +245,7 @@ private function prepareImmediately(EmailComposer $composer) */ private function prepareQueued(EmailComposer $composer) { - if ($this->getData('queued', false) === true) { + if ($composer->getData('queued', false) === true) { $composer->getEmail()->fill([ 'queued_at' => Carbon::now()->toDateTimeString(), ]); From 8a8bd429c9230f6b9c92d531a0f5443bde6a022d Mon Sep 17 00:00:00 2001 From: Marick van Tuil Date: Sat, 4 Dec 2021 22:30:46 +0100 Subject: [PATCH 3/4] Add tests --- tests/QueuedEmailsTest.php | 8 ++++++++ tests/SendEmailsCommandTest.php | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/tests/QueuedEmailsTest.php b/tests/QueuedEmailsTest.php index 340fdb2..0bb7b96 100644 --- a/tests/QueuedEmailsTest.php +++ b/tests/QueuedEmailsTest.php @@ -21,6 +21,14 @@ public function queueing_an_email_will_leave_sending_on_false() $this->assertEquals(0, $email->sending); } + /** @test */ + public function queueing_an_email_will_set_the_queued_at_column() + { + $email = $this->queueEmail(); + + $this->assertNotNull($email->queued_at); + } + /** @test */ public function queueing_an_email_will_dispatch_a_job() { diff --git a/tests/SendEmailsCommandTest.php b/tests/SendEmailsCommandTest.php index 486f0e8..5aacca4 100644 --- a/tests/SendEmailsCommandTest.php +++ b/tests/SendEmailsCommandTest.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Queue; use Stackkit\LaravelDatabaseEmails\Store; class SendEmailsCommandTest extends TestCase @@ -45,6 +46,18 @@ public function an_email_should_not_be_sent_once_it_is_marked_as_sent() $this->assertEquals($firstSend, $email->fresh()->getSendDate()); } + /** @test */ + public function an_email_should_not_be_sent_if_it_is_queued() + { + Queue::fake(); + + $email = $this->queueEmail(); + + $this->artisan('email:send'); + + $this->assertNull($email->fresh()->getSendDate()); + } + /** @test */ public function if_an_email_fails_to_be_sent_it_should_be_logged_in_the_database() { From d5a096293aaef5c7300b00a382c32a89f3c56d03 Mon Sep 17 00:00:00 2001 From: Marick van Tuil Date: Sat, 4 Dec 2021 22:38:30 +0100 Subject: [PATCH 4/4] Run tests on pull request instead of push --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 8f68b21..adca7fe 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,7 +1,7 @@ name: Run tests on: - push: + pull_request: schedule: - cron: '0 0 * * *'