-
Notifications
You must be signed in to change notification settings - Fork 4
/
MessagingTest.php
119 lines (95 loc) · 4.37 KB
/
MessagingTest.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
<?php
use App\Helpers\HttpStatusCodes;
use App\Helpers\MailHelper;
use App\Mail\ContactMessage;
class MessagingTest extends TestCase
{
private $data = [ 'data' => [
'type' => 'contactMessage',
'attributes' => [
'name' => 'Test Name',
'email' => '[email protected]',
'message' => 'Test message.',
],
]];
public function setUp()
{
parent::setUp();
config(['mail.driver' => 'log']);
}
/* EMAIL HELPER ***********************************************************/
public function testMailHelper_ErrorInvalidEmail()
{
$invalidData = $this->data['data']['attributes'];
$invalidData['email'] = 'invalid-email';
$jsonApiResponse = MailHelper::sendEmail($invalidData['email'], new ContactMessage($invalidData));
$this->assertEquals($jsonApiResponse->status(), HttpStatusCodes::CLIENT_ERROR_UNPROCESSABLE_ENTITY);
$this->assertEquals('email', $jsonApiResponse->getData()->errors[0]->source->parameter);
$this->assertEquals('Email Error', $jsonApiResponse->getData()->errors[0]->title);
$this->assertEquals(
'Address in mailbox given [' . $invalidData['email'] . '] does not comply with RFC 2822, 3.6.2.',
$jsonApiResponse->getData()->errors[0]->detail
);
}
public function testMailHelper_ErrorWrongMailgunDomain()
{
$this->markTestSkipped('Skip: Do not access Mailgun with wrong domain often to avoid being blacklisted.');
config(['mail.driver' => 'mailgun']);
config(['services.mailgun.domain' => 'invalid-domain']);
$jsonApiResponse = MailHelper::sendEmail(
$this->data['data']['attributes']['email'],
new ContactMessageEmail($this->data['data']['attributes'])
);
$this->assertEquals($jsonApiResponse->status(), HttpStatusCodes::CLIENT_ERROR_BAD_REQUEST);
$this->assertEquals('email', $jsonApiResponse->getData()->errors[0]->source->parameter);
$this->assertEquals(
'The contact message cannot be sent because of an error sending the email.',
$jsonApiResponse->getData()->errors[0]->title
);
}
public function testMailHelper_ErrorWrongMailgunSecret()
{
$this->markTestSkipped('Skip: Do not access the valid domain with wrong key often to avoid being blacklisted.');
config(['mail.driver' => 'mailgun']);
config(['services.mailgun.secret' => 'invalid-secret']);
$jsonApiResponse = MailHelper::sendEmail(
$this->data['data']['attributes']['email'],
new ContactMessageEmail($this->data['data']['attributes'])
);
$this->assertEquals($jsonApiResponse->status(), HttpStatusCodes::CLIENT_ERROR_BAD_REQUEST);
$this->assertEquals('email', $jsonApiResponse->getData()->errors[0]->source->parameter);
$this->assertEquals(
'The contact message cannot be sent because of an error sending the email.',
$jsonApiResponse->getData()->errors[0]->title
);
}
/* CONTACT MESSAGE ********************************************************/
public function testSendContactMessage_ErrorNoData()
{
$this->post('/api/contact-message', [])
->seeStatusCode(HttpStatusCodes::CLIENT_ERROR_UNPROCESSABLE_ENTITY)
->seeJsonStructure($this->jsonApiErrorStructure);
}
public function testSendContactMessage_ErrorEmptyEmail()
{
$invalidData = $this->data;
$invalidData['data']['attributes']['email'] = '';
$this->post('/api/contact-message', $invalidData)
->seeStatusCode(HttpStatusCodes::CLIENT_ERROR_UNPROCESSABLE_ENTITY)
->seeJsonStructure($this->jsonApiErrorStructure);
}
public function testSendContactMessage_ErrorInvalidEmail()
{
$invalidData = $this->data;
$invalidData['data']['attributes']['email'] = 'invalid-email';
$this->post('/api/contact-message', $invalidData)
->seeStatusCode(HttpStatusCodes::CLIENT_ERROR_UNPROCESSABLE_ENTITY)
->seeJsonStructure($this->jsonApiErrorStructure);
}
public function testSendContactMessage_Success()
{
// $this->markTestSkipped('Skip: Do not write too much on log.');
$this->post('/api/contact-message', $this->data)
->seeStatusCode(HttpStatusCodes::SUCCESS_NO_CONTENT);
}
}