-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest.php
171 lines (154 loc) · 6.99 KB
/
test.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
//<gen>phpunit_import
require_once(__DIR__ . '/vendor/autoload.php');
//</gen>
use PHPUnit\Framework\TestCase;
/**
* This testsuite demonstrates how to use MailSlurp Email API Client in PHP
*
* MailSlurp let's you create real email addresses in PHP. You can then send and receive emails
* and attachments in PHP applications and tests.
*
* See https://www.mailslurp.com/docs/ for more information.
*
*/
final class EmailTest extends TestCase
{
public function test_simpleUsecase() {
//<gen>phpunit_simple_usecase
// configure mailslurp/mailslurp-client-php library
$config = MailSlurp\Configuration::getDefaultConfiguration()
->setApiKey('x-api-key', getenv("API_KEY"));
$inboxController = new MailSlurp\Apis\InboxControllerApi(null, $config);
// create an inbox
$inbox = $inboxController->createInboxWithDefaults();
// send an email
$sendOptions = new MailSlurp\Models\SendEmailOptions();
$sendOptions->setTo([$inbox->getEmailAddress()]);
$sendOptions->setSubject("Test");
$sendOptions->setBody("Hello");
$inboxController->sendEmail($inbox->getId(), $sendOptions);
// receive the email
$waitForController = new MailSlurp\Apis\WaitForControllerApi(null, $config);
$email = $waitForController->waitForLatestEmail($inbox->getId(), 120000, true);
$this->assertNotNull($email->getBody());
//</gen>
}
//<gen>phpunit_get_config
private function getConfig()
{
// create a mailslurp configuration with API_KEY environment variable
// get your own free API KEY at https://app.mailslurp.com/sign-up/
return MailSlurp\Configuration::getDefaultConfiguration()
->setApiKey('x-api-key', getenv("API_KEY"));
}
//</gen>
public function test_CanCreateAnInbox_ThenSendAndReceiveEmails()
{
// create an inbox controller
//<gen>phpunit_create_controller
$inboxController = new MailSlurp\Apis\InboxControllerApi(null, $this->getConfig());
//</gen>
// create an inbox
//<gen>phpunit_create_inbox
$options = new \MailSlurp\Models\CreateInboxDto();
$options->setName("Test inbox");
$options->setPrefix("test");
$inbox = $inboxController->createInboxWithOptions($options);
//</gen>
// verify inbox has an email address ending in @mailslurp.com
$this->assertStringContainsString(
"mailslurp.com",
$inbox->getEmailAddress()
);
}
public function test_SmtpAccess()
{
$inboxController = new MailSlurp\Apis\InboxControllerApi(null, $this->getConfig());
// create an smtp inbox
//<gen>phpunit_create_smtp_inbox
$options = new \MailSlurp\Models\CreateInboxDto();
$options->setInboxType("SMTP_INBOX");
$inbox_smtp = $inboxController->createInboxWithOptions($options);
//</gen>
$inbox_recipient = $inboxController->createInboxWithDefaults();
//<gen>phpunit_phpmailer_config
$access_details = $inboxController->getImapSmtpAccess($inbox_smtp->getId());
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
// set mail server settings using the inbox access details
$mail->isSMTP();
$mail->Host = $access_details->getSecureSmtpServerHost();
$mail->SMTPAuth = true;
$mail->Username = $access_details->getSecureSmtpUsername();
$mail->Password = $access_details->getSecureSmtpPassword();
$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = $access_details->getSecureSmtpServerPort();
//</gen>
//<gen>phpunit_smtp_send
$mail->setFrom($inbox_smtp->getEmailAddress(), $inbox_smtp->getName());
$mail->addAddress($inbox_recipient->getEmailAddress()); //Add a recipient
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Testing smtp sending';
$mail->Body = 'This is the body';
$mail->send();
//</gen>
//<gen>phpunit_smtp_receive
$wait_for_controller = new MailSlurp\Apis\WaitForControllerApi(null, $this->getConfig());
$email = $wait_for_controller->waitForLatestEmail($inbox_recipient->getId());
$this->assertStringContainsString("smtp sending", $email->getSubject());
//</gen>
}
public function test_CanSendAndReceiveEmail_BetweenTwoInboxes()
{
// create inbox and waitFor controllers
$inbox_controller = new MailSlurp\Apis\InboxControllerApi(null, $this->getConfig());
//<gen>phpunit_create_waitfor_controller
$wait_for_controller = new MailSlurp\Apis\WaitForControllerApi(null, $this->getConfig());
//</gen>
// create two inboxes
$inbox = $inbox_controller->createInbox();
$inbox_2 = $inbox_controller->createInbox();
// send a confirmation code from inbox1 to inbox2 (sends an actual email)
//<gen>phpunit_send_email
$send_options = new MailSlurp\Models\SendEmailOptions();
$send_options->setTo([$inbox->getEmailAddress()]);
$send_options->setSubject("Test");
$send_options->setBody("Confirmation code = abc123");
$inbox_controller->sendEmail($inbox_2->getId(), $send_options);
//</gen>
// receive email for inbox2
//<gen>phpunit_wait_for_email
$timeout_ms = 30000;
$unread_only = true;
$email = $wait_for_controller->waitForLatestEmail($inbox->getId(), $timeout_ms, $unread_only);
//</gen>
//<gen>phpunit_email_count
$inbox_email_count = $inbox_controller->getInboxEmailCount($inbox->getId());
//</gen>
$this->assertGreaterThan(0, $inbox_email_count);
//<gen>phpunit_get_emails
$emails = $inbox_controller->getEmails($inbox->getId());
//</gen>
//<gen>phpunit_get_emails_paginated
$emails_paginated = $inbox_controller->getInboxEmailsPaginated($inbox->getId(), $page = 0, $size = 20);
$email = $emails_paginated->getContent()[0];
//</gen>
$email_id = $emails[0]->getId();
//<gen>phpunit_get_email
$email_controller = new MailSlurp\Apis\EmailControllerApi(null, $this->getConfig());
$email = $email_controller->getEmail($email_id);
//</gen>
// verify emails content
$this->assertEquals($inbox_2->getEmailAddress(), $email->getFrom());
$this->assertEquals($inbox->getEmailAddress(), $email->getTo()[0]);
$this->assertEquals("Test", $email->getSubject());
$this->assertStringContainsString("Confirmation code = ", $email->getBody());
// extract part of an email using regex (could be used in further test steps)
//<gen>phpunit_extract_pattern
$matches = array();
preg_match('/.+code = (.+)/', $email->getBody(), $matches);
$confirmation_code = $matches[1];
$this->assertEquals($confirmation_code, "abc123");
//</gen>
}
}