Skip to content

Commit

Permalink
Merge pull request #63 from wildbit/task/add-support-for-layout-templ…
Browse files Browse the repository at this point in the history
…ates

Task/add support for layout templates
  • Loading branch information
vladsandu authored Aug 22, 2019
2 parents 6c0d832 + 18b360a commit 0658127
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ php:
- 5.5
- 5.6
- 7.0
- hhvm
dist: trusty
before_script:
- composer self-update
- composer install --no-interaction --prefer-source --dev
Expand Down
21 changes: 16 additions & 5 deletions src/Postmark/PostmarkClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -955,16 +955,21 @@ function deleteTemplate($id) {
* @param string $htmlBody The template to be used for the 'htmlBody' of emails sent using this template, optional if 'textBody' is not NULL.
* @param string $textBody The template to be used for the 'textBody' of emails sent using this template, optional if 'htmlBody' is not NULL.
* @param string $alias An optional string you can provide to identify this Template. Allowed characters are numbers, ASCII letters, and ‘.’, ‘-’, ‘_’ characters, and the string has to start with a letter.
* @param string $templateType Creates the template based on the template type provided. Possible options: Standard or Layout. Defaults to Standard.
* @param string $layoutTemplate The alias of the Layout template that you want to use as layout for this Standard template. If not provided, a standard template will not use a layout template.
*
* @return DynamicResponseModel
*/
function createTemplate($name, $subject, $htmlBody, $textBody, $alias = NULL) {
function createTemplate($name, $subject, $htmlBody, $textBody, $alias = NULL, $templateType = 'Standard', $layoutTemplate = NULL) {
$template = array();
$template["name"] = $name;
$template["subject"] = $subject;
$template["htmlBody"] = $htmlBody;
$template["textBody"] = $textBody;
$template["alias"] = $alias;
$template["templateType"] = $templateType;
$template["layoutTemplate"] = $layoutTemplate;

return new DynamicResponseModel($this->processRestRequest('POST', "/templates", $template));
}

Expand All @@ -977,16 +982,18 @@ function createTemplate($name, $subject, $htmlBody, $textBody, $alias = NULL) {
* @param string $htmlBody The template to be used for the 'htmlBody' of emails sent using this template.
* @param string $textBody The template to be used for the 'textBody' of emails sent using this template.
* @param string $alias An optional string you can provide to identify this Template. Allowed characters are numbers, ASCII letters, and ‘.’, ‘-’, ‘_’ characters, and the string has to start with a letter.
* @param string $layoutTemplate The alias of the Layout template that you want to use as layout for this Standard template. If not provided, a standard template will not use a layout template.
*
* @return DynamicResponseModel
*/
function editTemplate($id, $name = NULL, $subject = NULL, $htmlBody = NULL, $textBody = NULL, $alias = NULL) {
function editTemplate($id, $name = NULL, $subject = NULL, $htmlBody = NULL, $textBody = NULL, $alias = NULL, $layoutTemplate = NULL) {
$template = array();
$template["name"] = $name;
$template["subject"] = $subject;
$template["htmlBody"] = $htmlBody;
$template["textBody"] = $textBody;
$template["alias"] = $alias;
$template["layoutTemplate"] = $layoutTemplate;

return new DynamicResponseModel($this->processRestRequest('PUT', "/templates/$id", $template));
}
Expand All @@ -1006,14 +1013,18 @@ function getTemplate($id) {
*
* @param integer $count The total number of templates to get at once (default is 100)
* @param integer $offset The number of templates to "Skip" before returning results.
* @param string $templateType Filters the results based on the template type provided. Possible options: Standard, Layout, All. Defaults to All.
* @param string $layoutTemplate Filters the results based on the layout template alias. Defaults to NULL.
*
* @return DynamicResponseModel
*/
function listTemplates($count = 100, $offset = 0) {
function listTemplates($count = 100, $offset = 0, $templateType = 'All', $layoutTemplate = NULL) {
$query = array();

$query["count"] = $count;
$query["offset"] = $offset;
$query["templateType"] = $templateType;
$query["layoutTemplate"] = $layoutTemplate;

return new DynamicResponseModel($this->processRestRequest('GET', "/templates", $query));
}
Expand All @@ -1038,8 +1049,8 @@ function validateTemplate($subject = NULL, $htmlBody = NULL, $textBody = NULL, $
$query["textBody"] = $textBody;
$query["testRenderModel"] = $testRenderModel;
$query["inlineCssForHtmlTestRender"] = $inlineCssForHtmlTestRender;
$query['templateType'] = $templateType;
$query['layoutTemplate'] = $layoutTemplate;
$query["templateType"] = $templateType;
$query["layoutTemplate"] = $layoutTemplate;

return new DynamicResponseModel($this->processRestRequest('POST', "/templates/validate", $query));
}
Expand Down
68 changes: 66 additions & 2 deletions tests/PostmarkClientTemplatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require_once __DIR__ . "/PostmarkClientBaseTest.php";

use Postmark\Models\PostmarkAttachment;
use Postmark\PostmarkClient as PostmarkClient;

class PostmarkClientTemplatesTest extends PostmarkClientBaseTest {
Expand All @@ -27,8 +28,16 @@ static function tearDownAfterClass() {
function testClientCanCreateTemplate() {
$tk = parent::$testKeys;
$client = new PostmarkClient($tk->WRITE_TEST_SERVER_TOKEN, $tk->TEST_TIMEOUT);
$result = $client->createTemplate('test-php-template-' . date('c'), "{{subject}}", "Hello <b>{{name}}</b>!", "Hello {{name}}!");
$this->assertNotEmpty($result);

// Creating a layout template
$layoutResult = $client->createTemplate('test-php-template-layout-' . date('c'), NULL, "Hello <b>{{{@content}}}</b>!", "Hello {{{@content}}}!", null, "Layout");
$this->assertNotEmpty($layoutResult->TemplateId);
$this->assertNotEmpty($layoutResult->Alias);

// Creating a standard template using that layout template
$standardResult = $client->createTemplate('test-php-template-' . date('c'), "{{subject}}", "Hello <b>{{name}}</b>!", "Hello {{name}}!", null, "Standard", $layoutResult->Alias);
$this->assertNotEmpty($standardResult->TemplateId);
$this->assertEquals($layoutResult->Alias, $standardResult->LayoutTemplate);
}

//edit
Expand All @@ -45,6 +54,20 @@ function testClientCanEditTemplate() {
$this->assertNotSame($firstVersion->HtmlBody, $secondVersion->HtmlBody);
$this->assertNotSame($firstVersion->Subject, $secondVersion->Subject);
$this->assertNotSame($firstVersion->TextBody, $secondVersion->TextBody);
$this->assertEquals($firstVersion->TemplateType, $secondVersion->TemplateType);

// Creating a layout template
$layoutTemplate = $client->createTemplate('test-php-template-layout-' . date('c'), NULL, "Hello <b>{{{@content}}}</b>!", "Hello {{{@content}}}!", null, "Layout");

// Adding a layout template to a standard template
$r3 = $client->editTemplate($result->TemplateId, NULL, NULL, NULL, NULL, NULL, $layoutTemplate->Alias);
$versionWithLayoutTemplate = $client->getTemplate($r3->TemplateId);
$this->assertEquals($layoutTemplate->Alias, $versionWithLayoutTemplate->LayoutTemplate);

// Removing the layout template
$r4 = $client->editTemplate($result->TemplateId, NULL, NULL, NULL, NULL, NULL, "");
$versionWithoutLayoutTemplate = $client->getTemplate($r4->TemplateId);
$this->assertNull($versionWithoutLayoutTemplate->LayoutTemplate);
}

//list
Expand All @@ -54,8 +77,20 @@ function testClientCanListTemplates() {
for ($i = 0; $i < 5; $i++) {
$client->createTemplate('test-php-template-' . $i . '-' . date('c'), "{{subject}}", "Hello <b>{{name}}</b>!", "Hello {{name}}!");
}

// Listing all templates
$result = $client->listTemplates();
$this->assertNotEmpty($result->Templates);

// Filtering Layout templates
$layoutTemplate = $client->createTemplate('test-php-template-layout-' . date('c'), NULL, "Hello <b>{{{@content}}}</b>!", "Hello {{{@content}}}!", null, "Layout");
$result = $client->listTemplates(100, 0, "Layout");
$this->assertNotEmpty($result->Templates);

// Filtering by LayoutTemplate
$client->createTemplate('test-php-template-' . date('c'), "{{subject}}", "Hello <b>{{name}}</b>!", "Hello {{name}}!", null, "Standard", $layoutTemplate->Alias);
$result = $client->listTemplates(100, 0, "All", $layoutTemplate->Alias);
$this->assertNotEmpty($result->Templates);
}

//get
Expand Down Expand Up @@ -101,5 +136,34 @@ function testClientCanSendMailWithTemplate() {

$this->assertEquals(0, $emailResult->ErrorCode);
}

//send batch
function testClientCanSendBatchMessagesWithTemplate() {
$tk = parent::$testKeys;

$client = new PostmarkClient($tk->WRITE_TEST_SERVER_TOKEN, $tk->TEST_TIMEOUT);

$result = $client->createTemplate('test-php-template-' . date('c'), "Subject", "Hello <b>{{name}}</b>!", "Hello {{name}}!");

$batch = array();

$attachment = PostmarkAttachment::fromRawData("attachment content", "hello.txt", "text/plain");

for ($i = 0; $i < 5; $i++) {
$payload = array(
'From' => $tk->WRITE_TEST_SENDER_EMAIL_ADDRESS,
'To' => $tk->WRITE_TEST_EMAIL_RECIPIENT_ADDRESS,
'TemplateID' => $result->TemplateId,
'TemplateModel' => array("name" => "Jones-" . $i),
'TrackOpens' => true,
'Headers' => array("X-Test-Header" => "Test Header Content", 'X-Test-Date-Sent' => date('c')),
'Attachments' => array($attachment));

$batch[] = $payload;
}

$response = $client->sendEmailBatchWithTemplate($batch);
$this->assertNotEmpty($response, 'The client could not send a batch of messages.');
}
}
?>

0 comments on commit 0658127

Please sign in to comment.