Skip to content
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

Email transport configuration enhancements #136

Merged
merged 5 commits into from
Oct 10, 2023
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
8 changes: 6 additions & 2 deletions src/Job/SendMailJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Cake\Log\Log;
use Cake\Mailer\AbstractTransport;
use Cake\Mailer\TransportFactory;
use Cake\Queue\Queue\Processor;

/**
Expand Down Expand Up @@ -65,12 +66,15 @@
*/
protected function getTransport(string $transportClassName, array $config): AbstractTransport
{
if ($transportClassName === '') {
throw new \InvalidArgumentException('Transport class name is empty.');

Check warning on line 70 in src/Job/SendMailJob.php

View check run for this annotation

Codecov / codecov/patch

src/Job/SendMailJob.php#L70

Added line #L70 was not covered by tests
}

if (
empty($transportClassName) ||
!class_exists($transportClassName) ||
!method_exists($transportClassName, 'send')
) {
throw new \InvalidArgumentException(sprintf('Transport class name is not valid: %s', $transportClassName));
return TransportFactory::get($transportClassName);
}

$transport = new $transportClassName($config);
Expand Down
2 changes: 1 addition & 1 deletion src/Mailer/Transport/QueueTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function prepareData(Message $message): array
{
return [
'transport' => $this->getConfig('transport'),
'config' => $this->getConfig(),
'config' => is_array($this->getConfig('config')) ? $this->getConfig('config') : $this->getConfig(),
'emailMessage' => json_encode($message),
];
}
Expand Down
27 changes: 27 additions & 0 deletions tests/TestCase/Job/SendMailJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Cake\Mailer\Mailer;
use Cake\Mailer\Transport\DebugTransport;
use Cake\Mailer\TransportFactory;
use Cake\Queue\Job\Message;
use Cake\Queue\Job\SendMailJob;
use Cake\Queue\Queue\Processor;
Expand Down Expand Up @@ -78,6 +79,32 @@ public function testExecute()
$this->assertSame(Processor::ACK, $actual);
}

/**
* Test execute method with transport name
*
* @return void
*/
public function testExecuteTransportName()
{
$job = new SendMailJob();
$message = $this->createMessage('foo', [], $this->message);
$emailMessage = new \Cake\Mailer\Message();
$data = json_decode($message->getArgument('emailMessage'), true);
$emailMessage->createFromArray($data);

$transport = $this->getMockBuilder(DebugTransport::class)->getMock();
$transport->expects($this->once())
->method('send')
->with($emailMessage)
->willReturn(['message' => 'test', 'headers' => []]);
TransportFactory::getRegistry()->set('foo', $transport);

$actual = $job->execute($message);
$this->assertSame(Processor::ACK, $actual);

TransportFactory::drop('foo');
}

/**
* Test execute with attachments method
*
Expand Down
95 changes: 93 additions & 2 deletions tests/TestCase/Mailer/Transport/QueueTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@
*/
namespace Cake\Queue\Test\TestCase\Job;

use Cake\Mailer\Message;
use Cake\Mailer\Transport\SmtpTransport;
use Cake\Queue\Mailer\Transport\QueueTransport;
use Cake\Queue\QueueManager;
use Cake\TestSuite\TestCase;

class QueueTransportTest extends TestCase
{
private $fsQueuePath = TMP . DS . 'queue';

private function getFsQueueUrl(): string
{
return 'file:///' . $this->fsQueuePath;
}

private function getFsQueueFile(): string
{
return $this->getFsQueueUrl() . DS . 'enqueue.app.default';
}

/**
* Test send
*
Expand All @@ -31,9 +45,9 @@ public function testSend()
{
QueueManager::setConfig('default', [
'queue' => 'default',
'url' => 'null:',
'url' => $this->getFsQueueUrl(),
]);
$message = (new \Cake\Mailer\Message())
$message = (new Message())
->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Sample Subject');
Expand All @@ -58,6 +72,83 @@ public function testSend()

$expected = ['headers' => $headers, 'message' => 'Message has been enqueued'];
$this->assertEquals($expected, $result);

$fsQueueFile = $this->getFsQueueFile();
$this->assertFileExists($fsQueueFile);

$content = file_get_contents($fsQueueFile);
$this->assertStringContainsString('MailTransport', $content);

QueueManager::drop('default');
}

/**
* Test send custom transport
*
* @return void
*/
public function testSendCustomTransport()
{
QueueManager::setConfig('default', [
'queue' => 'default',
'url' => $this->getFsQueueUrl(),
]);
$message = (new Message());

$transport = new QueueTransport([
'transport' => SmtpTransport::class,
'config' => [
'host' => 'mail.example.com',
],
]);
$transport->send($message);

$fsQueueFile = $this->getFsQueueFile();
$this->assertFileExists($fsQueueFile);

$content = file_get_contents($fsQueueFile);
$this->assertStringContainsString('SmtpTransport', $content);
$this->assertStringContainsString('"config\":{\"host\":\"mail.example.com\"}', $content);

QueueManager::drop('default');
}

/**
* Test send backwards compatibility transport config
*
* @return void
*/
public function testSendBcTransport()
{
QueueManager::setConfig('default', [
'queue' => 'default',
'url' => $this->getFsQueueUrl(),
]);
$message = (new Message());

$transport = new QueueTransport([
'transport' => SmtpTransport::class,
'host' => 'mail.example.com',
]);
$transport->send($message);

$fsQueueFile = $this->getFsQueueFile();
$this->assertFileExists($fsQueueFile);

$content = file_get_contents($fsQueueFile);
$this->assertStringContainsString('SmtpTransport', $content);
$this->assertStringContainsString('"host\":\"mail.example.com\"', $content);

QueueManager::drop('default');
}

public function tearDown(): void
{
parent::tearDown();

$fsQueueFile = $this->getFsQueueFile();
if (file_exists($fsQueueFile)) {
unlink($fsQueueFile);
}
}
}
Loading