Skip to content

Commit 3e54cf3

Browse files
author
danil zakablukovskii
committed
finished tests
fixed some errors
1 parent a98753f commit 3e54cf3

File tree

5 files changed

+181
-7
lines changed

5 files changed

+181
-7
lines changed

Message.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public function getCc()
349349
}
350350

351351
if (isset($item['name'])) {
352-
$addresses[$item['name']] = $item['email'];
352+
$addresses[$item['email']] = $item['name'];
353353
} else {
354354
$addresses[] = $item['email'];
355355
}
@@ -402,7 +402,7 @@ public function getBcc()
402402
}
403403

404404
if (isset($item['name'])) {
405-
$addresses[$item['name']] = $item['email'];
405+
$addresses[$item['email']] = $item['name'];
406406
} else {
407407
$addresses[] = $item['email'];
408408
}
@@ -574,7 +574,7 @@ public function embed($fileName, array $options = [])
574574
}
575575

576576
$mimeType = FileHelper::getMimeType($fileName);
577-
if (strpos($mimeType, 'image') === 0) {
577+
if (strpos($mimeType, 'image') === false) {
578578
throw new \InvalidArgumentException("Only images can be embed. Given file {$fileName} is " . $mimeType);
579579
}
580580

@@ -604,11 +604,11 @@ public function embedContent($content, array $options = [])
604604
}
605605

