Skip to content

Commit 5f3dbca

Browse files
committed
Track templates with email event
1 parent 5d8f912 commit 5f3dbca

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace Tatter\Outbox\Database\Migrations;
2+
3+
use CodeIgniter\Database\Migration;
4+
5+
class AddEmailsTemplate extends Migration
6+
{
7+
public function up()
8+
{
9+
$this->forge->addColumn('outbox_emails', [
10+
'template_id' => ['type' => 'int', 'null' => true, 'after' => 'BCCBatchSize'],
11+
'template' => ['type' => 'varchar', 'constraint' => 255, 'null' => true, 'after' => 'BCCBatchSize'],
12+
]);
13+
}
14+
15+
public function down()
16+
{
17+
$this->forge->dropColumn('outbox_emails', 'template');
18+
$this->forge->dropColumn('outbox_emails', 'template_id');
19+
}
20+
}

src/Entities/Email.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Email extends Entity
1919
'sendMultipart' => 'boolean',
2020
'BCCBatchMode' => 'boolean',
2121
'BCCBatchSize' => 'int',
22+
'template_id' => '?int',
2223
];
2324

2425
/**

src/Entities/Template.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ public function renderSubject($data = []): string
149149
public function email($data = [], string $styles = null): Email
150150
{
151151
// Start with the default config and add necessary settings
152-
$email = service('email');
153-
$email->mailType = 'html';
154-
$email->wordWrap = false;
152+
$email = service('email');
153+
$email->mailType = 'html';
154+
$email->wordWrap = false;
155+
$email->template = $this->attributes['name'];
156+
$email->template_id = $this->attributes['id'] ?? null;
155157

156158
// Render the subject and body and return the Email instance
157159
return $email

src/Models/EmailModel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ class EmailModel extends Model
1616
protected $allowedFields = [
1717
'subject', 'body', 'fromEmail', 'fromName', 'userAgent', 'protocol', 'mailType',
1818
'charset', 'priority', 'sendMultipart', 'BCCBatchMode', 'BCCBatchSize',
19+
'template', 'template_id',
1920
];
2021
}

tests/unit/EventTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

33
use CodeIgniter\Config\Config;
4+
use Tatter\Outbox\Entities\Template;
45
use Tatter\Outbox\Models\AttachmentModel;
6+
use Tatter\Outbox\Models\TemplateModel;
57
use Tests\Support\DatabaseTestCase;
68

79
class EventTest extends DatabaseTestCase
@@ -79,4 +81,19 @@ public function testEventRespectsConfigLogging()
7981
$this->assertTrue($result);
8082
$this->dontSeeInDatabase('outbox_emails', ['subject' => 'Email test']);
8183
}
84+
85+
public function testEventRecordsTemplate()
86+
{
87+
$templateId = model(TemplateModel::class)->insert(new Template([
88+
'name' => 'Test Template',
89+
'subject' => 'Some {subject}',
90+
'body' => '<p>{number}</p>',
91+
]));
92+
93+
$template = model(TemplateModel::class)->findByName('Test Template');
94+
$template->email()->send();
95+
96+
$this->seeInDatabase('outbox_emails', ['template' => 'Test Template']);
97+
$this->seeInDatabase('outbox_emails', ['template_id' => $templateId]);
98+
}
8299
}

0 commit comments

Comments
 (0)