Skip to content

Queue function and Laravel 7 support #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ jobs:
strategy:
matrix:
php: [7.4, 7.3, 7.2]
laravel: [6.*, 5.8.*, 5.7.*, 5.6.*]
laravel: [7.*, 6.*, 5.8.*, 5.7.*, 5.6.*]
os: [ubuntu-latest]
include:
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*
- laravel: 5.8.*
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
},
"require-dev": {
"mockery/mockery": "^1.2",
"orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0",
"symfony/console": "^4.4",
"orchestra/testbench": "^3.5 || ^3.6 || ^3.7 || ^3.8 || ^4.0 || ^5.0",
"symfony/console": "^4.4|^5.0",
"tecnickcom/tcpdf": "^6.3"
}
}
15 changes: 12 additions & 3 deletions src/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public function hasFrom()
*/
public function hasCc()
{
return strlen($this->getOriginal('cc')) > 0;
return strlen($this->getRawDatabaseValue('cc')) > 0;
}

/**
Expand All @@ -358,7 +358,7 @@ public function hasCc()
*/
public function hasBcc()
{
return strlen($this->getOriginal('bcc')) > 0;
return strlen($this->getRawDatabaseValue('bcc')) > 0;
}

/**
Expand All @@ -378,7 +378,7 @@ public function isScheduled()
*/
public function isEncrypted()
{
return (bool) $this->getOriginal('encrypted');
return (bool) $this->getRawDatabaseValue('encrypted');
}

/**
Expand Down Expand Up @@ -479,4 +479,13 @@ public function retry()

$retry->save();
}

public function getRawDatabaseValue($key = null, $default = null)
{
if (method_exists($this, 'getRawOriginal')) {
return $this->getRawOriginal($key, $default);
}

return $this->getOriginal($key, $default);
}
}
30 changes: 30 additions & 0 deletions src/EmailComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,27 @@ public function later($scheduledAt)
return $this->send();
}

/**
* Queue the e-mail.
*
* @param string|null $connection
* @param string|null $queue
* @param \DateTimeInterface|\DateInterval|int|null $delay
* @return Email
*/
public function queue($connection = null, $queue = null, $delay = null)
{
$connection = $connection ?: config('queue.default');
$queue = $queue ?: 'default';

$this->setData('queued', true);
$this->setData('connection', $connection);
$this->setData('queue', $queue);
$this->setData('delay', $delay);

return $this->send();
}

/**
* Set the Mailable.
*
Expand Down Expand Up @@ -273,6 +294,15 @@ public function send()

$this->email->refresh();

if ($this->getData('queued', false) === true) {
dispatch(new SendEmailJob($this->email))
->onConnection($this->getData('connection'))
->onQueue($this->getData('queue'))
->delay($this->getData('delay'));

return $this->email;
}

if (Config::sendImmediately()) {
$this->email->send();
}
Expand Down
27 changes: 27 additions & 0 deletions src/SendEmailJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace Stackkit\LaravelDatabaseEmails;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $email;

public function __construct(Email $email)
{
$this->email = $email;
}

public function handle()
{
(new Sender())->send($this->email);
}
}
14 changes: 2 additions & 12 deletions src/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Stackkit\LaravelDatabaseEmails;

use Illuminate\Mail\Mailer;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Mail;

class Sender
{
Expand All @@ -20,23 +20,13 @@ public function send(Email $email)

$email->markAsSending();

$this->getMailerInstance()->send([], [], function (Message $message) use ($email) {
Mail::send([], [], function (Message $message) use ($email) {
$this->buildMessage($message, $email);
});

$email->markAsSent();
}

/**
* Get the instance of the Laravel mailer.
*
* @return Mailer
*/
private function getMailerInstance()
{
return app('mailer');
}

