-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathPersonalizationTest.php
More file actions
160 lines (128 loc) · 4.89 KB
/
PersonalizationTest.php
File metadata and controls
160 lines (128 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* This file tests Personalization.
*/
namespace SendGrid\Tests\Unit;
use PHPUnit\Framework\TestCase;
use SendGrid\Mail\Bcc;
use SendGrid\Mail\Cc;
use SendGrid\Mail\CustomArg;
use SendGrid\Mail\From;
use SendGrid\Mail\Header;
use SendGrid\Mail\Personalization;
use SendGrid\Mail\SendAt;
use SendGrid\Mail\Subject;
use SendGrid\Mail\To;
use SendGrid\Mail\TypeException;
use SendGrid\Mail\Substitution;
/**
* This class tests Personalization.
*
* @package SendGrid\Tests
*/
class PersonalizationTest extends TestCase
{
public function testAddTo()
{
$personalization = new Personalization();
$personalization->addTo(new To('dx@sendgrid.com'));
$this->assertSame('dx@sendgrid.com', $personalization->getTos()[0]->getEmail());
}
public function testAddToWithSubstitution()
{
$personalization = new Personalization();
$personalization->addTo(new To('dx@sendgrid.com', 'Test with sub',
[
'-name-' => 'Example User'
]
));
$this->assertArrayHasKey('-name-', $personalization->getSubstitutions());
}
public function testAddSubstitutionHasPrecedence()
{
$personalization = new Personalization();
$personalization->addSubstitution(new Substitution('-name-', 'Example User 2'));
$personalization->addTo(new To('dx@sendgrid.com', 'Test with sub',
[
'-name-' => 'Example User'
]
));
$this->assertArrayHasKey('-name-', $personalization->getSubstitutions());
$this->assertSame('Example User 2', $personalization->getSubstitutions()['-name-']);
}
public function testAddFrom()
{
$personalization = new Personalization();
$personalization->addFrom(new From('dx@sendgrid.com'));
$this->assertSame('dx@sendgrid.com', $personalization->getFrom()->getEmail());
}
public function testAddCc()
{
$personalization = new Personalization();
$personalization->addCc(new Cc('dx@sendgrid.com'));
$this->assertSame('dx@sendgrid.com', $personalization->getCcs()[0]->getEmail());
}
public function testAddBcc()
{
$personalization = new Personalization();
$personalization->addBcc(new Bcc('dx@sendgrid.com'));
$this->assertSame('dx@sendgrid.com', $personalization->getBccs()[0]->getEmail());
}
public function testSetSubject()
{
$personalization = new Personalization();
$personalization->setSubject(new Subject('subject'));
$this->assertSame('subject', $personalization->getSubject()->getSubject());
}
public function testSetSubjectOnInvalidSubjectClass()
{
$this->expectException(TypeException::class);
$this->expectExceptionMessageMatches('/"\$subject" must be an instance of SendGrid\\\Mail\\\Subject or a string/');
$personalization = new Personalization();
$personalization->setSubject(false);
}
public function testAddHeader()
{
$personalization = new Personalization();
$personalization->addHeader(new Header('Content-Type', 'text/plain'));
$this->assertSame(['Content-Type' => 'text/plain'], $personalization->getHeaders());
}
public function testAddDynamicTemplateData()
{
$personalization = new Personalization();
$personalization->addDynamicTemplateData('data', 'data_value');
$this->assertSame(['data' => 'data_value'], $personalization->getDynamicTemplateData());
}
public function testAddCustomArg()
{
$personalization = new Personalization();
$personalization->addCustomArg(new CustomArg('custom_arg', 'arg_value'));
$this->assertSame(['custom_arg' => 'arg_value'], $personalization->getCustomArgs());
}
public function testSetSendAt()
{
$personalization = new Personalization();
$personalization->setSendAt(new SendAt(1539363393));
$this->assertSame(1539363393, $personalization->getSendAt()->getSendAt());
}
public function testSendAtOnInvalidSendAtClass()
{
$this->expectException(TypeException::class);
$this->expectExceptionMessageMatches('/"\$send_at" must be an instance of "SendGrid\\\Mail\\\SendAt/');
$personalization = new Personalization();
$personalization->setSendAt('invalid_send_at_class');
}
public function testSetHasDynamicTemplate()
{
$personalization = new Personalization();
$personalization->setHasDynamicTemplate(true);
$this->assertTrue($personalization->getHasDynamicTemplate());
}
public function testSetHasDynamicTemplateOnInvalidType()
{
$this->expectException(TypeException::class);
$this->expectExceptionMessageMatches('/"\$has_dynamic_template" must be a boolean/');
$personalization = new Personalization();
$personalization->setHasDynamicTemplate('invalid_bool_type');
}
}