几个月前我问过同样的问题,我决定测试是否收到了一条消息。
也许它违反了“不要嘲笑你不拥有的东西”的规则,但因为它对我有用,所以我决定保留它。
我不知道您是否像我一样使用Behat ,但我希望它可以帮助某人。我也在这里使用MailTrap API。这是值得的。
好吧,一旦你使用 Behat,在你FeatureContext.php创建一个这样的函数:
/**
* @Given I received a password reset token link at inbox
*/
public function iReceivedAPasswordResetTokenLinkAtInbox()
{
$inbox = env('MAILTRAP_INBOX');
$mailTrapToken = env('MAILTRAP_TOKEN');
$options = [
'headers' => [
'Api-Token' => $mailTrapToken
]
];
$client = new GuzzleHttp\Client($options);
$apiMailTrap = 'https://mailtrap.io/api/v1';
$response = $client->request('GET', "{$apiMailTrap}/inboxes/{$inbox}/messages");
$msg = json_decode($response->getBody()->getContents());
$response = $client->request('GET', "{$apiMailTrap}/inboxes/{$inbox}/".
"messages/{$msg[0]->id}/body.txt");
// Here you can get the message body and make any verification you need
$bodyTxt = $response->getBody()->getContents();
// In my case, I had to check if a URL was successfully sent, so
$this->tokenPasswordReset = trim(substr($bodyTxt, strpos($bodyTxt,'/resetpassword/') + 15, 60));
print('token: '.$this->tokenPasswordReset);
// Then I made my assertion
PHPUnit::assertNotEmpty($this->tokenPasswordReset);
}
在我的例子中,一旦我有了上面的函数,为了测试一个端点,我简单地添加了一个名为resetpw.feature包含的文件:
Feature: Changing Password
In order to reset an user password
As an user
I want to reset my password and update it
@password
Scenario: Users want to reset their passwords
Given I send a POST request to "/resetpassword" with parameters:
| key | value |
| email | behat@mytests.dev |
Then the response status code should be 200
Given I received a password reset token link at inbox
# And here comes the previously created function!
When I access a password reset link with this token
Then the response status code should be 200
然后我跑去behat --tags @password测试。
而已。我希望它可以激励某人!