/**
* Build the e-mail message.
*
Expand Down
14 changes: 7 additions & 7 deletions tests/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function the_recipient_should_be_encrypted_and_decrypted()
{
$email = $this->sendEmail(['recipient' => 'john@doe.com']);

$this->assertEquals('john@doe.com', decrypt($email->getOriginal('recipient')));
$this->assertEquals('john@doe.com', decrypt($email->getRawDatabaseValue('recipient')));

$this->assertEquals('john@doe.com', $email->getRecipient());
}
Expand All @@ -39,8 +39,8 @@ public function cc_and_bb_should_be_encrypted_and_decrypted()
'bcc' => $bcc = ['jane+1@doe.com', 'jane+2@doe.com'],
]);

$this->assertEquals($cc, decrypt($email->getOriginal('cc')));
$this->assertEquals($bcc, decrypt($email->getOriginal('bcc')));
$this->assertEquals($cc, decrypt($email->getRawDatabaseValue('cc')));
$this->assertEquals($bcc, decrypt($email->getRawDatabaseValue('bcc')));

$this->assertEquals($cc, $email->getCc());
$this->assertEquals($bcc, $email->getBcc());
Expand All @@ -51,7 +51,7 @@ public function the_subject_should_be_encrypted_and_decrypted()
{
$email = $this->sendEmail(['subject' => 'test subject']);

$this->assertEquals('test subject', decrypt($email->getOriginal('subject')));
$this->assertEquals('test subject', decrypt($email->getRawDatabaseValue('subject')));

$this->assertEquals('test subject', $email->getSubject());
}
Expand All @@ -63,7 +63,7 @@ public function the_variables_should_be_encrypted_and_decrypted()

$this->assertEquals(
['name' => 'Jane Doe'],
decrypt($email->getOriginal('variables'))
decrypt($email->getRawDatabaseValue('variables'))
);

$this->assertEquals(
Expand All @@ -79,7 +79,7 @@ public function the_body_should_be_encrypted_and_decrypted()

$expectedBody = "Name: Jane Doe\n";

$this->assertEquals($expectedBody, decrypt($email->getOriginal('body')));
$this->assertEquals($expectedBody, decrypt($email->getRawDatabaseValue('body')));

$this->assertEquals($expectedBody, $email->getBody());
}
Expand All @@ -94,7 +94,7 @@ public function from_should_be_encrypted_and_decrypted()
'name' => 'Marick',
];

$this->assertEquals($expect, decrypt($email->getOriginal('from')));
$this->assertEquals($expect, decrypt($email->getRawDatabaseValue('from')));
$this->assertEquals($expect, $email->getFrom());
}
}
100 changes: 100 additions & 0 deletions tests/QueuedEmailsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Tests;

use Illuminate\Support\Facades\Queue;
use Stackkit\LaravelDatabaseEmails\SendEmailJob;
use Illuminate\Support\Facades\Mail;

class QueuedEmailsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}

/** @test */
public function queueing_an_email_will_leave_sending_on_false()
{
$email = $this->queueEmail();

$this->assertEquals(0, $email->sending);
}

/** @test */
public function queueing_an_email_will_dispatch_a_job()
{
Queue::fake();

$email = $this->queueEmail();

Queue::assertPushed(SendEmailJob::class, function (SendEmailJob $job) use ($email) {
return $job->email->id === $email->id;
});
}

/** @test */
public function emails_can_be_queued_on_a_specific_connection()
{
Queue::fake();

$this->queueEmail('some-connection');

Queue::assertPushed(SendEmailJob::class, function (SendEmailJob $job) {
return $job->connection === 'some-connection';
});
}

/** @test */
public function emails_can_be_queued_on_a_specific_queue()
{
Queue::fake();

$this->queueEmail('default', 'some-queue');

Queue::assertPushed(SendEmailJob::class, function (SendEmailJob $job) {
return $job->queue === 'some-queue';
});
}

/** @test */
public function emails_can_be_queued_with_a_delay()
{
Queue::fake();

$delay = now()->addMinutes(6);

$this->queueEmail(null, null, $delay);

Queue::assertPushed(SendEmailJob::class, function (SendEmailJob $job) use ($delay) {
return $job->delay->getTimestamp() === $delay->timestamp;
});
}

/** @test */
public function the_send_email_job_will_call_send_on_the_email_instance()
{
Queue::fake();

$email = $this->queueEmail('default', 'some-queue');

$job = new SendEmailJob($email);

Mail::shouldReceive('send')->once();

$job->handle();
}

/** @test */
public function the_mail_will_be_marked_as_sent_when_job_is_finished()
{
Queue::fake();

$email = $this->queueEmail('default', 'some-queue');

$job = new SendEmailJob($email);
$job->handle();

$this->assertTrue($email->isSent());
}
}
8 changes: 3 additions & 5 deletions tests/SendEmailsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public function an_email_should_not_be_sent_once_it_is_marked_as_sent()

$this->assertNotNull($firstSend = $email->fresh()->getSendDate());

sleep(1);

$this->artisan('email:send');

$this->assertEquals(1, $email->fresh()->getAttempts());
Expand All @@ -50,14 +48,14 @@ public function an_email_should_not_be_sent_once_it_is_marked_as_sent()
/** @test */
public function if_an_email_fails_to_be_sent_it_should_be_logged_in_the_database()
{
$this->app['config']['mail.driver'] = 'does-not-exist';

$email = $this->sendEmail();

$email->update(['recipient' => 'asdf']);

$this->artisan('email:send');

$this->assertTrue($email->fresh()->hasFailed());
$this->assertStringContains('Driver [does-not-exist] not supported.', $email->fresh()->getError());
$this->assertStringContains('Swift_RfcComplianceException', $email->fresh()->getError());
}

/** @test */
Expand Down
3 changes: 1 addition & 2 deletions tests/SenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public function it_sends_an_email()
{
$this->sendEmail();

Mail::shouldReceive('send')
->once();
Mail::shouldReceive('send')->once();

$this->artisan('email:send');
}
Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ protected function getEnvironmentSetUp($app)
'prefix' => '',
'strict' => true,
]);

$app['config']->set('mail.driver', 'log');
}

public function createEmail($overwrite = [])
Expand Down Expand Up @@ -131,6 +133,11 @@ public function scheduleEmail($scheduledFor, $overwrite = [])
return $this->createEmail($overwrite)->schedule($scheduledFor);
}

public function queueEmail($connection = null, $queue = null, $delay = null, $overwrite = [])
{
return $this->createEmail($overwrite)->queue($connection, $queue, $delay);
}

public function assertStringContains($needle, $haystack)
{
if (method_exists($this, 'assertStringContainsString')) {
Expand Down