LoginSignup
5
2

More than 5 years have passed since last update.

Cakephp3のTestでメールを送りたくない

Last updated at Posted at 2017-11-14

Cake\Mailer\Mailer などでメール送信を行っている際にTestで実メールを送らない方法を調べた。mb_send_mailとか使ってる場合は当てはまらない。

mock化する場合

正攻法だがちょっと大変

EmailTransportをDebugTransportにする


class HogeTest extends IntegrationTestCase
{
    public function setUp()
    {
        parent::setUp();

        // 一度dropしないと設定できない
        // config/app.phpでEmailやEmailTransportの設定を確認して使用中のものを引数で渡す
        \Cake\Mailer\Email::dropTransport('default');
        \Cake\Mailer\Email::setConfigTransport('default', [
            'className' => 'Debug',
        ]);
    }

    // ... 
}

setUp() に書けばクラス全体、個別testメソッドに書けばそこだけ反映される。 tests/bootstrap.php にも書ける。cakephp-3.3.16 で確認。

参考

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2