606606
$mimeType = $this->getBinaryMimeType($content);
607-
if (strpos($mimeType, 'image') === 0) {
607+
if (strpos($mimeType, 'image') === false) {
608608
throw new \InvalidArgumentException("Only images can be embed. Given content is " . $mimeType);
609609
}
610610

611-
$cid = 'image' . count($this->_images);
611+
$cid = 'image_' . count($this->_images);
612612

613613
$this->_images[] = [
614614
'type' => ArrayHelper::getValue($options, 'contentType', $mimeType),
@@ -769,6 +769,22 @@ public function setUseDraftTemplate($useDraftTemplate)
769769
$this->_useDraftTemplate = $useDraftTemplate;
770770
}
771771

772+
/**
773+
* @return array
774+
*/
775+
public function getAttachments()
776+
{
777+
return $this->_attachments;
778+
}
779+
780+
/**
781+
* @return array
782+
*/
783+
public function getImages()
784+
{
785+
return $this->_images;
786+
}
787+
772788
/**
773789
* Prepares the message and gives it's array representation to send it through SparkSpot API
774790
* @see \SparkPost\Transmission::send()

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "djagya/yii2-sparkpost",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "A library provides Yii2 integration with SparkPost mail service",
55
"keywords": [
66
"sparkpost",

tests/test_image.png

3.82 KB
Loading

tests/unit/MailerTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function _before()
1313
{
1414
new \yii\console\Application(['id' => 'app', 'basePath' => __DIR__]);
1515
}
16-
16+
1717
public function testApiKeyRequired()
1818
{
1919
$this->setExpectedException('\yii\base\InvalidConfigException');
@@ -52,6 +52,7 @@ public function testDefaultEmail()
5252
$this->assertEquals($mailer->defaultEmail, $email);
5353
$message = $mailer->compose();
5454
$this->assertEquals($message->getReplyTo(), $email);
55+
$this->assertEquals($message->getFrom(), $email);
5556
}
5657

5758
public function testSandboxInheritance()

tests/unit/MessageTest.php

+157
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,162 @@ public function testSetGet()
6060
6161
$message->setBcc($bcc);
6262
$this->assertContains($bcc, array_keys($message->getBcc()), 'Unable to set bcc!');
63+
64+
$text = 'Text email';
65+
$message->setTextBody($text);
66+
67+
$html = '<a>Html email</a>';
68+
$message->setHtmlBody($html);
69+
70+
$template = 'template-id';
71+
$message->setTemplateId($template);
72+
$this->assertEquals($template, $message->getTemplateId());
73+
74+
$campaign = 'campaign-id';
75+
$message->setCampaign($campaign);
76+
$this->assertEquals($campaign, $message->getCampaign());
77+
78+
$description = 'description';
79+
$message->setDescription($description);
80+
$this->assertEquals($description, $message->getDescription());
81+
82+
$metadata = [
83+
'key' => 'value',
84+
'key1' => 'value1',
85+
];
86+
$message->setMetadata($metadata);
87+
$this->assertEquals($metadata, $message->getMetadata());
88+
}
89+
90+
public function testSubstitutionData()
91+
{
92+
$message = new Message();
93+
94+
$data = [
95+
'key' => 'value',
96+
'ke1' => 'value',
97+
'keykey' => [
98+
'123',
99+
'456',
100+
'789',
101+
],
102+
];
103+
$message->setSubstitutionData($data);
104+
$this->assertEquals($data, $message->getSubstitutionData());
105+
}
106+
107+
public function testMultipleToAndCcBcc()
108+
{
109+
$message = new Message();
110+
111+
$recipients = [
112+
113+
'[email protected]' => 'name',
114+
115+
'[email protected]' => 'name1',
116+
];
117+
118+
$message->setTo($recipients);
119+
$this->assertEquals($recipients, $message->getTo());
120+
121+
// check if cc, bcc do not affect 'to'
122+
123+
$message->setCc($cc);
124+
125+
$bcc = [
126+
127+
'[email protected]' => 'bcc name',
128+
];
129+
$message->setBcc($bcc);
130+
131+
$this->assertEquals($recipients, $message->getTo());
132+
133+
$this->assertEquals([$cc], $message->getCc());
134+
$this->assertEquals($bcc, $message->getBcc());
135+
}
136+
137+
public function testAttachFile()
138+
{
139+
$message = new Message();
140+
141+
$fileName = __FILE__;
142+
$message->attach($fileName);
143+
144+
$attachments = $message->getAttachments();
145+
$this->assertCount(1, $attachments);
146+
$attachment = $attachments[0];
147+
$this->assertEquals($attachment['name'], basename($fileName));
148+
$this->assertEquals($attachment['type'], \yii\helpers\FileHelper::getMimeType($fileName));
149+
$this->assertEquals($attachment['data'], base64_encode(file_get_contents($fileName)));
150+
151+
$message->attach($fileName);
152+
$attachments = $message->getAttachments();
153+
$this->assertCount(2, $attachments);
154+
}
155+
156+
public function testAttachContent()
157+
{
158+
$message = new Message();
159+
160+
$fileName = 'test.txt';
161+
$fileContent = 'Test attachment content';
162+
$message->attachContent($fileContent, ['fileName' => $fileName]);
163+
164+
$attachments = $message->getAttachments();
165+
$this->assertCount(1, $attachments);
166+
$attachment = $attachments[0];
167+
$this->assertEquals($attachment['name'], $fileName);
168+
$this->assertEquals($attachment['data'], base64_encode($fileContent));
169+
170+
$message->attachContent($fileContent);
171+
$attachments = $message->getAttachments();
172+
$this->assertCount(2, $attachments);
173+
$attachment = $attachments[1];
174+
$this->assertEquals($attachment['name'], 'file_1');
175+
}
176+
177+
public function testEmbedFile()
178+
{
179+
$message = new Message();
180+
181+
$fileName = __DIR__ . '/../test_image.png';
182+
183+
$cid = $message->embed($fileName);
184+
$this->assertEquals('image_0', $cid);
185+
186+
$images = $message->getImages();
187+
$this->assertCount(1, $images);
188+
$image = $images[0];
189+
$this->assertEquals($image['name'], 'test_image.png');
190+
$this->assertEquals($image['type'], \yii\helpers\FileHelper::getMimeType($fileName));
191+
$this->assertEquals($image['data'], base64_encode(file_get_contents($fileName)));
192+
193+
$cid1 = $message->embed($fileName);
194+
$images = $message->getImages();
195+
$this->assertEquals('image_1', $cid1);
196+
$this->assertCount(2, $images);
197+
}
198+
199+
public function testEmbedContent()
200+
{
201+
$message = new Message();
202+
203+
$fileName = __DIR__ . '/../test_image.png';
204+
205+
$cid = $message->embedContent(file_get_contents($fileName), ['fileName' => 'test_image.png']);
206+
$this->assertEquals('image_0', $cid);
207+
208+
$images = $message->getImages();
209+
$this->assertCount(1, $images);
210+
$image = $images[0];
211+
$this->assertEquals($image['name'], 'test_image.png');
212+
$this->assertEquals($image['data'], base64_encode(file_get_contents($fileName)));
213+
214+
$cid1 = $message->embedContent(file_get_contents($fileName));
215+
$images = $message->getImages();
216+
$this->assertCount(2, $images);
217+
$image = $images[1];
218+
$this->assertEquals($image['name'], 'image_1');
219+
$this->assertEquals($cid1, 'image_1');
63220
}
64221
}

0 commit comments

Comments
 (0)