Skip to content

Commit

Permalink
Merge pull request #29 from railsware/fix/template-variables
Browse files Browse the repository at this point in the history
Issue-28 > support more types in template_variables
  • Loading branch information
gaalferov authored Aug 15, 2024
2 parents 8021b0b + 32c291e commit d040739
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [2.0.1] - 2024-08-16

- Support mixed types in template_variables (array, string, int, float, bool)

## [2.0.0] - 2024-06-12
- [BC BREAK] PHP 7.4 will no longer be supported (PHP 8.0+).
- [BC BREAK] Rebuild `Emails` layers to use the `inboxId` at the client level ([examples](examples/testing/emails.php))
Expand Down
36 changes: 34 additions & 2 deletions examples/sending/emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,23 @@
'user_name' => 'Jon Bush',
'next_step_link' => 'https://mailtrap.io/',
'get_started_link' => 'https://mailtrap.io/',
'onboarding_video_link' => 'some_video_link'
'onboarding_video_link' => 'some_video_link',
'company' => [
'name' => 'Best Company',
'address' => 'Its Address',
],
'products' => [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
],
'isBool' => true,
'int' => 123
])
;

Expand Down Expand Up @@ -184,7 +200,23 @@
'user_name' => 'Jon Bush',
'next_step_link' => 'https://mailtrap.io/',
'get_started_link' => 'https://mailtrap.io/',
'onboarding_video_link' => 'some_video_link'
'onboarding_video_link' => 'some_video_link',
'company' => [
'name' => 'Best Company',
'address' => 'Its Address',
],
'products' => [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
],
'isBool' => true,
'int' => 123
])
;

Expand Down
18 changes: 17 additions & 1 deletion examples/testing/emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,23 @@
'user_name' => 'Jon Bush',
'next_step_link' => 'https://mailtrap.io/',
'get_started_link' => 'https://mailtrap.io/',
'onboarding_video_link' => 'some_video_link'
'onboarding_video_link' => 'some_video_link',
'company' => [
'name' => 'Best Company',
'address' => 'Its Address',
],
'products' => [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
],
'isBool' => true,
'int' => 123
])
;

Expand Down
21 changes: 8 additions & 13 deletions src/EmailHeader/Template/TemplateVariableHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mailtrap\EmailHeader\Template;

use Mailtrap\Exception\RuntimeException;
use Symfony\Component\Mime\Header\AbstractHeader;

/**
Expand All @@ -15,52 +16,46 @@ class TemplateVariableHeader extends AbstractHeader
{
public const VAR_NAME = 'template_variables';

private string $value;
private mixed $value;

public function __construct(string $name, string $value)
public function __construct(string $name, mixed $value)
{
parent::__construct($name);

$this->setValue($value);
}

/**
* @param string $body
*/
public function setBody($body): void
public function setBody(mixed $body): void
{
$this->setValue($body);
}

/**
* @psalm-suppress MethodSignatureMismatch
*/
public function getBody(): string
public function getBody(): mixed
{
return $this->getValue();
}

/**
* Get the (unencoded) value of this header.
*/
public function getValue(): string
public function getValue(): mixed
{
return $this->value;
}

/**
* Set the (unencoded) value of this header.
*/
public function setValue(string $value): void
public function setValue(mixed $value): void
{
$this->value = $value;
}

/**
* Get the value of this header prepared for rendering.
*/
public function getBodyAsString(): string
{
return $this->encodeWords($this, $this->value);
throw new RuntimeException(__METHOD__ . ' method is not supported for this type of header');
}
}
2 changes: 1 addition & 1 deletion src/Mime/MailtrapEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function templateUuid(string $templateUuid): self
return $this;
}

