php PHPUnit的Mailtrap测试特性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php PHPUnit的Mailtrap测试特性相关的知识,希望对你有一定的参考价值。

<?php

namespace Tests;

use GuzzleHttp\Client;

trait MailtrapTrait
{
    public $mailtrap;
    protected $mailtrap_inbox;

    public function usesMailtrap()
    {
        // Create connection mailtrap.io
        $this->mailtrap = new Client(
            [
                'base_uri' => getenv('MAILTRAP_API_BASE_URI'),
                'headers' => [
                    'Api-Token' => getenv('MAILTRAP_API_TOKEN'),
                ],
            ]
        );

        $this->mailtrap_inbox = getenv('MAILTRAP_API_INBOX');

        // Clean messages of mailtrap between each tests
        $this->cleanMessages();
    }

    public function assertEmailIsSent($description = '')
    {
        $this->assertNotEmpty($this->getMessages(), $description);
    }

    /**
     * Clean Messages of the mailtrap inbox.
     */
    protected function cleanMessages()
    {
        $this->mailtrap->request('PATCH', "inboxes/$this->mailtrap_inbox/clean");
    }

    /**
     * Fetch the last message received in mailtrap inbox.
     *
     * @return object Message
     */
    protected function getLastMessage()
    {
        $messages = $this->getMessages();
        if (empty($messages)) {
            $this->fail('Api Mailtrap: No messages found.');
        }

        return $messages[0];
    }

    /**
     * Fetch messages of the mailtrap inbox.
     *
     * @return json The messages of the inbox given
     */
    protected function getMessages()
    {
        $response = $this->mailtrap->request('GET', "inboxes/$this->mailtrap_inbox/messages");

        return json_decode((string) $response->getBody());
    }
}

以上是关于php PHPUnit的Mailtrap测试特性的主要内容,如果未能解决你的问题,请参考以下文章

掌握 Laravel 的测试方法

phpunit 单元测试

夯实PHP基础PHPUnit -- PHP测试框架

PHP单元测试框架PHPUnit的使用

windows 下 wamp php单元测试工具PHPUnit的安装

我无法使用 phpunit 进行测试:无法打开文件“autoload.php”