diegohq / mailcatcher
v1.3
2018-03-17 17:25 UTC
Requires
- php: >=5.3.3
- symfony/dom-crawler: ~2.3|~3.0|~4.0
Requires (Dev)
- behat/behat: ~3.0
- phpunit/phpunit: ~4.6
- swiftmailer/swiftmailer: ~5.0
README
在PHP应用程序中集成MailCatcher。
MailCatcher是一个简单的SMTP服务器,具有HTTP API,此库旨在将其集成,使其易于与PHP一起使用。
Behat扩展
此库提供Behat扩展,帮助您测试应用程序中的邮件。
要使用它,您首先需要确保MailCatcher已正确安装并运行。您可以使用docker来执行它
docker run -d -p 1080:1080 -p 1025:1025 --name mailcatcher schickling/mailcatcher
首先,在您的behat.yml
中进行配置
default: extensions: Alex\MailCatcher\Behat\MailCatcherExtension\Extension: url: http://localhost:1080 purge_before_scenario: true
然后,在您的FeatureContext
文件中添加MailCatcherContext
上下文
use Alex\MailCatcher\Behat\MailCatcherContext; use Behat\Behat\Context\BehatContext; class FeatureContext extends BehatContext { public function __construct(array $parameters) { $this->useContext('mailcatcher', new MailCatcherContext()); } }
可用步骤
此扩展在测试中为您提供了邮件上下文。要使用断言,您必须首先使用标准打开邮件。
一旦打开,您就可以对它进行断言,并点击它。
服务器操作
删除服务器上的所有消息
- 当我清除邮件时
邮件打开
- 当我打开来自"foo@example.org"的邮件时
- 当我打开包含"a message"的邮件时
- 当我打开发送给"me@example.org"的邮件时
- 当我打开主题为"Welcome, mister Bond!"的邮件时
断言
验证发送到服务器的邮件数量
- 然后应该发送1封邮件
- 然后应该发送13封邮件
验证消息中的文本存在
- 然后我应该看到邮件中的"something"
- 然后我应该看到邮件中的"something else"
验证未打开邮件中的文本存在
- 然后我应该看到来自"foo@example.org"的邮件
- 然后我应该看到包含"a message"的邮件
- 然后我应该看到发送给"me@example.org"的邮件
- 然后我应该看到主题为"Welcome, mister Bond!"的邮件
自定义mailcatcher上下文
仅从PHP 5.4可用
如果您想创建一个与MailCatcher相关的上下文类,您可以使用MailCatcherTrait
来获取注入到您的类中的mailcatcher客户端
use Alex\MailCatcher\Behat\MailCatcherAwareInterface; use Alex\MailCatcher\Behat\MailCatcherTrait; use Alex\MailCatcher\Message; use Behat\Behat\Context\Context; class WelcomeContext implements Context, MailCatcherAwareInterface { use MailCatcherTrait; /** * @Then /^a welcome mail should be sent$/ */ public function testTrait() { $this->findMail(Message::SUBJECT_CRITERIA, 'Welcome!'); } }
此特性提供了以下方法
- getMailCatcherClient():返回mailcatcher客户端实例。
- findMail($criteria, $value):搜索给定消息的功能,如果找不到则抛出异常
不要忘记实现MailCatcherAwareInterface
以在您的上下文类中注入mailcatcher客户端。
客户端API
使用集成SDK轻松浏览您的API
$client = new Alex\MailCatcher\Client('http://localhost:1080'); // Returns all messages $messages = $client->search(); // Count messages $client->getMessageCount(); // Filter messages $messages = $client->search(array( 'from' => 'bob@example.org', 'to' => 'alice@example.org', 'subject' => 'Bla', 'contains' => 'Hello', 'attachments' => true, 'format' => 'html', ), $limit = 3); // Search one message $message = $client->searchOne(array('subject' => 'Welcome'));
消息API
// Message API, get the content of a message $subject = $message->getSubject(); $plainTextBody = $message->getPart('text/plain')->getContent(); $htmlBody = $message->getPart('text/html')->getContent(); // Message API, return a Person object or an array of Person object $person = $message->getFrom(); $persons = $message->getRecipients(); // Person API $person = $message->getFrom(); $name = $person->getName(); // null means not provided $mail = $person->getMail(); // Attachments $message->hasAttachments(); $message->getAttachments(); // Delete $message->delete();
附件API
// Attachment API $attachment->getFilename(); $attachment->getSize(); $attachment->getType(); $attachment->getContent();