A while back, mailtrap changed the way the mail body was accessed through their api. I wanted to share how I solved the issue so others can keep using their amazing service.
Using the register user with email confirmation test as an example.
public function test_register_user_with_email_confirmation()
{
$this->setSettings([
'reg_enabled' => true,
'reg_email_confirmation' => true,
'registration.captcha.enabled' => false,
'tos' => false
]);
$data = [
'email' => 'john.doe@test.com',
'username' => 'john.doe',
'password' => '123123',
'password_confirmation' => '123123'
];
$this->postJson("/api/register", $data);
$expected = array_except($data, ['password', 'password_confirmation']);
$this->seeStatusCode(201)
->seeJson([
'requires_email_confirmation' => true
])
->seeInDatabase('users', $expected);
$token = User::where('email', $data['email'])->first()->confirmation_token;
$message = $this->fetchInbox()[0];
$this->assertEquals($data['email'], $message['to_email']);
$this->assertEquals(config('mail.from.address'), $message['from_email']);
$this->assertEquals(config('mail.from.name'), $message['from_name']);
$messageBody = $this
->requestClient()
->get("/api/v1/inboxes/{$message['inbox_id']}/messages/{$message['id']}/body.html")
->getBody()->getContents();
$this->assertContains(
trans('app.thank_you_for_registering', ['app' => settings('app_name')]),
trim($messageBody)
);
$this->assertContains(
trans('app.confirm_email_on_link_below'),
trim($messageBody)
);
$this->assertContains(
route('register.confirm-email', $token),
trim($messageBody)
);
$this->emptyInbox();
}
Hey Cecil,
Thanks for sharing this! The latest version of Vanguard is not using Mailtrap.io anymore, but if someone is still using previous versions then they can update their tests to work properly with Mailtrap.
Kind regards,
Milos