diff --git a/.travis.yml b/.travis.yml index 9f8106eb..5db51c85 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/Postmark/PostmarkClient.php b/src/Postmark/PostmarkClient.php index cb0359ef..55d12773 100644 --- a/src/Postmark/PostmarkClient.php +++ b/src/Postmark/PostmarkClient.php @@ -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)); } @@ -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)); } @@ -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)); } @@ -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)); } diff --git a/tests/PostmarkClientTemplatesTest.php b/tests/PostmarkClientTemplatesTest.php index 80feb2ad..f45bf654 100644 --- a/tests/PostmarkClientTemplatesTest.php +++ b/tests/PostmarkClientTemplatesTest.php @@ -4,6 +4,7 @@ require_once __DIR__ . "/PostmarkClientBaseTest.php"; +use Postmark\Models\PostmarkAttachment; use Postmark\PostmarkClient as PostmarkClient; class PostmarkClientTemplatesTest extends PostmarkClientBaseTest { @@ -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 {{name}}!", "Hello {{name}}!"); - $this->assertNotEmpty($result); + + // Creating a layout template + $layoutResult = $client->createTemplate('test-php-template-layout-' . date('c'), NULL, "Hello {{{@content}}}!", "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 {{name}}!", "Hello {{name}}!", null, "Standard", $layoutResult->Alias); + $this->assertNotEmpty($standardResult->TemplateId); + $this->assertEquals($layoutResult->Alias, $standardResult->LayoutTemplate); } //edit @@ -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 {{{@content}}}!", "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 @@ -54,8 +77,20 @@ function testClientCanListTemplates() { for ($i = 0; $i < 5; $i++) { $client->createTemplate('test-php-template-' . $i . '-' . date('c'), "{{subject}}", "Hello {{name}}!", "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 {{{@content}}}!", "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 {{name}}!", "Hello {{name}}!", null, "Standard", $layoutTemplate->Alias); + $result = $client->listTemplates(100, 0, "All", $layoutTemplate->Alias); + $this->assertNotEmpty($result->Templates); } //get @@ -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 {{name}}!", "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.'); + } } ?> \ No newline at end of file