public function templateVariable(string $name, string $value): self
public function templateVariable(string $name, mixed $value): self
{
$this->getHeaders()->add(new TemplateVariableHeader($name, $value));

Expand Down
82 changes: 81 additions & 1 deletion tests/Api/AbstractEmailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ public function testValidSendTemplate(): void
->add(new TemplateVariableHeader('next_step_link', 'https://mailtrap.io/'))
->add(new TemplateVariableHeader('get_started_link', 'https://mailtrap.io/'))
->add(new TemplateVariableHeader('onboarding_video_link', 'some_video_link'))
->add(new TemplateVariableHeader('company', [
'name' => 'Best Company',
'address' => 'Its Address',
]))
->add(new TemplateVariableHeader('products', [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
]))
->add(new TemplateVariableHeader('isBool', true))
->add(new TemplateVariableHeader('int', 123))
;

$this->email
Expand All @@ -175,6 +191,22 @@ public function testValidSendTemplate(): void
'next_step_link' => 'https://mailtrap.io/',
'get_started_link' => 'https://mailtrap.io/',
'onboarding_video_link' => 'some_video_link',
'company' => [
'name' => 'Best Company',
'address' => 'Its Address',
],
'products' => [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
],
'isBool' => true,
'int' => 123,
]
])
->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData)));
Expand Down Expand Up @@ -206,7 +238,23 @@ public function testValidSendTemplateNewEmailWrapper(): void
'unicode_user_name' => 'Subašić',
'next_step_link' => 'https://mailtrap.io/',
'get_started_link' => 'https://mailtrap.io/',
'onboarding_video_link' => 'some_video_link'
'onboarding_video_link' => 'some_video_link',
'company' => [
'name' => 'Best Company',
'address' => 'Its Address',
],
'products' => [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
],
'isBool' => true,
'int' => 123,
])
;

Expand All @@ -229,6 +277,22 @@ public function testValidSendTemplateNewEmailWrapper(): void
'next_step_link' => 'https://mailtrap.io/',
'get_started_link' => 'https://mailtrap.io/',
'onboarding_video_link' => 'some_video_link',
'company' => [
'name' => 'Best Company',
'address' => 'Its Address',
],
'products' => [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
],
'isBool' => true,
'int' => 123,
]
])
->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData)));
Expand Down Expand Up @@ -533,6 +597,22 @@ public function validTemplateVariablesDataProvider(): array
['user_name', 'Jon Bush'],
['next_step_link', 'https://mailtrap.io/'],
['onboarding_video_link', 'some_video_link'],
['company', [
'name' => 'Best Company',
'address' => 'Its Address',
]],
['products', [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
]],
['isBool', true],
['int', 123],
];
}

Expand Down
64 changes: 64 additions & 0 deletions tests/Api/Sandbox/EmailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ public function testValidSendTemplateToSandbox(): void
->add(new TemplateVariableHeader('next_step_link', 'https://mailtrap.io/'))
->add(new TemplateVariableHeader('get_started_link', 'https://mailtrap.io/'))
->add(new TemplateVariableHeader('onboarding_video_link', 'some_video_link'))
->add(new TemplateVariableHeader('company', [
'name' => 'Best Company',
'address' => 'Its Address',
]))
->add(new TemplateVariableHeader('products', [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
]))
->add(new TemplateVariableHeader('isBool', true))
->add(new TemplateVariableHeader('int', 123))
;

$this->email
Expand All @@ -144,6 +160,22 @@ public function testValidSendTemplateToSandbox(): void
'next_step_link' => 'https://mailtrap.io/',
'get_started_link' => 'https://mailtrap.io/',
'onboarding_video_link' => 'some_video_link',
'company' => [
'name' => 'Best Company',
'address' => 'Its Address',
],
'products' => [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
],
'isBool' => true,
'int' => 123,
]
])
->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData)));
Expand Down Expand Up @@ -176,6 +208,22 @@ public function testValidSendTemplateToSandboxNewEmailWrapper(): void
'next_step_link' => 'https://mailtrap.io/',
'get_started_link' => 'https://mailtrap.io/',
'onboarding_video_link' => 'some_video_link',
'company' => [
'name' => 'Best Company',
'address' => 'Its Address',
],
'products' => [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
],
'isBool' => true,
'int' => 123,
])
;

Expand All @@ -198,6 +246,22 @@ public function testValidSendTemplateToSandboxNewEmailWrapper(): void
'next_step_link' => 'https://mailtrap.io/',
'get_started_link' => 'https://mailtrap.io/',
'onboarding_video_link' => 'some_video_link',
'company' => [
'name' => 'Best Company',
'address' => 'Its Address',
],
'products' => [
[
'name' => 'Product 1',
'price' => 100,
],
[
'name' => 'Product 2',
'price' => 200,
],
],
'isBool' => true,
'int' => 123
]
])
->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedData)));
Expand Down

0 comments on commit d040739

Please sign in to comment.