-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
ErrorNotificationEmailTest.php
143 lines (130 loc) · 5.57 KB
/
ErrorNotificationEmailTest.php
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
<?php
declare(strict_types=1);
namespace EthanYehuda\CronjobManager\Test\Integration;
use EthanYehuda\CronjobManager\Model\ErrorNotificationEmail;
use Magento\Cron\Model\Schedule;
use Magento\Framework\Mail\Message;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
use Magento\TestFramework\ObjectManager;
use PHPUnit\Framework\TestCase;
/**
* @magentoAppArea crontab
* @magentoAppIsolation enabled
*/
class ErrorNotificationEmailTest extends TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var ErrorNotificationEmail
*/
private $errorNotificationEmail;
/**
* @var \Magento\TestFramework\Mail\Template\TransportBuilderMock
*/
private $transportBuilder;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->transportBuilder = $this->objectManager->get(TransportBuilderMock::class);
$this->objectManager->addSharedInstance(
$this->transportBuilder,
TransportBuilder::class
);
$this->errorNotificationEmail = $this->objectManager->get(ErrorNotificationEmail::class);
}
/**
* @magentoConfigFixture default_store system/cron_job_manager/email_notification 0
* @magentoAdminConfigFixture system/cron_job_manager/email_recipients errors@example.com,other@example.com
* @magentoAdminConfigFixture system/cron_job_manager/email_identity general
* @magentoConfigFixture current_store trans_email/ident_general/name No-Reply
* @magentoConfigFixture current_store trans_email/ident_general/email noreply@example.com
*/
public function testDoNotSendIfConfigurationDisabled()
{
$this->givenScheduleWithData(
[
'job_code' => 'dummy_job_code',
'executed_at' => '1999-12-31 23:59:00',
'finished_at' => '1900-01-01 00:00:00',
'status' => Schedule::STATUS_ERROR,
'messages' => "Hello, I am the <Y2K> Bug\n\nHere be stacktrace",
],
$schedule
);
$this->whenNotificationIsSent($schedule, $sentMessage);
$this->thenEmailShouldNotBeSent($sentMessage);
}
/**
* @magentoAdminConfigFixture system/cron_job_manager/email_notification 1
* @magentoAdminConfigFixture system/cron_job_manager/email_recipients errors@example.com,other@example.com
* @magentoAdminConfigFixture system/cron_job_manager/email_identity general
* @magentoConfigFixture current_store trans_email/ident_general/name No-Reply
* @magentoConfigFixture current_store trans_email/ident_general/email noreply@example.com
*/
public function testSentWithTemplateToConfiguredAddresses()
{
$this->givenScheduleWithData(
[
'job_code' => 'dummy_job_code',
'executed_at' => '1999-12-31 23:59:00',
'finished_at' => '1900-01-01 00:00:00',
'status' => Schedule::STATUS_ERROR,
'messages' => "Hello, I am the <Y2K> Bug\n\nHere be stacktrace",
],
$schedule
);
$this->whenNotificationIsSent($schedule, $sentMessage);
$this->thenEmailShouldBeSent($sentMessage, 'noreply@example.com', ['errors@example.com', 'other@example.com']);
$this->andEmailShouldHaveContents(
$sentMessage,
[
'job_code' => '<td>dummy_job_code</td>',
'messages' => "<td>Hello, I am the <Y2K> Bug<br />\n<br />\nHere be stacktrace</td>",
'exeuted_at' => '<td>1999-12-31 23:59:00</td>',
'finished_at' => '<td>1900-01-01 00:00:00</td>',
]
);
}
private function givenScheduleWithData(array $scheduleData, &$schedule): void
{
$schedule = $this->objectManager->create(
Schedule::class,
[
'data' => $scheduleData,
]
);
}
private function whenNotificationIsSent($schedule, &$sentMessage): void
{
$this->errorNotificationEmail->sendFor($schedule);
$sentMessage = $this->transportBuilder->getSentMessage();
}
private function thenEmailShouldBeSent(?Message $sentMessage, string $expectedSender, array $expectedRecipients)
{
$this->assertNotNull($sentMessage, 'A mail should have been sent');
$messageDetails = \Zend\Mail\Message::fromString($sentMessage->getRawMessage());
$this->assertEquals([$expectedSender], \array_keys(\iterator_to_array($messageDetails->getFrom())));
$this->assertEquals($expectedRecipients, \array_keys(\iterator_to_array($messageDetails->getTo())));
}
private function thenEmailShouldNotBeSent(?Message $sentMessage)
{
$this->assertNull($sentMessage, 'A mail should not have been sent');
}
private function andEmailShouldHaveContents(Message $sentMessage, array $expectedContents): void
{
$content = $sentMessage->getBody()->getParts()[0]->getContent();
$content = \Zend_Mime_Decode::decodeQuotedPrintable($content);
foreach ($expectedContents as $expectedKey => $expectedContent) {
if (\method_exists($this, 'assertStringContainsString')) {
$this->assertStringContainsString($expectedContent, $content, "Content should contain $expectedKey");
} else {
$this->assertContains($expectedContent, $content, "Content should contain $expectedKey");
}
}
}
}